from __future__ import division
import os
import sys
import glob
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
%precision 4
#plt.style.use('ggplot')
import seaborn as sns
sns.set_style('white')
sns.set_context('paper')
np.random.seed(1234)
import pymc3 as pm
import scipy.stats as stats
import logging
_logger = logging.getLogger("theano.gof.compilelock")
_logger.setLevel(logging.ERROR)
n = 100
h = 61
alpha = 2
beta = 2
niter = 1000
with pm.Model() as model: # context management
# define priors
p = pm.Beta('p', alpha=alpha, beta=beta)
# define likelihood
y = pm.Binomial('y', n=n, p=p, observed=h)
# inference
start = pm.find_MAP() # Use MAP estimate (optimization) as the initial state for MCMC
step = pm.Metropolis() # Have a choice of samplers
trace = pm.sample(niter, step, start, random_seed=123, progressbar=True)
[-----------------100%-----------------] 1000 of 1000 complete in 0.1 sec
plt.hist(trace['p'], 15, histtype='step', normed=True, label='post');
x = np.linspace(0, 1, 100)
plt.plot(x, stats.beta.pdf(x, alpha, beta), label='prior');
plt.legend(loc='best');