Skip to content

Commit a72045b

Browse files
authored
Merge pull request #191 from andrjohns/feature/basic_est_update
Feature/basic est update
2 parents e2de966 + 8256cf0 commit a72045b

File tree

9 files changed

+48
-62
lines changed

9 files changed

+48
-62
lines changed

basic_estimators/bernoulli.stan

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
data {
22
int<lower=0> N;
3-
int<lower=0,upper=1> y[N];
3+
int<lower=0, upper=1> y[N];
44
}
55
parameters {
6-
real<lower=0,upper=1> theta;
6+
real<lower=0, upper=1> theta;
77
}
88
model {
9-
theta ~ beta(1,1);
10-
for (n in 1:N)
11-
y[n] ~ bernoulli(theta);
9+
theta ~ beta(1, 1);
10+
y ~ bernoulli(theta);
1211
}

basic_estimators/negative_binomial.stan

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ parameters {
77
real<lower=0> beta;
88
}
99
model {
10-
alpha ~ cauchy(0,10);
11-
beta ~ cauchy(0,10);
12-
for (i in 1:N)
13-
y[i] ~ neg_binomial(alpha, beta);
10+
alpha ~ cauchy(0, 10);
11+
beta ~ cauchy(0, 10);
12+
y ~ neg_binomial(alpha, beta);
1413
}

basic_estimators/negative_binomial2.stan

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ data {
44
}
55
parameters {
66
real<lower=0> alpha;
7-
real<lower=0,upper=1> p_success;
7+
real<lower=0, upper=1> p_success;
88
}
99
transformed parameters {
10-
real<lower=0.0> beta;
11-
beta <- p_success / (1.0 - p_success);
10+
real<lower=0.0> beta = p_success / (1.0 - p_success);
1211
}
1312
model {
14-
for (i in 1:N)
15-
y[i] ~ neg_binomial(alpha, beta);
13+
y ~ neg_binomial(alpha, beta);
1614
}

basic_estimators/normal_censored.stan

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ parameters {
1010
model {
1111
for (n in 1:N_observed)
1212
y[n] ~ normal(mu,1.0) T[,U];
13-
increment_log_prob(N_censored
14-
* log1m(normal_cdf(U,mu,1.0)));
13+
target += N_censored * normal_lccdf(U | mu, 1);
1514
}
1615

1716

basic_estimators/normal_loc.stan

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
transformed data {
2-
real y[5];
3-
y[1] <- 2.0;
4-
y[2] <- 1.0;
5-
y[3] <- -0.5;
6-
y[4] <- 3.0;
7-
y[5] <- 0.25;
2+
vector[5] y = [2, 1, -0.5, 3, 0.25]';
83
}
94
parameters {
10-
real mu;
5+
real mu;
116
}
127
model {
13-
for (n in 1:5)
14-
y[n] ~ normal(mu,1.0);
15-
}
8+
y ~ normal(mu, 1);
9+
}

basic_estimators/normal_mixture.stan

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33

44
data {
55
int<lower=0> N;
6-
real y[N];
6+
vector[N] y;
77
}
88
parameters {
99
real<lower=0,upper=1> theta;
10-
real mu[2];
10+
vector[2] mu;
1111
}
1212
model {
1313
theta ~ uniform(0,1); // equivalently, ~ beta(1,1);
14-
for (k in 1:2)
15-
mu[k] ~ normal(0,10);
16-
for (n in 1:N)
17-
target += log_mix(theta, normal_lpdf(y[n]|mu[1],1.0), normal_lpdf(y[n]|mu[2],1.0));
14+
mu ~ normal(0,10);
15+
for (n in 1:N) {
16+
target += log_mix(theta,
17+
normal_lupdf(y[n] | mu[1], 1),
18+
normal_lupdf(y[n] | mu[2], 1));
19+
}
1820
}
1921

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
data {
22
int<lower=1> K;
33
int<lower=1> N;
4-
real y[N];
4+
vector[N] y;
55
}
66
parameters {
77
simplex[K] theta;
8-
real mu[K];
9-
real<lower=0,upper=10> sigma[K];
8+
vector[K] mu;
9+
vector<lower=0, upper=10>[K] sigma;
1010
}
1111
model {
12-
real ps[K];
12+
vector[K] ps[N];
1313
mu ~ normal(0,10);
1414
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-
increment_log_prob(log_sum_exp(ps));
15+
for (k in 1:K) {
16+
ps[n][k] = normal_lupdf(y[n] | mu[k], sigma[k]);
17+
}
1918
}
19+
target += log_mix(theta, ps);
2020
}
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
data {
22
int<lower=1> K;
33
int<lower=1> N;
4-
real y[N];
4+
vector[N] y;
55
}
66
parameters {
77
simplex[K] theta;
88
simplex[K] mu_prop;
99
real mu_loc;
1010
real<lower=0> mu_scale;
11-
real<lower=0> sigma[K];
11+
vector<lower=0>[K] sigma;
1212
}
1313
transformed parameters {
14-
ordered[K] mu;
15-
mu <- mu_loc + mu_scale * cumulative_sum(mu_prop);
14+
ordered[K] mu = mu_loc + mu_scale * cumulative_sum(mu_prop);
1615
}
1716
model {
17+
vector[K] ps[N];
18+
1819
// prior
19-
mu_loc ~ cauchy(0,5);
20-
mu_scale ~ cauchy(0,5);
21-
sigma ~ cauchy(0,5);
20+
mu_loc ~ cauchy(0, 5);
21+
mu_scale ~ cauchy(0, 5);
22+
sigma ~ cauchy(0, 5);
2223

2324
// likelihood
24-
{
25-
real ps[K];
26-
vector[K] log_theta;
27-
log_theta <- log(theta);
28-
29-
for (n in 1:N) {
30-
for (k in 1:K) {
31-
ps[k] <- log_theta[k]
32-
+ normal_log(y[n],mu[k],sigma[k]);
33-
}
34-
increment_log_prob(log_sum_exp(ps));
35-
}
25+
for (n in 1:N) {
26+
for (k in 1:K) {
27+
ps[n][k] = normal_lupdf(y[n] | mu[k], sigma[k]);
28+
}
3629
}
30+
31+
target += log_mix(theta, ps);
3732
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
data {
22
real U;
33
int<lower=1> N;
4-
real<upper=U> y[N];
4+
vector<upper=U>[N] y;
55
}
66
parameters {
77
real mu;
8-
real<lower=0,upper=2> sigma;
8+
real<lower=0, upper=2> sigma;
99
}
1010
model {
1111
for (n in 1:N)
12-
y[n] ~ normal(mu,sigma) T[,U];
12+
y[n] ~ normal(mu, sigma) T[,U];
1313
}

0 commit comments

Comments
 (0)