贝叶斯推断
贝叶斯推断是结合有关模型或模型参数的先验知识来分析统计模型的过程。这种推断的根基是贝叶斯定理:
例如,假设我们有正态观测值
其中 sigma 是已知的,theta 的先验分布为
在此公式中,mu 和 tau(有时也称为超参数)也是已知的。如果观察 X 的 n 个样本,我们可以获得 theta 的后验分布
下图显示 theta 的先验、似然和后验。
rng(0,'twister');
n = 20;
sigma = 50;
x = normrnd(10,sigma,n,1);
mu = 30;
tau = 20;
theta = linspace(-40, 100, 500);
y1 = normpdf(mean(x),theta,sigma/sqrt(n));
y2 = normpdf(theta,mu,tau);
postMean = tau^2*mean(x)/(tau^2+sigma^2/n) + sigma^2*mu/n/(tau^2+sigma^2/n);
postSD = sqrt(tau^2*sigma^2/n/(tau^2+sigma^2/n));
y3 = normpdf(theta, postMean,postSD);
plot(theta,y1,'-', theta,y2,'--', theta,y3,'-.')
legend('Likelihood','Prior','Posterior')
xlabel('\theta')
汽车试验数据
在一些简单的问题中,例如前面的正态均值推断示例,很容易计算出封闭形式的后验分布。但是,在涉及非共轭先验的一般问题中,后验分布很难或不可能通过分析来进行计算。我们将以逻辑回归作为示例。此示例包含一个试验,以帮助建模不同重量的汽车在里程测试中的未通过比例。数据包括被测汽车的重量、汽车数量以及失败次数等观测值。我们采用一组经过变换的重量,以