如何解决评估初始值的对数概率时出错Stan 错误
我正在尝试在 Stan 中运行具有异方差性的向量自回归模型 (VAR (1))。我可以使用 JAGS 成功运行模型,但我不知道为什么 Stan 在运行相同模型时会出现一些错误。这是数据和模型:
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
#Simulating data:
T <- 100
alpha <- 1
gamma_1 <- 1
gamma_2 <- 0.4
sigma <- y <- rep(NA,length = T)
set.seed(123)
sigma[1] <- runif(1)
y[1] <- 0
for (t in 2:T) {
sigma[t] <- sqrt(gamma_1 + gamma_2 * (y[t - 1] - alpha)^2)
y[t] <- rnorm(1,mean = alpha,sd = sigma[t])
}
df <- data.frame(y1 = y,y2 = y,x = rnorm(100,1))
model_code <- "
data{
int<lower=1> T; //Time
int<lower=2> K; //location
matrix[T,K] y; //Target variable
vector[T] x; //Covariate
}
parameters {
vector[K] alpha; //Modelling mean: intercept
real<lower=0> sigma; //Modelling y: variance
matrix[K,K] theta; //AR(1) coefficient matrix
row_vector[K] mu_t1 ; //Initial values of the AR process
vector[K] beta; //Covariate's effect coefficient
}
transformed parameters {
matrix[T,K] epsilon; //Residuals (innovation)
matrix[T,K] mu; //Mean of the process
mu[1,] = mu_t1 ; //Initial values of the time-series
epsilon[1,] = y[1,] - mu[1,];
for(k in 1:K){
for (t in 2:T){
mu[t,k] = alpha[k] + theta[k,] * epsilon[t - 1,]' + beta[k] * x[t];
epsilon[t,k] = y[t,k] - mu[t,k] ;
}
}
}
model{
//priors
for(k in 1:K){
alpha[k] ~ normal(0,3);
beta[k] ~ normal(0,10);
theta[k,] ~ normal(0,1);
}
mu_t1 ~ normal(7,1) ;
sigma ~ normal(0,5);
//Model likelihood
for(k in 1:K){
for (t in 1:T)
y[t,k] ~ normal(mu[t,k],sigma);
}
}
"
model_data <- list(
T = nrow(df),K = 2,x = df$x,y = df[,1:2]
)
stan_run <- stan(
data = model_data,model_code = model_code
)
当我运行这段代码时,Stan 在开始采样之前停下来说:
链 2:拒绝初始值:链 2:评估日志时出错 初始值的概率。链 2:例外:normal_lpdf: 位置参数是nan,但必须是有限的! (在 'model290f30a800bc_9a829e355b070cb7ca3039bdb9dcc780' 在第 43 行)
我不确定为什么它不能以初始值评估对数概率。我的输入没有任何问题。有谁知道我的代码出了什么问题?
解决方法
我在转换后的参数块中使用了 print()
语句来查看哪个值是 Nan(正如错误中所述)。所以这就是我所做的:
"
transformed parameters {
matrix[T,K] epsilon; //Residuals (innovation)
matrix[T,K] mu; //Mean of the process
mu[1,] = mu_t1 ; //Initial values of the time-series
epsilon[1,] = y[1,] - mu[1,];
for(k in 1:K){
for (t in 2:T){
mu[t,k] = alpha[k] + theta[k,] * epsilon[t - 1,]' + beta[k] * x[t];
epsilon[t,k] = y[t,k] - mu[t,k] ;
}
}
print('mu =',mu);
print('epsilon =',epsilon);
print('theta =',theta);
print('beta =',beta);
}
"
参数 mu 是罪魁祸首。我发现我的错误是在转换后的参数块中错误地定义了循环。 k 循环应该在 t 循环之后,替换它们可以修复错误并且模型运行没有问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。