stan icon indicating copy to clipboard operation
stan copied to clipboard

gaussian_dlm_obs should allow nonnegative-definite matrices

Open ksvanhorn opened this issue 6 years ago • 2 comments

Summary:

Although it is not specified in the manual, gaussian_dlm_obs() gives an error if the C0 argument is nonnegative definite instead of positive definite.

Description:

The C0 argument is the prior covariance matrix for the latent state vector. It is perfectly legitimate, and sometimes useful, for C0 (likewise W) to be merely nonnegative-definite instead of positive definite.

Not sure if this should be a bug or an enhancement request, but at a minimum the manual should make it clear when nonnegative-definite is not good enough.

Reproducible Steps:

Program:

data {
  int<lower = 1> N;
  vector[N] y;
  real<lower = 0.0, upper = 1.0> rho_;
  real mu_;
  real<lower = 0.0> sigma_;
  real<lower = 0.0> sigma_h_scale_;
}
transformed data {
  matrix[1, N] yy = to_matrix(y');
  vector[2] Z_0_ = to_vector({1.0, 1.0});
  vector[2] a1_0_ = to_vector({0.0, mu_});
  matrix[2, 2] P1_0_ = diag_matrix(to_vector({square(sigma_), 0.0}));
}
parameters {
  real<lower = 0.0> raw_0_;
}
transformed parameters {
}
model {
  real sigma_h_ = raw_0_ * sigma_h_scale_;
  real phi_ = sqrt(1.0 - square(rho_));
  real H_0_ = square(sigma_h_);
  matrix[2, 2] T_0_ = diag_matrix(to_vector({phi_, 1.0}));
  matrix[2, 2] Q_0_ = diag_matrix(to_vector({square(rho_ * sigma_), 0.0}));
  raw_0_ ~ normal(0.0, 1.0) T[0.0, ];
  yy ~ gaussian_dlm_obs(to_matrix(Z_0_), T_0_, rep_matrix(H_0_, 1, 1), Q_0_, a1_0_, P1_0_);
}

data:

list(
  N=100,
  y=c(118, 123, 123, 125, 131, 103, 103, 112, 89, 99, 96, 100, 107, 80, 91, 110, 98, 115, 98, 81, 86, 82, 74, 81, 72, 95, 94, 120, 83, 59, 79, 60, 79, 81, 100, 111, 50, 112, 103, 86, 93, 106, 91, 114, 122, 82, 113, 92, 102, 99, 100, 128, 113, 96, 116, 116, 93, 123, 104, 107, 120, 114, 136, 95, 136, 140, 132, 145, 151, 139, 136, 123, 148, 115, 117, 143, 132, 118, 101, 106, 122, 119, 78, 98, 133, 109, 117, 129, 125, 112, 133, 113, 80, 128, 135, 137, 150, 141, 142, 131),
  rho_=0.1,
  mu_=100,
  sigma_=50,
  sigma_h_scale_=30)

Current Output:

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: gaussian_dlm_obs_lpdf: C0 is not positive definite.

Current Version:

v2.17

ksvanhorn avatar Oct 06 '18 00:10 ksvanhorn

I had to look up in the Wikipedia to figure out "positive semidefinite" and "nonnegative definite" are synonyms.

Is this one of these cases where semidefiniteness means that normalization is over some subspace?

bob-carpenter avatar Oct 08 '18 01:10 bob-carpenter

Nonnegative definiteness means that eigenvalues are all nonnegative. The zero eigenvalues correspond to things known with complete certainty. The most common way this can arise is if the initial value of element k of the latent state is known with certainty to be mu[k]; then both column and row k of C0 are all zeroes.

ksvanhorn avatar Oct 08 '18 18:10 ksvanhorn