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.25 Negative binomial model (AR1) in Python using Stan, for assessing the evolution of the number of sunspots through the years.

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

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_10p10/sunspot.csv"

# read data
data_frame = dict(pd.read_csv(path_to_data))

# prepare data for Stan
data = {}
data['Y'] = [int(round(item)) for item in data_frame['nspots']]
data['nobs'] = len(data['Y'])
data['K'] = 2

# Fit
stan_code="""
data{
    int<lower=0> nobs;                # number of data points
    int<lower=0> K;                    # number of coefficients
    int Y[nobs];                            # nuber of sunspots
}
parameters{
    vector[K] phi;                         # linear predictor coefficients
    real<lower=0> theta;              # noise parameter
}
model{
    vector[nobs] mu;
    
    mu[1] = Y[1];                         # set initial value

    for (t in 2:nobs) mu[t] = exp(phi[1] + phi[2] * Y[t - 1]);

    # priors and likelihood
    theta ~ gamma(0.001, 0.001);
    for (i in 1:K) phi[i] ~ normal(0, 100);

    Y ~ neg_binomial_2(mu, theta);
}
"""

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

# Output
print(fit)

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

Output on screen:

Inference for Stan model: anon_model_127abffb5ead7c28ca68816154997b15.
3 chains, each with iter=7500; warmup=5000; 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
phi[0]         3.33         1.1e-3        0.06        3.22         3.29       3.33        3.38          3.46       3178        1.0
phi[1]         0.01         1.0e-5     6.3e-4     9.4e-3         0.01       0.01        0.01          0.01       3742       nan
theta           2.55         3.6e-3        0.21        2.15           2.4       2.54        2.68          2.97       3363        1.0
lp__         9.2e4             0.02        1.16      9.2e4       9.2e4      9.2e4      9.2e4         9.2e4       2836        1.0

Samples were drawn using NUTS at Thu May  4 17:06:28 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).