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 10.19 Bernoulli logit model, in Python using Stan, for assessing the relationship between Seyfert AGN activity and galactocentric distance
==================================================================================
import numpy as np
import pandas as pd
import pystan
import statsmodels.api as sm
​
# Data
path_to_data = 'https://raw.githubusercontent.com/astrobayes/BMAD/master/data/Section_10p8/Seyfert.csv'
​
# read data
data_frame = dict(pd.read_csv(path_to_data))
​
x1 = data_frame['logM200']
x2 = data_frame['r_r200']
​
data = {}
data['Y'] = data_frame['bpt']
data['X'] = sm.add_constant(np.column_stack((x1,x2)))
data['K'] = data['X'].shape[1]
data['N'] = data['X'].shape[0]
data['gal'] = [0 if item == data_frame['zoo'][0] else 1
for item in data_frame['zoo']]
data['P'] = 2
# Fit
stan_code="""
data{
int<lower=0> N; # number of data points
int<lower=0> K; # number of coefficients
int<lower=0> P; # number of populations
matrix[N,K] X; # [logM200, galactocentric distance]
int<lower=0, upper=1> Y[N]; # Seyfert 1/AGN 0
int<lower=0, upper=1> gal[N]; # elliptical 0/spiral 1
}
parameters{
matrix[K,P] beta;
real<lower=0> sigma;
real mu;
}
model{
vector[N] pi;
for (i in 1:N) {
if (gal[i] == gal[1]) pi[i] = dot_product(col(beta,1),X[i]);
else pi[i] = dot_product(col(beta,2), X[i]);
}
# shared hyperpriors
sigma ~ gamma(0.001, 0.001);
mu ~ normal(0, 100);
​
# priors and likelihood
for (i in 1:K) {
for (j in 1:P) beta[i,j] ~ normal(mu, sigma);
}
​
Y ~ bernoulli_logit(pi);
}
"""
​
# Run mcmc
fit = pystan.stan(model_code=stan_code, data=data, iter=60000, chains=3,
warmup=30000, thin=10, n_jobs=3)
​
# Output
print(fit)
==================================================================================
Output on screen:
​
Inference for Stan model: anon_model_540051ae737bb985bed8ad3b87a52b84.
3 chains, each with iter=60000; warmup=30000; thin=10;
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,0] 0.04 1.6e-3 0.08 -0.11 -0.02 0.03 0.09 0.22 2491 1.0
beta[1,0] -0.15 3.8e-3 0.1 -0.35 -0.22 -0.15 -0.07 0.02 650 1.0
beta[2,0] 0.17 5.5e-3 0.12 -0.03 0.08 0.17 0.26 0.42 467 1.0
beta[0,1] 3.1e-4 8.3e-4 0.05 -0.1 -0.03 5.3e-5 0.03 0.1 3509 1.0
beta[1,1] -0.02 7.6e-4 0.05 -0.12 -0.05 -0.02 0.01 0.07 4246 1.0
beta[2,1] 4.9e-3 7.0e-4 0.05 -0.09 -0.03 4.4e-3 0.04 0.11 5304 1.0
sigma 0.14 3.8e-3 0.09 0.02 0.08 0.13 0.18 0.37 604 1.0
mu 7.0e-3 8.1e-4 0.08 -0.14 -0.03 4.9e-3 0.05 0.17 8934 1.0
lp__ -1193 0.19 2.91 -1199 -1195 -1193 -1192 -1186 247 1.01
​
Samples were drawn using NUTS at Thu May 4 14:55:41 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).