我正在试验 stan 和 Gaussian Processes。在一些错误之后,我发现一切的根源是函数的奇怪行为cov_exp_quad
。
特别是我不明白为什么它返回一个不对称的矩阵。
这里是 stan 脚本:
data {
int<lower=1> N;
real x[N];
real y[N];
}
transformed data {
matrix[N,N] K = cov_exp_quad(x, y, 1, 1);
}
generated quantities {
matrix[N, N] Cov = K;
}
这里是R代码:
library(rstan)
x <- seq(0, 1, length.out = 3)
X <- expand.grid(x, x)
data_stan <- list(N = dim(X)[1],
x = X[, 1],
y = X[, 2])
fit <- stan(file = "./stan_script.stan",
data = data_stan,
algorithm = "Fixed_param",
warmup = 0,
chains = 1,
iter = 1)
samples <- rstan::extract(fit)
drop(samples$Cov)
这是,而不是输出
> drop(samples$Cov)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969 0.6065307 0.6065307 0.6065307
[2,] 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969
[3,] 0.6065307 0.6065307 0.6065307 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000
[4,] 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969 0.6065307 0.6065307 0.6065307
[5,] 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969
[6,] 0.6065307 0.6065307 0.6065307 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000
[7,] 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969 0.6065307 0.6065307 0.6065307
[8,] 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000 0.8824969 0.8824969 0.8824969
[9,] 0.6065307 0.6065307 0.6065307 0.8824969 0.8824969 0.8824969 1.0000000 1.0000000 1.0000000
为什么这不是对称的?谢谢