The Stan Modeling Language

Stan
Hamiltonian Monte Carlo

ModelingLanguage

Automatic
Differentiation

Adaptation

A Stan model is defined by five program blocks

data!
transformed data!
parameters!
transformed parameters!
model!
generated quantities(required)(required)

The “data” block reads external information

data {
int N;
int x[N];int offset;}

The “transformed data” blockallows for preprocessing of the datatransformed data {int y[N];for (n in 1:N)
y[n] <- x[n] - offset;}

The “parameters” block defines the sampling spaceparameters {real<lower=0> lambda1;
real<lower=0> lambda2;}

The “transformed parameters” block allows forparameter processing before the posterior is computed

transformed parameters {real<lower=0> lambda;
lambda <- lambda1 + lambda2;}

In the “model” block we get to define our posterior

model {
y ~ poisson(lambda);
lambda1 ~ cauchy(0, 2.5);
lambda2 ~ cauchy(0, 2.5);}

Lastly, the “generated quantities” blockallows for postprocessing

generated quantities {int x_predict;x_predict <- poisson_rng(lambda)+ offset;}data {
int N;
int x[N];int offset;}!

transformed data {int y[N];for (n in 1:N)
y[n] <- x[n] - offset;}!

parameters {real<lower=0> lambda1;
real<lower=0> lambda2;}!

transformed parameters {real<lower=0> lambda;
lambda <- lambda1 + lambda2;}!

model {
y ~ poisson(lambda);
lambda1 ~ cauchy(0, 2.5);
lambda2 ~ cauchy(0, 2.5);}!

generated quantities {int x_predictx_predict <- poisson_rng(lambda) + offset;
}

data

transformed
transformedparametersdataparametersmodel

generated
quantities

Execution

Per chain

Per chainNA

Per leapfrog

Per leapfrog

Per sample

Variable
Declarations

Yes

Yes

Yes

Yes

Yes

Yes

Variable
Scope

GlobalGlobalGlobalGlobal

Local

Local

Variables
Saved?

No

No

Yes

Yes

No

Yes

ModifyPosterior?

No

No

No

No

Yes

No

RandomVariables

NoNo

No

No

No

Yes

Stan has two primitive types

int is an integer type!

real is a floating point precision type

Both can be bounded

int<lower=1> N;!
real<upper=5> alpha;real<lower=-1,upper=1> beta;!
real gamma;
real<upper=gamma> zeta;

Reals extend to linear algebra types

vector[10] a;// Column vector
matrix[10, 1] b;!
row_vector[10] c; // Row vector
matrix[1, 10] d;

Arrays of int, reals, vectors, and matrices are available

real a[10];!
vector[10] b[10];!
matrix[10, 10] c[10];Stan also implements a variety of constrained types

simplex[5] theta;!
ordered[5] o;positive_ordered[5] p;!
corr_matrix[5] C;
cov_matrix[5] Sigma;// sum(theta) = 1// o[1] < ... < o[5]// Symmetric and// positive-definite

All of your favorite statements are available, too

if/then/else!
for (i in 1:I)!
while (i < I)

There are two ways to modify the posteriory ~ normal(0, 1);!
increment_log_posterior(log_normal(y, 0, 1));

Many sampling statements are vectorizedparameters {real mu[N];real<lower=0> sigma[N];}!
model {!
for (n in 1:N)
y[n] ~ normal(mu[n], sigma[n]);!

!
}Many sampling statements are vectorizedparameters {real mu[N];real<lower=0> sigma[N];}!
model {!
for (n in 1:N)
y[n] ~ normal(mu[n], sigma[n]);!
y ~ normal(mu, sigma);!
}Because of the huge number of possible
configurations, Stan uses hierarchical arguments

Because of the huge number of possible
configurations, Stan uses hierarchical arguments

Interactive example

http://www.mc-stan.org/mlss14/support.data.R
http://www.mc-stan.org/mlss14/support-data.json
http://www.mc-stan.org/mlss14/support-data.pkl

Younger Americans are more likely to support
the statewide legalization of gay marriage

Support For Gay Marriage (k / n)

10.8
0.6
0.40.20102030

40

50 60
Age70

80

90 100

http://www.mc-stan.org/mlss14/support.data.R

The GP models the data well

Support For Gay Marriage (%)

10.8
0.6
0.40.2GP Mean0102030

40

50 60
Age70

80

90 100

http://www.mc-stan.org/mlss14/support.data.R

And the posterior predictive checks indicate consistency~
Support For Gay Marriage (k / n)

10.8
0.6
0.4
0.2
PPD
0
102030

40

50 60
Age70

80

90 100

http://www.mc-stan.org/mlss14/support.data.R

But what about the covariates we’ve ignored?

Support For Gay Marriage (%)

10.8
0.6
0.4
Female0.2

Male0102030

4050 60
Age7080

90 100

http://www.mc-stan.org/mlss14/support.data.R

Support For Gay Marriage (%)

10.8
0.6
0.4
0.2

FemaleMale0102030

40

50 60
Age70

80

90 100

http://www.mc-stan.org/mlss14/support.data.R

~Support For Gay Marriage (k / n)

10.8
0.6
0.4
0.2
0102030

40

50 60
Age70

80

FemaleMale90 100

http://www.mc-stan.org/mlss14/support.data.R