HSI
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.3 Normal linear model in Python using Stan
==================================================
import numpy as np
import statsmodels.api as sm
import pystan
from scipy.stats import uniform
​
# 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
x1.transpose() # create response matrix
X = sm.add_constant(x1) # add intercept
beta = [2.0, 3.0] # create vector of parameters
xb = np.dot(X, beta) # linear predictor
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'] = x1 # explanatory variable
toy_data['y'] = y # response variable
​
# STAN code
stan_code = """
data {
int<lower=0> nobs;
vector[nobs] x;
vector[nobs] y;
}
parameters {
real beta0;
real beta1;
real<lower=0> sigma;
}
model {
vector[nobs] mu;
mu = beta0 + beta1 * x;
y ~ normal(mu, sigma); # Likelihood function
}
"""
​
fit = pystan.stan(model_code=stan_code, data=toy_data, iter=5000, chains=3, verbose=False, n_jobs=3)
​
# Output
nlines = 8 # 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_2fd44c911bfef7c6ae1d9a3b6094940d.
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
beta0 1.99 4.8e-4 0.03 1.93 1.97 1.99 2.01 2.04 3100 1.0
beta1 3.00 8.4e-4 0.05 2.90 2.97 3.00 3.03 3.09 3067 1.0
sigma 0.99 1.5e-4 9.4e-3 0.97 0.98 0.99 1.00 1.01 4105 1.0
Code 4.4 Plotting posteriors from Code 4.3
​
import pylab as plt
​
# Plot posterior and chain
fit.plot(['beta0', 'beta1', 'sigma'])
plt.tight_layout()
plt.show()
​