Computational Methods in Bayesian Analysis

 【Markov chain Monte Carlo】【Gibbs Sampling】【The Metropolis-Hastings Algorithm】【Random-walk Metropolis-Hastings】【Adaptive Metropolis】
 

About the author 

This notebook was forked from this project . The original author is Chris Fonnesbeck, Assistant Professor of Biostatistics. You can follow Chris on Twitter @fonnesbeck .

 

Introduction 

For most problems of interest, Bayesian analysis requires integration over multiple parameters, making the calculation of a posterior intractable whether via analytic methods or standard methods of numerical integration.

However, it is often possible to approximate these integrals by drawing samples from posterior distributions. For example, consider the expected value (mean) of a vector-valued random variable x:

E[x]=xf(x)dx,x={x1,,xk}

where k (dimension of vector x) is perhaps very large.

 

If we can produce a reasonable number of random vectors {xi}, we can use these values to approximate the unknown integral. This process is known as Monte Carlo integration . In general, Monte Carlo integration allows integrals against probability density functions

I=h(x)f(x)dx

to be estimated by finite sums

I^=1ni=1nh(xi),

where xi is a sample from f. This estimate is valid and useful because:

 

Example (Negative Binomial Distribution) 

We can use this kind of simulation to estimate the expected value of a random variable that is negative binomial-distributed. The negative binomial distributionapplies to discrete positive random variables. It can be used to model the number of Bernoulli trials that one can expect to conduct until r failures occur.

 

The probability mass function reads

f(kp,r)=(k+r1k)(1p)kpr,

