Skip to content

Commit c5d0641

Browse files
Stan code is now in separate files; updated some deprecated Stan functions
1 parent e5b7d9e commit c5d0641

File tree

12 files changed

+254
-243
lines changed

12 files changed

+254
-243
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// #### Notes to Stan model #######################################################
2+
// ## Implementation of this model can be difficult to understand for beginners.
3+
// ## Therefore I suggest either not trying to understand it and look on WinBUGS
4+
// ## version or go deep into Stan manual.
5+
// ################################################################################
6+
7+
// ChaSaSoon Censored Data
8+
data {
9+
int<lower=0> n_fails;
10+
int<lower=0> n;
11+
int<lower=0> z_observed;
12+
}
13+
14+
parameters {
15+
real<lower=0.25, upper=1> theta; // Uniform Prior on Rate Theta
16+
}
17+
18+
model {
19+
// Observed Data
20+
z_observed ~ binomial(n, theta);
21+
22+
// Unobserved Data
23+
target += n_fails * log(binomial_cdf(25 | n, theta)
24+
- binomial_cdf(14 | n, theta));
25+
}

Bayesian_Cognitive_Modeling/ParameterEstimation/DataAnalysis/ChaSaSoon_Stan.R

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22
rm(list=ls())
33

44
library(rstan)
5-
6-
#### Notes to Stan model #######################################################
7-
## Implementation of this model can be difficult to understand for beginners.
8-
## Therefore I suggest either not trying to understand it and look on WinBUGS
9-
## version or go deep into Stan manual.
10-
################################################################################
11-
model <- "
12-
# ChaSaSoon Censored Data
13-
data {
14-
int<lower=0> nfails;
15-
int<lower=0> n;
16-
int<lower=0> z_observed;
17-
}
18-
parameters {
19-
real<lower=.25,upper=1> theta; // Uniform Prior on Rate Theta
20-
}
21-
model {
22-
// Observed Data
23-
z_observed ~ binomial(n, theta);
24-
25-
// Unobserved Data
26-
increment_log_prob(nfails * log(binomial_cdf(25, n, theta)
27-
- binomial_cdf(14, n, theta)));
28-
}"
295

306
nfails <- 949
317
n <- 50 # Number of questions
@@ -40,7 +16,7 @@ parameters <- c("theta")
4016

