1
저는 스탠 (Stan)을 배우고 있으며 간단한 혼합 모델을 구현하고 싶습니다. 참조 설명서에 스탠 벡터화의 혼합 모델
(스탠 참조-2.14.0) 용액에 이미 존재
:data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of data points
real y[N]; // observations
}
parameters {
simplex[K] theta; // mixing proportions
real mu[K]; // locations of mixture components
real<lower=0> sigma[K]; // scales of mixture components
}
model {
real ps[K]; // temp for log component densities
sigma ~ cauchy(0, 2.5);
mu ~ normal(0, 10);
for (n in 1:N) {
for (k in 1:K) {
ps[k] = log(theta[k])
+ normal_lpdf(y[n] | mu[k], sigma[k]);
}
target += log_sum_exp(ps);
}
}
다음 페이지는 외부 루프의 벡터화 설명은 불가능하다. 그러나 내부 루프의 parallization 여전히 경우 궁금 해서요.
그리고 그래서 나는 다음과 같은 모델을 시도 :
data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of data points
real y[N]; // observations
}
parameters {
simplex[K] theta; // mixing proportions
vector[K] mu; // locations of mixture components
vector<lower=0>[K] sigma; // scales of mixture components
}
model {
vector[K] ps;//[K]; // temp for log component densities
vector[K] ppt;
sigma ~ cauchy(0, 2.5);
mu ~ normal(0, 10);
for (n in 1:N) {
ppt = log(theta);
/*
for (k in 1:K) {
ps[k] = ppt[k] + //log(theta[k])
normal_lpdf(y[n] | mu[k], sigma[k]);
}
*/
ps = ppt + normal_lpdf(y[n] | mu, sigma);
target += log_sum_exp(ps);
}
}
을 ... 그리고 (원래 모델 반대)이 모델은 잘못된 추정을합니다.
data("faithful")
erupdata <- list(
K = 2,
N = length(faithful$eruptions),
y = faithful$eruptions
)
fiteruptions <- stan(file = 'mixturemodel.stan', data = erupdata, iter = 1000, chains = 1)
내가 궁금한 점은 모델 사양에 대해 잘못 이해하고있는 부분입니다. 그 문법이 제공하는 차이 (다른 것들은 vector[K]
과 real[K]
의 차이)를 이해하고 Stan에 대한 더 깊은 통찰력을 얻고 싶습니다.