where k{0,1,2,} is the value taken by our non-negative discrete random variable and p is the probability of success ($0

negative binomial (courtesy Wikipedia) - Bayesian Analysis image01

 

Most frequently, this distribution is used to model overdispersed counts , that is, counts that have variance larger than the mean (i.e., what would be predicted under aPoisson distribution ).

In fact, the negative binomial can be expressed as a continuous mixture of Poisson distributions, where a gamma distributions act as mixing weights:

f(kp,r)=0Poisson(kλ)Gamma(r,(1p)/p)(λ)dλ,

where the parameters of the gamma distribution are denoted as (shape parameter, inverse scale parameter).

Let's resort to simulation to estimate the mean of a negative binomial distribution withp=0.7 and r=3:

import numpy as np

r = 3
p = 0.7
# Simulate Gamma means (r: shape parameter; p / (1 - p): scale parameter).
lam = np.random.gamma(r, p / (1 - p), size=100)
# Simulate sample Poisson conditional on lambda.
sim_vals = np.random.poisson(lam)
sim_vals.mean()
6.3399999999999999
 

The actual expected value of the negative binomial distribution is rp/(1p), which in this case is 7. That's pretty close, though we can do better if we draw more samples:

lam = np.random.gamma(r, p / (1 - p), size=100000)
sim_vals = np.random.poisson(lam)
sim_vals.mean()
7.0135199999999998
 

This approach of drawing repeated random samples in order to obtain a desired numerical result is generally known as Monte Carlo simulation .

Clearly, this is a convenient, simplistic example that did not require simuation to obtain an answer. For most problems, it is simply not possible to draw independent random samples from the posterior distribution because they will generally be (1) multivariate and (2) not of a known functional form for which there is a pre-existing random number generator.

However, we are not going to give up on simulation. Though we cannot generally draw independent samples for our model, we can usually generate dependent samples, and it turns out that if we do this in a particular way, we can obtain samples from almost any posterior distribution.

 

Markov Chains 

A Markov chain is a special type of stochastic process . The standard definition of a stochastic process is an ordered collection of random variables:

{Xt:tT}

where t is frequently (but not necessarily) a time index. If we think of Xt as a state Xat time t, and invoke the following dependence condition on each state:

Pr(Xt+1=xt+1|Xt=xt,Xt1=xt1,,X0=x0)=Pr(Xt+1=xt+1|Xt=xt)

then the stochastic process is known as a Markov chain. This conditioning specifies that the future depends on the current state, but not past states. Thus, the Markov chain wanders about the state space, remembering only where it has just been in the last time step.

The collection of transition probabilities is sometimes called a transition matrix when dealing with discrete states, or more generally, a transition kernel .

It is useful to think of the Markovian property as mild non-independence .

If we use Monte Carlo simulation to generate a Markov chain, this is called Markov chain Monte Carlo , or MCMC. If the resulting Markov chain obeys some important properties, then it allows us to indirectly generate independent samples from a particular posterior distribution.

Why MCMC Works: Reversible Markov Chains 

 Markov chain Monte Carlo simulates a Markov chain for which some function of interest (e.g., the joint distribution of the parameters of some model) is the unique, invariant limiting distribution. An invariant distribution with respect to some Markov chain with transition kernel Pr(yx) implies that:

xPr(yx)π(x)dx=π(y).

 Invariance is guaranteed for any reversible Markov chain. Consider a Markov chain in reverse sequence: {θ(n),θ(n1),...,θ(0)}. This sequence is still Markovian, because:

Pr(θ(k)=yθ(k+1)=x,θ(k+2)=x1,)=Pr(θ(k)=yθ(k+1)=x)

 Forward and reverse transition probabilities may be related through Bayes theorem:

Pr(θ(k+1)=xθ(k)=y)π(k)(y)π(k+1)(x)

 Though not homogeneous in general, π becomes homogeneous if:

  •  n 

  •  π(i)=π for some $i 

 If this chain is homogeneous it is called reversible, because it satisfies thedetailed balance equation :

π(x)Pr(yx)=π(y)Pr(xy)

 Reversibility is important because it has the effect of balancing movement through the entire state space. When a Markov chain is reversible, π is the unique, invariant, stationary distribution of that chain. Hence, if π is of interest, we need only find the reversible Markov chain for which π is the limiting distribution. This is what MCMC does! 

 
 

Gibbs Sampling 

The Gibbs sampler is the simplest and most prevalent MCMC algorithm. If a posterior has k parameters to be estimated, we may condition each parameter on current values of the other k1 parameters, and sample from the resultant distributional form (usually easier), and repeat this operation on the other parameters in turn. This procedure generates samples from the posterior distribution. Note that we have now combined Markov chains (conditional independence) and Monte Carlo techniques (estimation by simulation) to yield Markov chain Monte Carlo.

Here is a stereotypical Gibbs sampling algorithm:

  1. Choose starting values for states (parameters): θ=[θ(0)1,θ(0)2,,θ(0)k].

  2. Initialize counter j=1.

  3. Draw the following values from each of the k conditional distributions:

     

    θ(j)1θ(j)2θ(j)3θ(j)k1θ(j)kπ(θ1|θ(j1)2,θ(j1)3,,θ(j1)k1,θ(j1)k)π(θ2|θ(j)1,θ(j1)3,,θ(j1)k1,θ(j1)k)π(θ3|θ(j)1,θ(j)2,,θ(j1)k1,θ(j1)k)π(θk1|θ(j)1,θ(j)2,,θ(j)k2,θ(j1)k)π(θk|θ(j)1,θ(j)2,θ(j)4,,θ(j)k2,θ(j)k1)

     

  4. Increment j and repeat until convergence occurs.

As we can see from the algorithm, each distribution is conditioned on the last iteration of its chain values, constituting a Markov chain as advertised. The Gibbs sampler has all of the important properties outlined in the previous section: it is aperiodic, homogeneous and ergodic. Once the sampler converges, all subsequent samples are from the target distribution. This convergence occurs at a geometric rate.

 

Example: Inferring patterns in UK coal mining disasters 

Let's try to model a more interesting example, a time series of recorded coal mining disasters in the UK from 1851 to 1962.

Occurrences of disasters in the time series is thought to be derived from a Poisson process with a large rate parameter in the early part of the time series, and from one with a smaller rate in the later part. We are interested in locating the change point in the series, which perhaps is related to changes in mining safety regulations.

disasters_array = np.array([4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6,
                            3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5,
                            2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0,
                            1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1,
                            0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2,
                            3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4,
                            0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])

n_count_data = len(disasters_array)
import plotly.plotly as py
import plotly.graph_objs as pgo
data = pgo.Data([
        pgo.Scatter(
            x=[str(year) + '-01-01' for year in np.arange(1851, 1962)],
            y=disasters_array,
            mode='lines+markers'
    )
])
layout = pgo.Layout(
    title='UK coal mining disasters (per year), 1851--1962',
    xaxis=pgo.XAxis(title='Year', type='date', range=['1851-01-01', '1962-01-01']),
    yaxis=pgo.YAxis(title='Disaster count')
)
fig = pgo.Figure(data=data, layout=layout)
py.iplot(fig, filename='coal_mining_disasters')
 

We are going to use Poisson random variables for this type of count data. Denoting year i's accident count by yi,

yiPoisson(λ).

For those unfamiliar, Poisson random variables look like this:

data2 = pgo.Data([
        pgo.Histogram(
            x=np.random.poisson(l, 1000),
            opacity=0.75,
            name=u'λ=%i' % l
        ) for l in [1, 5, 12, 25]
])
layout_grey_bg = pgo.Layout(
    xaxis=pgo.XAxis(zeroline=False, showgrid=True, gridcolor='rgb(255, 255, 255)'),
    yaxis=pgo.YAxis(zeroline=False, showgrid=True, gridcolor='rgb(255, 255, 255)'),
    paper_bgcolor='rgb(255, 255, 255)',
    plot_bgcolor='rgba(204, 204, 204, 0.5)'
)
layout2 = layout_grey_bg.copy()
layout2.update(
    barmode='overlay',
    title='Poisson Means',
    xaxis=pgo.XAxis(range=[0, 50]),
    yaxis=pgo.YAxis(range=[0, 400])
)
fig2 = pgo.Figure(data=data2, layout=layout2)
py.iplot(fig2, filename='poisson_means')
 

The modeling problem is about estimating the values of the λ parameters. Looking at the time series above, it appears that the rate declines over time.

changepoint model identifies a point (here, a year) after which the parameter λdrops to a lower value. Let us call this point in time τ. So we are estimating two λparameters: λ=λ1 if t<τ and λ=λ2 if tτ.

We need to assign prior probabilities to both {λ1,λ2}. The gamma distribution not only provides a continuous density function for positive numbers, but it is alsoconjugate with the Poisson sampling distribution.

lambda1_lambda2 = [(0.1, 100), (1, 100), (1, 10), (10, 10)]
data3 = pgo.Data([
        pgo.Histogram(
            x=np.random.gamma(*p, size=1000),
            opacity=0.75,
            name=u'α=%i, β=%i' % (p[0], p[1]))
        for p in lambda1_lambda2
])
layout3 = layout_grey_bg.copy()
layout3.update(
    barmode='overlay',
    xaxis=pgo.XAxis(range=[0, 300])
)
fig3 = pgo.Figure(data=data3, layout=layout3)
py.iplot(fig3, filename='gamma_distributions')
 

We will specify suitably vague hyperparameters α and β for both priors:

λ1λ2Gamma(1,10),Gamma(1,10).

Since we do not have any intuition about the location of the changepoint (unless we visualize the data), we will assign a discrete uniform prior over the entire observation period [1851, 1962]:

τDiscreteUniform(1851, 1962)P(τ=k)=1111.
 

Implementing Gibbs sampling 

We are interested in estimating the joint posterior of λ1,λ2 and τ given the array of annnual disaster counts y. This gives:

P(λ1,λ2,τ|y)P(y|λ1,λ2,τ)P(λ1,λ2,τ)

To employ Gibbs sampling, we need to factor the joint posterior into the product of conditional expressions:

P(λ1,λ2,τ|y)P(yt<τ|λ1,τ)P(ytτ|λ2,τ)P(λ1)P(λ2)P(τ)

which we have specified as:

P(λ1,λ2,τ|y)[t=1851τPoi(yt|λ1)t=τ+11962Poi(yt|λ2)]Gamma(λ1|α,β)Gamma(λ2|α,β)1111[t=1851τeλ1λyt1t=τ+11962eλ2λyt2]λα11eβλ1λα12eβλ2λτt=1851yt+α11e(β+τ)λ1λ1962t=τ+1yi+α12eβλ2

So, the full conditionals are known, and critically for Gibbs, can easily be sampled from.

λ1Gamma(t=1851τyt+α,τ+β)
λ2Gamma(t=τ+11962yi+α,1962τ+β)
τCategorical⎛⎝λτt=1851yt+α11e(β+τ)λ1λ1962t=τ+1yi+α12eβλ21962k=1851λτt=1851yt+α11e(β+τ)λ1λ1962t=τ+1yi+α12eβλ2⎞⎠

Implementing this in Python requires random number generators for both the gamma and discrete uniform distributions. We can leverage NumPy for this:

# Function to draw random gamma variate
rgamma = np.random.gamma

def rcategorical(probs, n=None):
    # Function to draw random categorical variate
    return np.array(probs).cumsum().searchsorted(np.random.sample(n))
 

Next, in order to generate probabilities for the conditional posterior of τ, we need the kernel of the gamma density:

 

λα1eβλ

 

dgamma = lambda lam, a, b: lam**(a - 1) * np.exp(-b * lam)
 

Diffuse hyperpriors for the gamma priors on {λ1,λ2}:

alpha, beta = 1., 10
 

For computational efficiency, it is best to pre-allocate memory to store the sampled values. We need 3 arrays, each with length equal to the number of iterations we plan to run:

# Specify number of iterations
n_iterations = 1000

# Initialize trace of samples
lambda1, lambda2, tau = np.empty((3, n_iterations + 1))
 

The penultimate step initializes the model paramters to arbitrary values:

lambda1[0] = 6
lambda2[0] = 2
tau[0] = 50
 

Now we can run the Gibbs sampler.

# Sample from conditionals
for i in range(n_iterations):
    
    # Sample early mean
    lambda1[i + 1] = rgamma(disasters_array[:tau[i]].sum() + alpha, 1./(tau[i] + beta))
    
    # Sample late mean
    lambda2[i + 1] = rgamma(disasters_array[tau[i]:].sum() + alpha,
                            1./(n_count_data - tau[i] + beta))
    
    # Sample changepoint: first calculate probabilities (conditional)
    p = np.array([dgamma(lambda1[i + 1], disasters_array[:t].sum() + alpha, t + beta) *
                  dgamma(lambda2[i + 1], disasters_array[t:].sum() + alpha, n_count_data - t + beta)
                  for t in range(n_count_data)])
    
    # ... then draw sample
    tau[i + 1] = rcategorical(p/p.sum())
 

Plotting the trace and histogram of the samples reveals the marginal posteriors of each parameter in the model.

color = '#3182bd'
trace1 = pgo.Scatter(
    y=lambda1,
    xaxis='x1',
    yaxis='y1',
    line=pgo.Line(width=1),
    marker=pgo.Marker(color=color)
)

trace2 = pgo.Histogram(
    x=lambda1,
    xaxis='x2',
    yaxis='y2',
    line=pgo.Line(width=0.5),
    marker=pgo.Marker(color=color)
)

trace3 = pgo.Scatter(
    y=lambda2,
    xaxis='x3',
    yaxis='y3',
    line=pgo.Line(width=1),
    marker=pgo.Marker(color=color)
)

trace4 = pgo.Histogram(
    x=lambda2,
    xaxis='x4',
    yaxis='y4',
    marker=pgo.Marker(color=color)
)

trace5 = pgo.Scatter(
    y=tau,
    xaxis='x5',
    yaxis='y5',
    line=pgo.Line(width=1),
    marker=pgo.Marker(color=color)
)

trace6 = pgo.Histogram(
    x=tau,
    xaxis='x6',
    yaxis='y6',
    marker=pgo.Marker(color=color)
)
data4 = pgo.Data([trace1, trace2, trace3, trace4, trace5, trace6])
import plotly.tools as tls
fig4 = tls.make_subplots(3, 2)
 
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]
[ (3,1) x5,y5 ]  [ (3,2) x6,y6 ]

