Skip to content

Commit 6faa10e

Browse files
author
Bob Carpenter
committed
added normal mixture of K components to test model in manual
1 parent e504d49 commit 6faa10e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
K <- 3;
2+
3+
N1 <- 500;
4+
N2 <- 300;
5+
N3 <- 200;
6+
N <- N1 + N2 + N3;
7+
mu1 <- -2.0;
8+
mu2 <- 0.5;
9+
mu3 <- 3.5;
10+
sigma1 <- 1.0;
11+
sigma2 <- 0.5;
12+
sigma3 <- 2;
13+
14+
y <- c(rnorm(N1,mu1,sigma1),rnorm(N2,mu2,sigma2),rnorm(N3,mu3,sigma3));
15+
16+
library("rstan");
17+
fit <- stan(file="normal_mixture_k.stan", data=("K","N","y"), iter=500, chains=1, init=0);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data {
2+
int<lower=1> K;
3+
int<lower=1> N;
4+
real y[N];
5+
}
6+
parameters {
7+
simplex[K] theta;
8+
real mu[K];
9+
real<lower=0,upper=10> sigma[K];
10+
}
11+
model {
12+
real ps[K];
13+
mu ~ normal(0,10);
14+
for (n in 1:N) {
15+
for (k in 1:K)
16+
ps[k] <- log(theta[k])
17+
+ normal_log(y[n],mu[k],sigma[k]);
18+
lp__ <- lp__ + log_sum_exp(ps);
19+
}
20+
}

0 commit comments

Comments
 (0)