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.8 Log-gamma model in Python using Stan

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

import numpy as np
import pystan

from scipy.stats import uniform, gamma

​

# Data
np.random.seed(33559)                                    # set seed to replicate example


nobs = 3000                                                      # number of obs in model 
x1 = uniform.rvs(size=nobs)                            # random uniform variable
x2 = uniform.rvs(size=nobs)                            # second explanatory

​

beta0 = 1.0                                                        # intercept
beta1 = 0.66                                                      # first linear coefficient
beta2 = -1.25                                                    # second linear coefficient

​

eta = beta0 + beta1 * x1 + beta2 * x2              # linear predictor


mu = np.exp(eta)
y = gamma.rvs(mu)                                         # create y as adjusted

​

# Fit
mydata = {}                                                     # build data dictionary
mydata['N'] = nobs                                          # sample size
mydata['x1'] = x1                                            # explanatory variable         
mydata['x2'] = x2
mydata['y'] = y                                                # response variable

​

# STAN code
stan_gamma = """
data{
    int<lower=0> N;
    vector[N] x1;
    vector[N] x2;
    vector[N] y;
}
parameters{
    real beta0;
    real beta1;
    real beta2;
    real<lower=0> r;
}
transformed parameters{
    vector[N] eta;
    vector[N] mu;
    vector[N] lambda;

​

    for (i in 1:N){
        eta[i] = beta0 + beta1 * x1[i] + beta2 * x2[i];
        mu[i] = exp(eta[i]);
        lambda[i] = r/mu[i];
    }
}
model{
    r ~ gamma(0.01, 0.01);
    for (i in 1:N) y[i] ~ gamma(r, lambda[i]);
}
"""

​

# Run mcmc
fit = pystan.stan(model_code=stan_gamma, data=mydata, iter=7000, chains=3,
                           warmup=6000, n_jobs=3)

​

# Output
nlines = 9                                                        # 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_3c824db1250f91b7962c170204aef856.
3 chains, each with iter=7000; warmup=6000; thin=1; 
post-warmup draws per chain=1000, total post-warmup draws=3000.

​

                 mean      se_mean           sd         2.5%         25%          50%          75%        97.5%        n_eff       Rhat
beta0          1.05          9.5e-4        0.03         0.98          1.02          1.05          1.07            1.11      1365.0         1.0
beta1          0.60          1.2e-3        0.05         0.51          0.57          0.60          0.63            0.69      1561.0         1.0
beta2         -1.28          1.1e-3        0.04       -1.37         -1.31        -1.28         -1.25           -1.19      1554.0         1.0
r                 1.83          9.7e-4        0.04         1.74          1.80          1.83          1.86            1.91      1915.0         1.0

 

bottom of page