后验概率python示例

时间:2018-08-31 15:20:48

标签: python statistics bayesian

我一直在构建一个简单的近似贝叶斯计算应用程序,但遇到了一个问题。我不知道如何正确实施posterior probability

我的先前:非信息性(均匀分布)

生成模型:使用numpy二项分布进行随机是/否猜测

代码如下:

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)

谢谢!

2 个答案:

答案 0 :(得分:1)

感谢:this site

我的解决方案的完整代码是:

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)

让我产生这种可爱的可能性:

[a]

答案 1 :(得分:0)

我添加了一些更正,因此它将在第一次使用:

import math
from scipy import stats
from scipy.special import factorial
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

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)