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 5.24 Probit model in Python using Stan

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

import numpy as np
import pystan
import statsmodels.api as sm
from scipy.stats import uniform, norm, bernoulli

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

x1 = uniform.rvs(size=nobs)
x2 = 2 * uniform.rvs(size=nobs)

â€‹

beta0 = 2.0
beta1 = 0.75
beta2 = -1.25

â€‹

xb = beta0 + beta1 * x1 + beta2 * x2
exb = 1 - norm.sf(xb)                                            # inverse probit link
py = bernoulli.rvs(exb)

â€‹

# Data transformation for illustrative purpouses
K = 3                                                                    # number of coefficients
X = np.column_stack((x1, x2))

# Fit
probit_data = {}
probit_data['N'] = nobs
probit_data['K'] = K
probit_data['X'] = X
probit_data['Y'] = py
probit_data['logN'] = np.log(nobs)

â€‹

probit_code = """
data{
int<lower=0> N;
int<lower=0> K;
matrix[N,K] X;
int Y[N];
real logN;
}
parameters{
vector[K] beta;
}
transformed parameters{
vector[N] xb;

xb = X * beta;
}
model{

for (i in 1:N) Y[i] ~ bernoulli(Phi(xb[i]));      # likelihood
}
generated quantities{
real LLi[N];
real AIC;
real BIC;
real LogL;
vector[N] xb2;
real p[N];

xb2 = X * beta;

for (i in 1:N){
p[i] = Phi(xb2[i]);
LLi[i] = Y[i] * log(p[i]) + (1-Y[i]) * log(1 - p[i]);
}

LogL = sum(LLi);
AIC = -2 * LogL + 2 * K;
BIC = -2 * LogL + logN * K;
}
"""

â€‹

fit = pystan.stan(model_code=probit_code, data=probit_data, iter=5000, chains=3,
warmup=3000, n_jobs=3)

â€‹

# Output
lines = list(range(8)) + [2 * nobs + 8, 2 * nobs + 9, 2 * nobs + 10]

â€‹

output = str(fit).split('\n')

for i in lines:
print(output[i])

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

Output on screen:

â€‹

Inference for Stan model: anon_model_9905916d1d6d9be23f09c6fce0de139c.
3 chains, each with iter=5000; warmup=3000; thin=1;
post-warmup draws per chain=2000, total post-warmup draws=6000.

â€‹

mean      se_mean          sd          2.5%         25%         50%         75%       97.5%         n_eff      Rhat
beta[0]       1.92          2.4e-3       0.11           1.71          1.85         1.92           2.0           2.12      1947.0        1.0
beta[1]       0.67          2.3e-3       0.12          0.44           0.59         0.67         0.75           0.92      2816.0        1.0
beta[2]       -1.2           1.4e-3       0.07        -1.33          -1.25          -1.2       -1.16          -1.07      2162.0        1.0
AIC       1642.9              0.05       2.28     1640.3       1641.2      1642.3    1643.9       1648.6      2256.0        1.0
BIC       1659.7              0.05       2.28     1657.1       1658.0       1659.1   1660.7       1665.4      2256.0        1.0
LogL      -818.4             0.02       1.14      -821.3        -818.9       -818.1    -817.6        -817.1      2256.0        1.0

bottom of page