贝叶斯分析——从数值积分到MCMC

161 篇文章 8 订阅

Analytical solution

Numerical integration

One simple way of numerical integration is to estimate the values on a grid of values for θ . To calculate the posterior, we find the prior and the likelhood for each value of θ , and for the marginal likelhood, we replace the integral with the equivalent sum

p(X)=θP(θ)P(X|θ)

One advantage of this is that the prior does not have to be conjugate (although the example below uses the same beta prior for ease of comaprsion), and so we are not restricted in our choice of an approproirate prior distribution. For example, the prior can be a mixture distribution or estimated empirically from data. For example, the prior can be a mixture distribution or estimated empirically from data. The disadvantage, of course, is that this is computationally very expenisve when we need to esitmate multiple parameters, since the number of grid points grows as O(nd) , wher n defines the grid resolution(thetas=np.arange(0, 1, .001)) and d is the size of θ=[θ[0],θ[1],gi] .

import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt

a, b = 10, 10
n, k = 100, 61

thetas = np.arange(0, 1, .001)
prior = st.beta(a, b)
poster = prior.pdf(thetas) * st.binom(n, thetas).pmf(k)
                    # 
poster /= (poser.sum()/len(thetas))
                    # ∑p(θ)p(x|θ)
plt.plot(thetas, prior.pdf(thetas), 'b', lw=2, label='Prior')
plt.plot(thetas, n*st.binom(n, thetas).pmf(k), 'g', lw=2, label='Likelihood')
plt.plot(thetas, poster, 'r', lw=2, label='Posterior')



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值