fig4['data'] += data4
def add_style(fig):
    for i in fig['layout'].keys():
        fig['layout'][i]['zeroline'] = False
        fig['layout'][i]['showgrid'] = True
        fig['layout'][i]['gridcolor'] = 'rgb(255, 255, 255)'
    fig['layout']['paper_bgcolor'] = 'rgb(255, 255, 255)'
    fig['layout']['plot_bgcolor'] = 'rgba(204, 204, 204, 0.5)'
    fig['layout']['showlegend']=False
add_style(fig4)
fig4['layout'].update(
    yaxis1=pgo.YAxis(title=r'$\lambda_1$'),
    yaxis3=pgo.YAxis(title=r'$\lambda_2$'),
    yaxis5=pgo.YAxis(title=r'$\tau$'))
py.iplot(fig4, filename='modelling_params')
 

The Metropolis-Hastings Algorithm 

The key to success in applying the Gibbs sampler to the estimation of Bayesian posteriors is being able to specify the form of the complete conditionals of θ, because the algorithm cannot be implemented without them. In practice, the posterior conditionals cannot always be neatly specified.

Taking a different approach, the Metropolis-Hastings algorithm generates candidatestate transitions from an alternate distribution, and accepts or rejects each candidate probabilistically.

Let us first consider a simple Metropolis-Hastings algorithm for a single parameter, θ. We will use a standard sampling distribution, referred to as the proposal distribution , to produce candidate variables qt(θ|θ). That is, the generated value, θ, is a possiblenext value for θ at step t+1. We also need to be able to calculate the probability of moving back to the original value from the candidate, or qt(θ|θ). These probabilistic ingredients are used to define an acceptance ratio :

