Posterior Probability python example

11,472

thanks: this site

full code of my solution was:

import math
from scipy import stats
from scipy.special import factorial
from matplotlib import pyplot as plt

def likelihood(theta, n, x):
    return (factorial(n) / (factorial(x) * factorial(n - x))) * (theta  x) * ((1 - theta)  (n - x))

def pprob(prior, posterior, n_occured, n_events):
    return pd.Series(map(lambda theta: likelihood(theta, n_events, n_occured), prior))

def generative_model(n_events, p):
    return np.random.binomial(n_events, p)

def ABC(n_occured, n_events, n_draws=1000):
    prior = pd.Series(sorted(np.random.uniform(0, 1, size=n_draws)))
    sim_data = [generative_model(n_events ,p) for p in prior]
    posterior = prior[list(map(lambda x: x == n_occured, sim_data))]
    posterior_probability = pprob(prior, posterior, n_occured, n_events)

    # let's see what we got
    f, ax = plt.subplots(1)
    ax.plot(prior, posterior_probability)
    ax.set_xlabel("Theta")
    ax.set_ylabel("Likelihood")
    ax.grid()
    ax.set_title("Likelihood of Theta for New Campaign")
    plt.show()

ABC(10, 16)

produces me this cute likelihood:

[a]

Share:
11,472

Related videos on Youtube

user10300706
Author by

user10300706

Updated on June 04, 2022

Comments

  • user10300706
    user10300706 almost 2 years

    I've been building a simple Approximate Bayes Calculation application and ran into a problem. I don't know how to properly implement posterior probability.

    My prior: non-informative (uniform distribution)

    Generative model: random yes/no guessing implemented using numpy binomial distribution

    Here is the code:

    import numpy as np
    import pandas as pd
    
    def pprob():
        pass
    
    def generative_model(n_events, p):
        return np.random.binomial(n_events, p)
    
    def ABC(n_occured, n_events, n_draws=100000):
        prior = pd.Series(np.random.uniform(0, 1, size=n_draws))
        sim_data = [generative_model(n_events, p) for p in prior]
        posterior = prior[list(map(lambda x: x == n_occured, sim_data))]
        posterior_probability = pprob()
    
    ABC(10, 16)
    

    Thanks in advance!