Hi everyone,
I am trying to fit a Gaussian Process with a Covariance function specified by a prior Wishart distribution.
I am simulating some random data in R and then fit the model. I always get the error:
LDLT_Factor of random variable is not positive definite.
which is weird because the Covariance Matrix should be positive definite and symmetric by definition.
Here the full code:
library(rstan)
library(fields)
set.seed(999)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
N <- 100
x <- seq(0, 1, length.out = sqrt(N))
index <- structure(as.matrix(expand.grid(x, x)))
K <- Exponential(as.matrix(dist(index)), alpha = .5)
r_w <- rWishart(N, N, K)[,,1]
rmv <- drop(rmvnorm(n = 1, mu = 0, Sigma = r_w))
fit <-
stan(
file = "./example1.stan",
data = list(N = N,
index = index,
y_obs = rmv),
chains = 4,
iter = 1000
)
and the relative stan code:
data {
int<lower=1> N;
array[N] vector[2] index;
vector[N] y_obs;
}
parameters{
real<lower=0> sigma;
real<lower=0> length_scale;
cov_matrix[N] KW;
}
transformed parameters{
cov_matrix[N] K = gp_exponential_cov(index, sigma, length_scale) + diag_matrix(rep_vector(1e-9, N));
}
model{
sigma ~ gamma(1, 1);
length_scale ~ gamma(1, 1);
KW ~ wishart(N, K);
y_obs ~ multi_normal(rep_vector(0, N), KW);
}
Is there anything I am doing wrong?
Thank you,
Marco