top of page

From: Bayesian Models for Astrophysical Data, Cambridge Univ. Press

(c) 2017,  Joseph M. Hilbe, Rafael S. de Souza and Emille E. O. Ishida

you are kindly asked to include the complete citation if you used this material in a publication

â€‹

â€‹

Code 4.7 Multivariate normal linear model in Python using Stan

==================================================

import numpy as np
import statsmodels.api as sm
import pystan

from scipy.stats import uniform, norm

â€‹

# Data
np.random.seed(1056)                                       # set seed to replicate example
nobs= 5000                                                        # number of obs in model

x1 = uniform.rvs(size=nobs)                             # random uniform variable
x2 = uniform.rvs(size=nobs)                             # second explanatory

X = np.column_stack((x1,x2))                          # create response matrix

beta = [2.0, 3.0, -2.5]                                        # create vector of parameters

xb = np.dot(X, beta)                                         # linear predictor, xb

y = np.random.normal(loc=xb, scale=1.0, size=nobs)                      # create y as adjusted
# random normal variate

# Fit
toy_data = {}                                                    # build data dictionary
toy_data['nobs'] = nobs                                     # sample size
toy_data['x'] = X                                               # explanatory variable
toy_data['y'] = y                                                # response variable
toy_data['k'] = toy_data['x'].shape[1]               # number of explanatory variables

â€‹

# Stan code
stan_code = """
data {
int<lower=1> k;
int<lower=0> nobs;
matrix[nobs, k] x;
vector[nobs] y;
}
parameters {
matrix[k,1] beta;
real<lower=0> sigma;
}
transformed parameters{
matrix[nobs,1] mu;
vector[nobs] mu2;

mu = x * beta;
mu2 = to_vector(mu);                          # normal distribution
# does not take matrices as input
}
model {
for (i in 1:k){                                        # Diffuse normal priors for predictors
beta[i] ~ normal(0.0, 100);
}

sigma ~ uniform(0, 100);                      # Uniform prior for standard deviation

y ~ normal(mu2, sigma);                      # Likelihood function
}
"""

â€‹

# Run mcmc
fit = pystan.stan(model_code=stan_code, data=toy_data, iter=5000, chains=3,
n_jobs=3, verbose=False)

â€‹

# Output
nlines = 9                                                  # number of lines in screen output

â€‹

output = str(fit).split('\n')
for item in output[:nlines]:
print(item)

==================================================

Output on screen:

â€‹

Inference for Stan model: anon_model_825ea1806fb73116fc9ba0e7ef76d885.
3 chains, each with iter=5000; warmup=2500; thin=1;
post-warmup draws per chain=2500, total post-warmup draws=7500.

â€‹

mean        se_mean         sd        2.5%         25%         50%         75%       97.5%      n_eff       Rhat
beta[0,0]      2.01           6.3e-4      0.04         1.94         1.99          2.01         2.04          2.09        3319        1.0
beta[1,0]      2.98           7.9e-4      0.05         2.88         2.94          2.98         3.01          3.07        3651        1.0
beta[2,0]     -2.49           7.3e-4      0.05       -2.58        -2.52         -2.49       -2.45         -2.39        4201        1.0
sigma           0.99           1.4e-4   9.9e-3        0.97          0.99          0.99        1.00          1.01         5021        1.0

bottom of page