a(θ,θ)=qt(θ|θ)π(θ)qt(θ|θ)π(θ)

The value of θ(t+1) is then determined by:

θ(t+1)={θθ(t)with probability min(a(θ,θ(t)),1)with probability 1min(a(θ,θ(t)),1)

This transition kernel implies that movement is not guaranteed at every step. It only occurs if the suggested transition is likely based on the acceptance ratio.

A single iteration of the Metropolis-Hastings algorithm proceeds as follows:

  1. Sample θ from q(θ|θ(t)).

  2. Generate a Uniform[0,1] random variate u.

  3. If a(θ,θ)>u then θ(t+1)=θ, otherwise θ(t+1)=θ(t).

The original form of the algorithm specified by Metropolis required that qt(θ|θ)=qt(θ|θ), which reduces a(θ,θ) to π(θ)/π(θ), but this is not necessary. In either case, the state moves to high-density points in the distribution with high probability, and to low-density points with low probability. After convergence, the Metropolis-Hastings algorithm describes the full target posterior density, so all points are recurrent.

Random-walk Metropolis-Hastings 

A practical implementation of the Metropolis-Hastings algorithm makes use of a random-walk proposal. Recall that a random walk is a Markov chain that evolves according to:

θ(t+1)=θ(t)+ϵtϵtf(ϕ)

As applied to the MCMC sampling, the random walk is used as a proposal distribution, whereby dependent proposals are generated according to:

q(θ|θ(t))=f(θθ(t))=θ(t)+ϵt

Generally, the density generating ϵt is symmetric about zero, resulting in a symmetric chain. Chain symmetry implies that q(θ|θ(t))=q(θ(t)|θ), which reduces the Metropolis-Hastings acceptance ratio to:

a(θ,θ)=π(θ)π(θ)

The choice of the random walk distribution for ϵt is frequently a normal or Student’s tdensity, but it may be any distribution that generates an irreducible proposal chain.

An important consideration is the specification of the scale parameter for the random walk error distribution. Large values produce random walk steps that are highly exploratory, but tend to produce proposal values in the tails of the target distribution, potentially resulting in very small acceptance rates. Conversely, small values tend to be accepted more frequently, since they tend to produce proposals close to the current parameter value, but may result in chains that mix very slowly.

Some simulation studies suggest optimal acceptance rates in the range of 20-50%. It is often worthwhile to optimize the proposal variance by iteratively adjusting its value, according to observed acceptance rates early in the MCMC simulation .

 

Example: Linear model estimation 

This very simple dataset is a selection of real estate prices p, with the associated age aof each house. We wish to estimate a simple linear relationship between the two variables, using the Metropolis-Hastings algorithm.

Linear model :

μi=β0+β1ai

Sampling distribution :

piN(μi,τ)

Prior distributions :

βiN(0,10000)τGamma(0.001,0.001)
age = np.array([13, 14, 14,12, 9, 15, 10, 14, 9, 14, 13, 12, 9, 10, 15, 11, 
                15, 11, 7, 13, 13, 10, 9, 6, 11, 15, 13, 10, 9, 9, 15, 14, 
                14, 10, 14, 11, 13, 14, 10])

price = np.array([2950, 2300, 3900, 2800, 5000, 2999, 3950, 2995, 4500, 2800, 
                  1990, 3500, 5100, 3900, 2900, 4950, 2000, 3400, 8999, 4000, 
                  2950, 3250, 3950, 4600, 4500, 1600, 3900, 4200, 6500, 3500, 
                  2999, 2600, 3250, 2500, 2400, 3990, 4600, 450,4700])/1000.
 

To avoid numerical underflow issues, we typically work with log-transformed likelihoods, so the joint posterior can be calculated as sums of log-probabilities and log-likelihoods.

This function calculates the joint log-posterior, conditional on values for each parameter:

from scipy.stats import distributions
dgamma = distributions.gamma.logpdf
dnorm = distributions.norm.logpdf

def calc_posterior(a, b, t, y=price, x=age):
    # Calculate joint posterior, given values for a, b and t

    # Priors on a,b
    logp = dnorm(a, 0, 10000) + dnorm(b, 0, 10000)
    # Prior on t
    logp += dgamma(t, 0.001, 0.001)
    # Calculate mu
    mu = a + b*x
    # Data likelihood
    logp += sum(dnorm(y, mu, t**-2))
    
    return logp
 

The metropolis function implements a simple random-walk Metropolis-Hastings sampler for this problem. It accepts as arguments:

  • the number of iterations to run
  • initial values for the unknown parameters
  • the variance parameter of the proposal distribution (normal)
rnorm = np.random.normal
runif = np.random.rand

def metropolis(n_iterations, initial_values, prop_var=1):

    n_params = len(initial_values)
            
    # Initial proposal standard deviations
    prop_sd = [prop_var]*n_params
    
    # Initialize trace for parameters
    trace = np.empty((n_iterations+1, n_params))
    
    # Set initial values
    trace[0] = initial_values
        
    # Calculate joint posterior for initial values
    current_log_prob = calc_posterior(*trace[0])
    
    # Initialize acceptance counts
    accepted = [0]*n_params
    
    for i in range(n_iterations):
    
        if not i%1000: print('Iteration %i' % i)
    
        # Grab current parameter values
        current_params = trace[i]
    
        for j in range(n_params):
    
            # Get current value for parameter j
            p = trace[i].copy()
    
            # Propose new value
            if j==2:
                # Ensure tau is positive
                theta = np.exp(rnorm(np.log(current_params[j]), prop_sd[j]))
            else:
                theta = rnorm(current_params[j], prop_sd[j])
            
            # Insert new value 
            p[j] = theta
    
            # Calculate log posterior with proposed value
            proposed_log_prob = calc_posterior(*p)
    
            # Log-acceptance rate
            alpha = proposed_log_prob - current_log_prob
    
            # Sample a uniform random variate
            u = runif()
    
            # Test proposed value
            if np.log(u) < alpha:
                # Accept
                trace[i+1,j] = theta
                current_log_prob = proposed_log_prob
                accepted[j] += 1
            else:
                # Reject
                trace[i+1,j] = trace[i,j]
                
    return trace, accepted
 

Let's run the MH algorithm with a very small proposal variance:

n_iter = 10000
trace, acc = metropolis(n_iter, initial_values=(1,0,1), prop_var=0.001)
 
Iteration 0
Iteration 1000
Iteration 2000
Iteration 3000
Iteration 4000
Iteration 5000
Iteration 6000
Iteration 7000
Iteration 8000
Iteration 9000
 

We can see that the acceptance rate is way too high:

np.array(acc, float)/n_iter
array([ 0.9768,  0.9689,  0.961 ])
trace1 = pgo.Scatter(
    y=trace.T[0],
    xaxis='x1',
    yaxis='y1',
    marker=pgo.Marker(color=color)
)

trace2 = pgo.Histogram(
    x=trace.T[0],
    xaxis='x2',
    yaxis='y2',
    marker=pgo.Marker(color=color)
)

trace3 = pgo.Scatter(
    y=trace.T[1],
    xaxis='x3',
    yaxis='y3',
    marker=pgo.Marker(color=color)
)

trace4 = pgo.Histogram(
    x=trace.T[1],
    xaxis='x4',
    yaxis='y4',
    marker=pgo.Marker(color=color)
)

trace5 = pgo.Scatter(
    y=trace.T[2],
    xaxis='x5',
    yaxis='y5',
    marker=pgo.Marker(color=color)
)

trace6 = pgo.Histogram(
    x=trace.T[2],
    xaxis='x6',
    yaxis='y6',
    marker=pgo.Marker(color=color)
)
data5 = pgo.Data([trace1, trace2, trace3, trace4, trace5, trace6])
fig5 = tls.make_subplots(3, 2)
 
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]
[ (3,1) x5,y5 ]  [ (3,2) x6,y6 ]

