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 10.13 Bernoulli model in Python using Stan, for assessing the relationship between bulge size and the fraction of red spirals

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

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

â€‹

â€‹

x = np.array(data_frame['fracdeV'])

â€‹

# prepare data for Stan
data = {}
data['Y'] = np.array(data_frame['type'])
data['nobs'] = data['X'].shape[0]
data['K'] = data['X'].shape[1]

â€‹

# Fit
stan_code="""
data{
int<lower=0> nobs;                                 # number of data points
int<lower=0> K;                                      # number of coefficients
matrix[nobs, K] X;                                   # bulge size
int Y[nobs];                                              # galaxy type: 1 - red, 0 - blue
}
parameters{
vector[K] beta;                                         # linear predictor coefficients
}
model{
# priors and likelihood
for (i in 1:K) beta[i] ~ normal(0, 100);

â€‹

Y ~ bernoulli_logit(X * beta);
}
"""

â€‹

# Run mcmc
fit = pystan.stan(model_code=stan_code, data=data, iter=6000, chains=3,
warmup=3000, thin=1, n_jobs=3)

â€‹

# Output
print(fit)

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

Output on screen:

â€‹

3 chains, each with iter=6000; warmup=3000; thin=1;
post-warmup draws per chain=3000, total post-warmup draws=9000.

â€‹

mean     se_mean         sd       2.5%       25%      50%       75%     97.5%       n_eff       Rhat
beta[0]      -4.91         3.3e-3      0.16      -5.24      -5.02      -4.91      -4.81        -4.6        2318         1.0
beta[1]       8.17         9.6e-3      0.46       7.26        7.86       8.17       8.47         9.07       2282         1.0
lp__        -954.4            0.02      0.95    -957.0    -954.8    -954.1    -953.7      -953.5       2953        1.0

â€‹

Samples were drawn using NUTS at Wed May  3 19:10:45 2017.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).

bottom of page