用Python计算模型证据/边缘
我的问题涉及到贝叶斯推理,以及如何在给定一些数据和先验分布和后验分布的情况下对模型证据进行数值计算 给定共轭先验,模型证据指定如下: 其中σ和β为参数,m为模型,Y为数据,X为先验值 鉴于以下设置,我如何计算模型证据?我需要返回一个标量数的东西。 下面我有一个生成一些数据(从法线中提取)并假设先验(法线)和似然函数(高斯)的最小工作示例。请注意,数据的PDF和之前的PDF是如何(近似)集成到一个的,而似然函数的值可以大于1 我主要困惑于如何“整合”模型中的参数,从而考虑模型的复杂性。如果你能写下对数似然函数,我就能从解析的角度看出来。但我真的看不出这是如何产生一个标量数的用Python计算模型证据/边缘,python,statistics,bayesian,Python,Statistics,Bayesian,我的问题涉及到贝叶斯推理,以及如何在给定一些数据和先验分布和后验分布的情况下对模型证据进行数值计算 给定共轭先验,模型证据指定如下: 其中σ和β为参数,m为模型,Y为数据,X为先验值 鉴于以下设置,我如何计算模型证据?我需要返回一个标量数的东西。 下面我有一个生成一些数据(从法线中提取)并假设先验(法线)和似然函数(高斯)的最小工作示例。请注意,数据的PDF和之前的PDF是如何(近似)集成到一个的,而似然函数的值可以大于1 我主要困惑于如何“整合”模型中的参数,从而考虑模型的复杂性。如果你能写
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import scipy
import seaborn as sns
sns.set(style="white", palette="muted", color_codes=True)
%matplotlib inline
mu = 0
variance = 1
sigma = np.sqrt(variance)
data = np.random.normal(mu,variance,100)
x = np.linspace(-5,5,100)
density = scipy.stats.kde.gaussian_kde(data)
data_pdf = density(x)
prior_pdf = scipy.stats.norm.pdf(x, mu, sigma)
likelihood = np.exp(-np.power(x - mu, 2.) / (2 * np.power(sigma, 2.)))
I1=scipy.integrate.trapz(data_pdf,x)
I2=scipy.integrate.trapz(prior_pdf,x)
I3=scipy.integrate.trapz(likelihood,x)
fig1 = plt.figure(figsize=(7.5,5))
ax1 = fig1.add_subplot(3,1,1)
sns.despine(right=True)
ax1.plot(x,data_pdf,'k')
ax1.legend([r'$PDF(Data)$'],loc='upper left')
ax2 = fig1.add_subplot(3,1,2)
sns.despine(right=True)
ax2.plot(x,prior_pdf,'b')
ax2.legend([r'$Prior$'],loc='upper left')
ax3 = fig1.add_subplot(3,1,3)
sns.despine(right=True)
ax3.plot(x,likelihood,'r')
ax3.legend([r'$Likelihood$'],loc='upper left')
plt.tight_layout()
print(I1,I2,I3)
您可能需要使用
conjugate\u previor
包