fig5['data'] += data5
add_style(fig5)
fig5['layout'].update(showlegend=False,
                     yaxis1=pgo.YAxis(title='intercept'),
                     yaxis3=pgo.YAxis(title='slope'),
                     yaxis5=pgo.YAxis(title='precision')
)
py.iplot(fig5, filename='MH algorithm small proposal variance')
 

Now, with a very large proposal variance:

trace_hivar, acc = metropolis(n_iter, initial_values=(1,0,1), prop_var=100)
 
Iteration 0
Iteration 1000
Iteration 2000
Iteration 3000
Iteration 4000
Iteration 5000
Iteration 6000
Iteration 7000
Iteration 8000
Iteration 9000
np.array(acc, float)/n_iter
array([ 0.003 ,  0.0001,  0.0009])
trace1 = pgo.Scatter(
    y=trace_hivar.T[0],
    xaxis='x1',
    yaxis='y1',
    marker=pgo.Marker(color=color)
)

trace2 = pgo.Histogram(
    x=trace_hivar.T[0],
    xaxis='x2',
    yaxis='y2',
    marker=pgo.Marker(color=color)
)

trace3 = pgo.Scatter(
    y=trace_hivar.T[1],
    xaxis='x3',
    yaxis='y3',
    marker=pgo.Marker(color=color)
)

