Skip to content

Commit 1e6c5eb

Browse files
committed
checkpointing
1 parent 028bd8a commit 1e6c5eb

File tree

9 files changed

+746
-321
lines changed

9 files changed

+746
-321
lines changed
281 KB
Loading

jupyter/radon/radon.ipynb

Lines changed: 611 additions & 315 deletions
Large diffs are not rendered by default.

jupyter/radon/stan/normal_glm.stan

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* normal linear regression model */
2+
data {
3+
int<lower=0> N; // number of observations
4+
int<lower=1> M; // number of predictors
5+
matrix[N, M] xs;
6+
vector[N] y;
7+
}
8+
parameters {
9+
real alpha;
10+
vector beta[M];
11+
real<lower=0> sigma;
12+
}
13+
model {
14+
y ~ normal_glm_id(xs, alpha, beta, sigma);
15+
sigma ~ normal(0, 10); // override default uniform(-inf, +inf)
16+
}

jupyter/radon/stan/radon_complete_pool.stan renamed to jupyter/radon/stan/radon_cp.stan

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ parameters {
99
real<lower=0> sigma;
1010
}
1111
model {
12-
sigma ~ normal(0, 5);
1312
y ~ normal(alpha + beta * x, sigma);
13+
alpha ~ normal(0, 10);
14+
beta ~ normal(0, 10);
15+
sigma ~ normal(0, 10);
16+
}
17+
generated quantities {
18+
array[N] real y_rep = normal_rng(alpha + beta * x, sigma);
1419
}
15-
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ data {
22
int<lower=1> N; // observations
33
int<lower=1> J; // counties
44
array[N] int<lower=1, upper=J> county;
5-
vector[N] x;
6-
vector[N] y;
5+
vector[N] x; // floor
6+
vector[N] y; // radon
77
}
88
parameters {
9-
real beta;
109
vector[J] alpha;
10+
real beta;
1111
real<lower=0> sigma;
1212
}
1313
model {
14-
sigma ~ normal(0, 5);
1514
y ~ normal(alpha[county] + beta * x, sigma);
15+
alpha ~ normal(0, 10);
16+
beta ~ normal(0, 10);
17+
sigma ~ normal(0, 10);
18+
}
19+
generated quantities {
20+
array[N] real y_rep = normal_rng(alpha[county] + beta * x, sigma);
1621
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
data {
2+
int<lower=1> N; // observations
3+
int<lower=1> J; // counties
4+
array[N] int<lower=1, upper=J> county;
5+
vector[N] y; // radon
6+
vector[N] x; // floor
7+
vector[N] u; // uranium
8+
}
9+
parameters {
10+
vector[J] alpha;
11+
vector[2] beta;
12+
real<lower=0> sigma;
13+
}
14+
model {
15+
y ~ normal(alpha[county] + beta[1] * x + beta[2] * u, sigma);
16+
alpha ~ normal(0, 10);
17+
beta ~ normal(0, 10);
18+
sigma ~ normal(0, 10);
19+
}
20+
generated quantities {
21+
array[N] real y_rep = normal_rng(alpha[county] + beta[1] * x + beta[2] * u, sigma);
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data {
2+
int<lower=1> N; // observations
3+
int<lower=1> J; // counties
4+
array[N] int<lower=1, upper=J> county;
5+
vector[N] y; // radon
6+
vector[N] x; // floor
7+
vector[N] u; // uranium
8+
}
9+
transformed data {
10+
matrix[N, 2] xs = [x', u']';
11+
}
12+
parameters {
13+
vector[J] alpha;
14+
vector[2] beta;
15+
real<lower=0> sigma;
16+
}
17+
transformed parameters {
18+
vector[N] alphas = alpha[county];
19+
}
20+
model {
21+
y ~ normal_id_glm(xs, alphas, beta, sigma);
22+
alpha ~ normal(0, 10);
23+
beta ~ normal(0, 10);
24+
sigma ~ normal(0, 10);
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data {
2+
int<lower=1> N; // observations
3+
int<lower=1> J; // counties
4+
array[N] int<lower=1, upper=J> county;
5+
vector[N] x;
6+
vector[N] y;
7+
}
8+
parameters {
9+
real mu_alpha;
10+
real<lower=0> sigma_alpha;
11+
vector<offset=mu_alpha, multiplier=sigma_alpha>[J] alpha; // non-centered parameterization
12+
real beta;
13+
real<lower=0> sigma;
14+
}
15+
model {
16+
y ~ normal(alpha[county] + beta * x, sigma);
17+
alpha ~ normal(mu_alpha, sigma_alpha); // partial-pooling
18+
beta ~ normal(0, 10);
19+
sigma ~ normal(0, 10);
20+
mu_alpha ~ normal(0, 10);
21+
sigma_alpha ~ normal(0, 10);
22+
}
23+
generated quantities {
24+
array[N] real y_rep = normal_rng(alpha[county] + beta * x, sigma);
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
data {
2+
int<lower=1> N; // observations
3+
int<lower=1> J; // counties
4+
array[N] int<lower=1, upper=J> county;
5+
vector[N] y; // radon
6+
vector[N] x; // floor
7+
vector[N] u; // uranium
8+
}
9+
transformed data {
10+
matrix[N, 2] xs = [x', u']';
11+
}
12+
parameters {
13+
real mu_alpha;
14+
real<lower=0> sigma_alpha;
15+
vector<offset=mu_alpha, multiplier=sigma_alpha>[J] alpha; // non-centered parameterization
16+
vector[2] beta;
17+
real<lower=0> sigma;
18+
}
19+
transformed parameters {
20+
vector[N] alphas = alpha[county];
21+
}
22+
model {
23+
y ~ normal_id_glm(xs, alphas, beta, sigma);
24+
alpha ~ normal(mu_alpha, sigma_alpha); // partial-pooling
25+
beta ~ normal(0, 10);
26+
sigma ~ normal(0, 10);
27+
mu_alpha ~ normal(0, 10);
28+
sigma_alpha ~ normal(0, 10);
29+
}
30+
generated quantities {
31+
array[N] real y_rep = normal_rng(alpha[county] + beta * xs, sigma);
32+
}

0 commit comments

Comments
 (0)