在初始值处评估对数概率的误差(Stan 误差)
Error evaluating the log probability at the initial value (Stan error)
我正在尝试 运行 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,0,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 在开始采样之前停下来说:
Chain 2: Rejecting initial value: Chain 2: Error evaluating the log
probability at the initial value. Chain 2: Exception: normal_lpdf:
Location parameter is nan, but must be finite! (in
'model290f30a800bc_9a829e355b070cb7ca3039bdb9dcc780' at line 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 循环之后,替换它们可以修复错误,模型运行没有问题。
我正在尝试 运行 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,0,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 在开始采样之前停下来说:
Chain 2: Rejecting initial value: Chain 2: Error evaluating the log probability at the initial value. Chain 2: Exception: normal_lpdf: Location parameter is nan, but must be finite! (in 'model290f30a800bc_9a829e355b070cb7ca3039bdb9dcc780' at line 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 循环之后,替换它们可以修复错误,模型运行没有问题。