trace4 = pgo.Histogram(
    x=trace_hivar.T[1],
    xaxis='x4',
    yaxis='y4',
    marker=pgo.Marker(color=color)
)

trace5 = pgo.Scatter(
    y=trace_hivar.T[2],
    xaxis='x5',
    yaxis='y5',
    marker=pgo.Marker(color=color)
)

trace6 = pgo.Histogram(
    x=trace_hivar.T[2],
    xaxis='x6',
    yaxis='y6',
    marker=pgo.Marker(color=color)
)
data6 = pgo.Data([trace1, trace2, trace3, trace4, trace5, trace6])
fig6 = tls.make_subplots(3, 2)
 
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]
[ (3,1) x5,y5 ]  [ (3,2) x6,y6 ]

fig6['data'] += data6
add_style(fig6)
fig6['layout'].update(
    yaxis1=pgo.YAxis(title='intercept'),
    yaxis3=pgo.YAxis(title='slope'),
    yaxis5=pgo.YAxis(title='precision')
)
py.iplot(fig6, filename='MH algorithm large proposal variance')
 

Adaptive Metropolis 

In order to avoid having to set the proposal variance by trial-and-error, we can add some tuning logic to the algorithm. The following implementation of Metropolis-Hastings reduces proposal variances by 10% when the acceptance rate is low, and increases it by 10% when the acceptance rate is high.

