Ejemplo n.º 1
0
def analyze_simu(simu):
    simu_l = simu[0]
    simu_y = simu[1]

    # Fit the simulated observation
    input_data = dict(N=N, y=simu_y)

    fit = fit_model.sampling(data=input_data, seed=4938483, n_jobs=1)

    # Compute diagnostics
    warning_code = stan_utility.check_all_diagnostics(fit, quiet=True)

    # Compute rank of prior draw with respect to thinned posterior draws
    thinned_l = fit.extract()['lambda'][numpy.arange(0, 4000 - 7, 8)]
    sbc_rank = len(list(filter(lambda x: x > simu_l, thinned_l)))

    # Compute posterior sensitivities
    summary = fit.summary(probs=[0.5])
    post_mean_l = [x[0] for x in summary['summary']][0]
    post_sd_l = [x[2] for x in summary['summary']][0]

    prior_sd_l = 6.44787

    z_score = (post_mean_l - simu_l) / post_sd_l
    shrinkage = 1 - (post_sd_l / prior_sd_l)**2

    return [warning_code, sbc_rank, z_score, shrinkage]
Ejemplo n.º 2
0
        x1_mean[m] = mcmc_stats[0]
        x1_se[m] = mcmc_stats[1]

    return iters, x1_mean, x1_se


############################################################
# Normal Model
############################################################

# Compile Stan program and fit with dynamic Hamiltonian Monte Carlo
model = stan_utility.compile_model('normal.stan')
fit = model.sampling(seed=4838282)

# Check diagnostics
stan_utility.check_all_diagnostics(fit)

# Check MCMC estimators
print(fit)

############################################################
# Student-t Model
############################################################

model = stan_utility.compile_model('student_t.stan')

# 100 degrees of freedom
data = dict(nu=100)
fit100 = model.sampling(data=data,
                        seed=4838282,
                        control=dict(metric="unit_e",
Ejemplo n.º 3
0
            x_obs = fit.extract()['x_obs'][0,:])

pystan.stan_rdump(data, 'selection.data.R')

############################################################
# Fit model
############################################################

data = pystan.read_rdump('selection.data.R')

model = stan_utility.compile_model('selection.stan')
fit = model.sampling(data=data, chains=4, seed=4938483,
                     control=dict(adapt_delta=0.9, max_treedepth=12))

# Check diagnostics
stan_utility.check_all_diagnostics(fit)

# Default visual summaries
params = fit.extract()

# Plot marginal posteriors
f, axarr = plot.subplots(2, 2)
for a in axarr[0,:]:
    a.xaxis.set_ticks_position('bottom')
    a.yaxis.set_ticks_position('none')
for a in axarr[1,:]:
    a.xaxis.set_ticks_position('bottom')
    a.yaxis.set_ticks_position('none')

axarr[0, 0].set_title("mu")
axarr[0, 0].hist(params['mu'], bins = 25, color = dark, ec = dark_highlight)
Ejemplo n.º 4
0
# gen.model = stan_model("gen.stan")
# gen.data = sampling(data=d, algorithm='Fixed_param', warmup=0, iter=200)
# gendf = as.data.frame(gen.data)
# dataset.1 = gendf[1,]

#############
## Fit the model
#############
model = util.compile_model("../pooled.stan")
fit = model.sampling(dict(log_radon=df.log_radon,
                          basement = df.basement,
                          N=df.shape[0]))
# #############
# ## Check diagnostics
# #############
util.check_all_diagnostics(fit)

# #############
# ## Check & graph fit
# #############
print(fit)
sample = fit.extract()

# # This line just plots our data by basement indicator
g = ggplot(df, aes(x='basement', y='log_radon')) + geom_point()

# This next snippet will select every 10th posterior sample and plot the
# fit lines corresponding to that sample. It's a good way to get a sense of
# the uncertainty / distribution of the posterior
for i in range(0, len(sample['alpha']), 100):
    g += geom_abline(intercept = sample['alpha'][i],