4117
# The following command calls Stan with specific options.
4218
# For a detailed description type "?rstan".
43-
samples <- stan(model_code=model,
19+
samples <- stan(file="ChaSaSoon.stan",
4420
data=data,
4521
init=myinits, # If not specified, gives random inits
4622
pars=parameters,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// #### Notes to Stan model #######################################################
2+
// ## The model is not very effective.
3+
// ## 1) Don't change seed or lower iterations. This model converges slowly so if
4+
// ## you change the values, you'll need to increment iterations significantly
5+
// ## 2) Code is quite dissimilar to original WinBUGS model - using conditionals
6+
// ## instead of step function. This will happen in further models more often.
7+
// ## There is a difference in what functions are efficient in BUGS and Stan.
8+
// ################################################################################
9+
10+
// Change Detection
11+
data {
12+
int n;
13+
array[n] int t;
14+
array[n] real c;
15+
}
16+
17+
parameters {
18+
array[2] real mu;
19+
real<lower=0> lambda;
20+
real<lower=0, upper=n> tau;
21+
}
22+
23+
transformed parameters {
24+
real<lower=0> sigma;
25+
26+
sigma = inv_sqrt(lambda);
27+
}
28+
model {
29+
// Group Means
30+
mu ~ normal(0, sqrt(1000));
31+
// Common Precision
32+
lambda ~ gamma(0.001, 0.001);
33+
34+
// Which Side is Time of Change Point?
35+
// Data Come From A Gaussian
36+
for (i in 1:n) {
37+
if ((t[i] - tau) < 0.0)
38+
c[i] ~ normal(mu[1], sigma);
39+
else
40+
c[i] ~ normal(mu[2], sigma);
41+
}
42+
}

Bayesian_Cognitive_Modeling/ParameterEstimation/DataAnalysis/ChangeDetection_Stan.R

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,6 @@ rm(list=ls())
33

44
library(rstan)
55

6-
#### Notes to Stan model #######################################################
7-
## The model is not very effective.
8-
## 1) Don't change seed or lower iterations. This model converges slowly so if
9-
## you change the values, you'll need to increment iterations significantly
10-
## 2) Code is quite dissimilar to original WinBUGS model - using conditionals
11-
## instead of step function. This will happen in further models more often.
12-
## There is a difference in what functions are efficient in BUGS and Stan.
13-
################################################################################
14-
model <- "
15-
// Change Detection
16-
data {
17-
int n;
18-
vector[n] t;
19-
vector[n] c;
20-
}
21-
parameters {
22-
vector[2] mu;
23-
real<lower=0> lambda;
24-
real<lower=0,upper=n> tau;
25-
}
26-
transformed parameters {
27-
real<lower=0> sigma;
28-
29-
sigma <- inv_sqrt(lambda);
30-
}
31-
model {
32-
// Group Means
33-
mu ~ normal(0, inv_sqrt(.001));
34-
// Common Precision
35-
lambda ~ gamma(.001, .001);
36-
37-
// Which Side is Time of Change Point?
38-
// Data Come From A Gaussian
39-
for (i in 1:n) {
40-
if ((t[i] - tau) < 0.0)
41-
c[i] ~ normal(mu[1], sigma);
42-
else
43-
c[i] ~ normal(mu[2], sigma);
44-
}
45-
}"
46-
476
c <- scan("changepointdata.txt")
487
n <- length(c)
498
t <- 1:n
@@ -57,7 +16,7 @@ parameters <- c("mu", "sigma", "tau")
5716

5817
# The following command calls Stan with specific options.
5918
# For a detailed description type "?rstan".
60-
samples <- stan(model_code=model,
19+
samples <- stan(file="ChangeDetection.stan",
6120
data=data,
6221
init=myinits, # If not specified, gives random inits
6322
pars=parameters,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// #### Notes to Stan model #######################################################
2+
// ## 1) Multivariate normal distribution in Stan uses covariance matrix instead of
3+
// ## precision matrix.
4+
// ## 2) Multivariate normal distribution can be (and is) also vectorized.
5+
// ## 3) Warnings may occur during sampling, ignore them.
6+
// ################################################################################
7+
8+
// Pearson Correlation
9+
data {
10+
int<lower=0> n;
11+
array[n] vector[2] x;
12+
}
13+
14+
parameters {
15+
vector[2] mu;
16+
vector<lower=0>[2] lambda;
17+
real<lower=-1, upper=1> r;
18+
}
19+
20+
transformed parameters {
21+
vector<lower=0>[2] sigma;
22+
cov_matrix[2] T;
23+
24+
// Reparameterization
25+
sigma[1] = inv_sqrt(lambda[1]);
26+
sigma[2] = inv_sqrt(lambda[2]);
27+
28+
T[1,1] = square(sigma[1]);
29+
T[1,2] = r * sigma[1] * sigma[2];
30+
T[2,1] = r * sigma[1] * sigma[2];
31+
T[2,2] = square(sigma[2]);
32+
}
33+
34+
model {
35+
// Priors
36+
mu ~ normal(0, sqrt(1000));
37+
lambda ~ gamma(0.001, 0.001);
38+
39+
// Data
40+
x ~ multi_normal(mu, T);
41+
}

Bayesian_Cognitive_Modeling/ParameterEstimation/DataAnalysis/Correlation_1_Stan.R

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,6 @@ rm(list=ls())
33

44
library(rstan)
55

6-
#### Notes to Stan model #######################################################
7-
## 1) Multivariate normal distribution in Stan uses covariance matrix instead of
8-
## precision matrix.
9-
## 2) Multivariate normal distribution can be (and is) also vectorized.
10-
## 3) Warnings may occur during sampling, ignore them.
11-
################################################################################
12-
model <- "
13-
// Pearson Correlation
14-
data {
15-
int<lower=0> n;
16-
vector[2] x[n];
17-
}
18-
parameters {
19-
vector[2] mu;
20-
vector<lower=0>[2] lambda;
21-
real<lower=-1,upper=1> r;
22-
}
23-
transformed parameters {
24-
vector<lower=0>[2] sigma;
25-
cov_matrix[2] T;
26-
27-
// Reparameterization
28-
sigma[1] <- inv_sqrt(lambda[1]);
29-
sigma[2] <- inv_sqrt(lambda[2]);
30-
T[1,1] <- square(sigma[1]);
31-
T[1,2] <- r * sigma[1] * sigma[2];
32-
T[2,1] <- r * sigma[1] * sigma[2];
33-
T[2,2] <- square(sigma[2]);
34-
}
35-
model {
36-
// Priors
37-
mu ~ normal(0, inv_sqrt(.001));
38-
lambda ~ gamma(.001, .001);
39-
40-
// Data
41-
x ~ multi_normal(mu, T);
42-
}"
43-
446
# Choose a dataset:
457
dataset <- 1
468

@@ -94,7 +56,7 @@ parameters <- c("r", "mu", "sigma")
9456

9557
# The following command calls Stan with specific options.
9658
# For a detailed description type "?rstan".
97-
samples <- stan(model_code=model,
59+
samples <- stan(file="Correlation_1.stan",
9860
data=data,
9961
init=myinits, # If not specified, gives random inits
10062
pars=parameters,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// #### Notes to Stan model #######################################################
2+
// ## 1) All notes from previous model Correlation_1 also apply to this model.
3+
// ## 2) If you change sigmaerror to c(0.03, 10) as suggested in excercise 5.2.2
4+
// ## warning statements will be more frequent and posterior less smooth.
5+
// ################################################################################
6+
7+
// Pearson Correlation With Uncertainty in Measurement
8+
data {
9+
int<lower=0> n;
10+
array[n] vector[2] x;
11+
vector[2] sigmaerror;
12+
}
13+
14+
parameters {
15+
vector[2] mu;
16+
vector<lower=0>[2] lambda;
17+
real<lower=-1, upper=1> r;
18+
array[n] vector[2] y;
19+
}
20+
21+
transformed parameters {
22+
vector<lower=0>[2] sigma;
23+
cov_matrix[2] T;
24+
25+
// Reparameterization
26+
sigma[1] = inv_sqrt(lambda[1]);
27+
sigma[2] = inv_sqrt(lambda[2]);
28+
29+
T[1,1] = square(sigma[1]);
30+
T[1,2] = r * sigma[1] * sigma[2];
31+
T[2,1] = r * sigma[1] * sigma[2];
32+
T[2,2] = square(sigma[2]);
33+
}
34+
35+
model {
36+
// Priors
37+
mu ~ normal(0, sqrt(1000));
38+
lambda ~ gamma(0.001, 0.001);
39+
40+
// Data
41+
y ~ multi_normal(mu, T);
42+
for (i in 1:n)
43+
x[i] ~ normal(y[i], sigmaerror);
44+
}

Bayesian_Cognitive_Modeling/ParameterEstimation/DataAnalysis/Correlation_2_Stan.R

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,6 @@ rm(list=ls())
33

44
library(rstan)
55

6-
#### Notes to Stan model #######################################################
7-
## 1) All notes from previous model Correlation_1 also apply to this model.
8-
## 2) If you change sigmaerror to c(0.03, 10) as suggested in excercise 5.2.2
9-
## warning statements will be more frequent and posterior less smooth.
10-
################################################################################
11-
model <- "
12-
// Pearson Correlation With Uncertainty in Measurement
13-
data {
14-
int<lower=0> n;
15-
vector[2] x[n];
16-
vector[2] sigmaerror;
17-
}
18-
parameters {
19-
vector[2] mu;
20-
vector<lower=0>[2] lambda;
21-
real<lower=-1,upper=1> r;
22-
vector[2] y[n];
23-
}
24-
transformed parameters {
25-
vector<lower=0>[2] sigma;
26-
cov_matrix[2] T;
27-
28-
// Reparameterization
29-
sigma[1] <- inv_sqrt(lambda[1]);
30-
sigma[2] <- inv_sqrt(lambda[2]);
31-
32-
T[1,1] <- square(sigma[1]);
33-
T[1,2] <- r * sigma[1] * sigma[2];
34-
T[2,1] <- r * sigma[1] * sigma[2];
35-
T[2,2] <- square(sigma[2]);
36-
}
37-
model {
38-
// Priors
39-
mu ~ normal(0, inv_sqrt(.001));
40-
lambda ~ gamma(.001, .001);
41-
42-
// Data
43-
y ~ multi_normal(mu, T);
44-
for (i in 1:n)
45-
x[i] ~ normal(y[i], sigmaerror);
46-
}"
47-
486
x <- matrix(c( .8, 102,
497
1.0, 98,
508
.5, 100,
@@ -72,7 +30,7 @@ parameters <- c("r", "mu", "sigma")
7230

7331
# The following command calls Stan with specific options.
7432
# For a detailed description type "?rstan".
75-
samples <- stan(model_code=model,
33+
samples <- stan(file="Correlation_2.stan",
7634
data=data,
7735
init=myinits, # If not specified, gives random inits
7836
pars=parameters,

0 commit comments

Comments
 (0)