def metropolis_tuned(n_iterations, initial_values, f=calc_posterior, prop_var=1, 
                     tune_for=None, tune_interval=100):
    
    n_params = len(initial_values)
            
    # Initial proposal standard deviations
    prop_sd = [prop_var] * n_params
    
    # Initialize trace for parameters
    trace = np.empty((n_iterations+1, n_params))
    
    # Set initial values
    trace[0] = initial_values
    # Initialize acceptance counts
    accepted = [0]*n_params
    
    # Calculate joint posterior for initial values
    current_log_prob = f(*trace[0])
    
    if tune_for is None:
        tune_for = n_iterations/2

    for i in range(n_iterations):
    
        if not i%1000: print('Iteration %i' % i)
    
        # Grab current parameter values
        current_params = trace[i]
    
        for j in range(n_params):
    
            # Get current value for parameter j
            p = trace[i].copy()
    
            # Propose new value
            if j==2:
                # Ensure tau is positive
                theta = np.exp(rnorm(np.log(current_params[j]), prop_sd[j]))
            else:
                theta = rnorm(current_params[j], prop_sd[j])
            
            # Insert new value 
            p[j] = theta
    
            # Calculate log posterior with proposed value
            proposed_log_prob = f(*p)
    
            # Log-acceptance rate
            alpha = proposed_log_prob - current_log_prob
    
            # Sample a uniform random variate
            u = runif()
    
            # Test proposed value
            if np.log(u) < alpha:
                # Accept
                trace[i+1,j] = theta
                current_log_prob = proposed_log_prob
                accepted[j] += 1
            else:
                # Reject
                trace[i+1,j] = trace[i,j]
                
            # Tune every 100 iterations
            if (not (i+1) % tune_interval) and (i < tune_for):
        
                # Calculate aceptance rate
                acceptance_rate = (1.*accepted[j])/tune_interval
                if acceptance_rate<0.1:
                    prop_sd[j] *= 0.9
                if acceptance_rate<0.2:
                    prop_sd[j] *= 0.95
                if acceptance_rate>0.4:
                    prop_sd[j] *= 1.05
                elif acceptance_rate>0.6:
                    prop_sd[j] *= 1.1
        
                accepted[j] = 0
                
    return trace[tune_for:], accepted
trace_tuned, acc = metropolis_tuned(n_iter*2, initial_values=(1,0,1), prop_var=5, tune_interval=25, tune_for=n_iter)
 
Iteration 0
Iteration 1000
Iteration 2000
Iteration 3000
Iteration 4000
Iteration 5000
Iteration 6000
Iteration 7000
Iteration 8000
Iteration 9000
Iteration 10000
Iteration 11000
Iteration 12000
Iteration 13000
Iteration 14000
Iteration 15000
Iteration 16000
Iteration 17000
Iteration 18000
Iteration 19000
np.array(acc, float)/(n_iter)
array([ 0.2888,  0.312 ,  0.3421])
trace1 = pgo.Scatter(
    y=trace_tuned.T[0],
    xaxis='x1',
    yaxis='y1',
    line=pgo.Line(width=1),
    marker=pgo.Marker(color=color)
)

trace2 = pgo.Histogram(
    x=trace_tuned.T[0],
    xaxis='x2',
    yaxis='y2',
    marker=pgo.Marker(color=color)
)

trace3 = pgo.Scatter(
    y=trace_tuned.T[1],
    xaxis='x3',
    yaxis='y3',
    line=pgo.Line(width=1),
    marker=pgo.Marker(color=color)
)

trace4 = pgo.Histogram(
    x=trace_tuned.T[1],
    xaxis='x4',
    yaxis='y4',
    marker=pgo.Marker(color=color)
)

trace5 = pgo.Scatter(
    y=trace_tuned.T[2],
    xaxis='x5',
    yaxis='y5',
    line=pgo.Line(width=0.5),
    marker=pgo.Marker(color=color)
)

