Skip to Content
《贝叶斯思维》,第二版
book

《贝叶斯思维》,第二版

by Allen B. Downey
May 2025
Beginner to intermediate
338 pages
3h 39m
Chinese
O'Reilly Media, Inc.
Content preview from 《贝叶斯思维》,第二版

第 18 章 共轭先验 共轭先验

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在前面几章中,我们使用网格近似法解决了各种问题。我的目标之一是证明这种方法足以解决许多实际问题。我认为这是一个很好的开始,因为它清楚地展示了这些方法是如何工作的。

然而,正如我们在上一章中所看到的,网格方法只能帮你到这里。随着参数数量的增加,网格中的点数也会呈指数级增长当参数超过 3-4 个时,网格方法就变得不切实际了

因此,在剩下的三章中,我将提出三种备选方案:

  1. 在本章中,我们将使用共轭先验来加速我们已经完成的一些计算。

  2. 在下一章中,我将介绍马尔科夫链蒙特卡罗(MCMC)方法,这种方法可以在合理的时间内解决数十个甚至数百个参数的问题。

  3. 在最后一章,我们将使用近似贝叶斯计算(ABC)来解决难以用简单分布建模的问题。

我们先从 "世界杯问题 "开始。

世界杯问题再探讨

第 8 章中,我们使用泊松过程将足球比赛中的进球建模为随机事件,这些事件在比赛过程中的任意时刻发生的可能性相同,从而解决了世界杯问题。

我们使用伽马分布来表示 λ 的先验分布。我们使用泊松分布来计算 k 的概率。

这是一个表示先验分布的伽马对象:

from scipy.stats import gamma

alpha = 1.4
dist = gamma(alpha)

这里是网格近似值:

import numpy as np
from utils import pmf_from_dist

lams = np.linspace(0, 10, 101)
prior = pmf_from_dist(dist, lams)

以下是lam 的每个可能值进 4 球的可能性:

from scipy.stats import poisson

k = 4
likelihood = poisson(lams).pmf(k)

以下是最新消息:

posterior = prior * likelihood
posterior.normalize()
0.05015532557804499

至此,我们应该对这个问题不陌生了。现在我们用共轭先验来解决同样的问题。

共轭先验

"伽玛分布 "一文我提出了使用伽玛分布作为先验的三个理由,并说我稍后会揭示第四个理由。那么,现在就是时候了。

我选择伽马分布的另一个原因是它是泊松分布的 "共轭先验",所谓 "共轭先验 "是因为 两个分布是相连或耦合的,这就是 "共轭 "的意思。

在下一节中,我将解释它们之间联系,但首先我会向你展示这种联系的结果,即有一种非常简单的方法来计算后验分布。

不过,为了证明这一点,我们必须将伽马分布的单参数版本转换为双参数版本。由于第一个参数被称为alpha ,你可能会猜到第二个参数被称为beta

下面的函数接收alphabeta ,并生成一个表示具有这些参数的伽马分布的对象:

def make_gamma_dist(alpha, beta):
    """Makes a gamma object."""
    dist = gamma(alpha, scale=1/beta)
    dist.alpha = alpha
    dist.beta = beta
    return dist

下面是alpha=1.4beta=1 的先验分布:

alpha = 1.4
beta = 1

prior_gamma = make_gamma_dist(alpha, beta)
prior_gamma ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python算法交易实战

Python算法交易实战

Posts & Telecom Press, Sebastien Donadio
Python数据分析基础

Python数据分析基础

Clinton W. Brownley

Publisher Resources

ISBN: 9798341659124