trace6 = pgo.Histogram(
    x=trace_tuned.T[2],
    xaxis='x6',
    yaxis='y6',
    marker=pgo.Marker(color=color)
)
data7 = pgo.Data([trace1, trace2, trace3, trace4, trace5, trace6])
fig7 = tls.make_subplots(3, 2)
 
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]
[ (3,1) x5,y5 ]  [ (3,2) x6,y6 ]

fig7['data'] += data7
add_style(fig7)
fig7['layout'].update(
    yaxis1=pgo.YAxis(title='intercept'),
    yaxis3=pgo.YAxis(title='slope'),
    yaxis5=pgo.YAxis(title='precision')
)
py.iplot(fig7, filename='adaptive-metropolis')
 

50 random regression lines drawn from the posterior:

# Data points
points = pgo.Scatter(
    x=age,
    y=price,
    mode='markers'
)

# Sample models from posterior
xvals = np.linspace(age.min(), age.max())
line_data = [np.column_stack([np.ones(50), xvals]).dot(trace_tuned[np.random.randint(0, 1000), :2]) for i in range(50)]

# Generate Scatter obejcts
lines = [pgo.Scatter(x=xvals, y=line, opacity=0.5, marker=pgo.Marker(color='#e34a33'),
                     line=pgo.Line(width=0.5)) for line in line_data]

data8 = pgo.Data([points] + lines)

layout8 = layout_grey_bg.copy()
layout8.update(
    showlegend=False,
    hovermode='closest',
    xaxis=pgo.XAxis(title='Age', showgrid=False, zeroline=False),
    yaxis=pgo.YAxis(title='Price', showline=False, zeroline=False)
)

fig8 = pgo.Figure(data=data8, layout=layout8)
py.iplot(fig8, filename='regression_lines')
 

Exercise: Bioassay analysis 

Gelman et al. (2003) present an example of an acute toxicity test, commonly performed on animals to estimate the toxicity of various compounds.

In this dataset log_dose includes 4 levels of dosage, on the log scale, each administered to 5 rats during the experiment. The response variable is death , the number of positive responses to the dosage.

The number of deaths can be modeled as a binomial response, with the probability of death being a linear function of dose:

yilogit(pi)Bin(ni,pi)=a+bxi

The common statistic of interest in such experiments is the LD50 , the dosage at which the probability of death is 50%.

Use Metropolis-Hastings sampling to fit a Bayesian model to analyze this bioassay data, and to estimate LD50.

# Log dose in each group
log_dose = [-.86, -.3, -.05, .73]

# Sample size in each group
n = 5

# Outcomes
deaths = [0, 1, 3, 5]
from scipy.stats import distributions
dbin = distributions.binom.logpmf
dnorm = distributions.norm.logpdf

invlogit = lambda x: 1./(1 + np.exp(-x))

def calc_posterior(a, b, y=deaths, x=log_dose):

    # Priors on a,b
    logp = dnorm(a, 0, 10000) + dnorm(b, 0, 10000)
    # Calculate p
    p = invlogit(a + b*np.array(x))
    # Data likelihood
    logp += sum([dbin(yi, n, pi) for yi,pi in zip(y,p)])
    
    return logp
bioassay_trace, acc = metropolis_tuned(n_iter, f=calc_posterior, initial_values=(1,0), prop_var=5, tune_for=9000)
 
Iteration 0
Iteration 1000
Iteration 2000
Iteration 3000
Iteration 4000
Iteration 5000
Iteration 6000
Iteration 7000
Iteration 8000
Iteration 9000
trace1 = pgo.Scatter(
    y=bioassay_trace.T[0],
    xaxis='x1',
    yaxis='y1',
    marker=pgo.Marker(color=color)
)

trace2 = pgo.Histogram(
    x=bioassay_trace.T[0],
    xaxis='x2',
    yaxis='y2',
    marker=pgo.Marker(color=color)
)

trace3 = pgo.Scatter(
    y=bioassay_trace.T[1],
    xaxis='x3',
    yaxis='y3',
    marker=pgo.Marker(color=color)
)

trace4 = pgo.Histogram(
    x=bioassay_trace.T[1],
    xaxis='x4',
    yaxis='y4',
    marker=pgo.Marker(color=color)
)
data9 = pgo.Data([trace1, trace2, trace3, trace4])
fig9 = tls.make_subplots(2, 2)
 
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

fig9['data'] += data9
add_style(fig9)
fig9['layout'].update(
    yaxis1=pgo.YAxis(title='intercept'),
    yaxis3=pgo.YAxis(title='slope')
)
py.iplot(fig9, filename='bioassay')
 
posted @ 2015-06-27 18:45  菜鸡一枚  阅读(484)  评论(0编辑  收藏  举报