条件概率与贝叶斯定理Conditional Probability & Bayes
门检测器报告"前面有扇门",机器人该信几分?答案不只取决于检测器有多准,还取决于走廊里本来就有多少门。贝叶斯定理把"先验信念"和"传感器的可靠性"合在一起,算出"看到这个读数之后我该相信什么"。这是所有概率定位、地图构建和滤波器的核心一步。 A door detector says "there is a door ahead". How much should the robot believe it? The answer depends not only on how accurate the detector is, but also on how many doors the corridor has to begin with. Bayes' theorem combines the prior belief with the sensor's reliability and returns what to believe after seeing the reading. It is the core step of every probabilistic localizer, mapper and filter.
1为什么机器人需要它Why robots need this
传感器给出的是读数,机器人想知道的是世界的状态。方向恰好相反:传感器的说明书告诉你"如果前面真有一扇门,检测器有 90% 的概率报告有门",即 \(p(z \mid x)\);机器人真正需要的却是"检测器刚刚报告有门,前面真有一扇门的概率是多少",即 \(p(x \mid z)\)。这两个数完全不是一回事。
贝叶斯定理就是把前者翻译成后者的公式。它几乎无处不在:
- 定位:机器人看到一扇门,更新"我在走廊哪个位置"的信念;
- 建图:激光打到障碍物,更新"这个栅格被占用"的概率;
- 目标检测与数据关联:检测网络给出 0.8 的置信度,它是不是真的?这得看这类物体平时多常见;
- 卡尔曼滤波、粒子滤波的"更新"一步,从头到尾就是贝叶斯定理(4.7、4.8、4.10)。
本节先在离散情形下把它彻底看懂:一个二选一的检测器、一条会让你"迷路"的走廊,再看连续读数的检测分数,最后看它在实际系统里最常见的一个坑:同一份证据被重复计算。
A sensor gives a reading; the robot wants the state of the world. The direction is reversed. The datasheet says "if there really is a door ahead, the detector reports a door 90% of the time", i.e. \(p(z \mid x)\). What the robot needs is "the detector just reported a door; what is the probability there is really one?", i.e. \(p(x \mid z)\). These two numbers are not the same thing at all.
Bayes' theorem is the formula that translates the former into the latter. It is everywhere:
- Localization: the robot sees a door and updates its belief about where in the corridor it is;
- Mapping: a laser hits an obstacle and the probability that a grid cell is occupied is updated;
- Object detection and data association: a detector says 0.8 confidence; is it real? That depends on how common such objects usually are;
- The "update" step of the Kalman and particle filters is Bayes' theorem from start to finish (4.7, 4.8, 4.10).
This lesson first makes the discrete case completely clear: a yes/no detector, then a corridor that can make you "lost". Then we look at a continuous detection score, and finally at the most common trap in real systems: counting the same evidence twice.
2直觉:用面积来数"哪一种可能"Intuition: counting "which possibility" with area
把走廊上机器人可能停下的所有位置画成一个单位正方形。先按左右分成两块:左边这一块是"墙前面是门"的位置,占比就是先验 \(p(\text{门})\);右边是"墙前面是墙"。然后把每一块再按上下分开:门这一块里,检测器报告"有门"的部分占 \(p(z \mid \text{门})\)(真阳性率);墙这一块里,检测器错报"有门"的部分占 \(p(z \mid \text{墙})\)(假阳性率)。
现在检测器响了。机器人知道自己落在两块"报告有门"的区域里的某处,但不知道是哪一块。门的那一块占了多少?这就是后验。关键在于:两块"报告有门"的区域面积是不等的,要比的是面积,而不是纵向的比例。拖动下面的三个手柄试试。
Draw every position where the robot might be standing as a unit square. Split it left/right: the left slab is positions that face a door, its share is the prior \(p(\text{door})\); the right slab faces a wall. Now split each slab top/bottom: within the door slab, the part where the detector says "door" has share \(p(z \mid \text{door})\) (true-positive rate); within the wall slab, the part where the detector wrongly says "door" has share \(p(z \mid \text{wall})\) (false-positive rate).
The detector fires. The robot knows it is somewhere in the two "says door" regions, but not which one. What share of them is the door slab? That is the posterior. The key point: the two "says door" regions have unequal areas, and it is the areas that must be compared, not the vertical fractions. Drag the three handles below.
- 被稀有性骗一次:先验 ≤ 5%、真阳性率 ≥ 90%、假阳性率 ≤ 10%。检测器很准,可后验竟然低于 50%。Get fooled by rarity: prior ≤ 5%, true-positive ≥ 90%, false-positive ≤ 10%. A good detector, yet the posterior drops below 50%.
- 保持先验 ≤ 5%,只靠降低假阳性率,让后验升到 80% 以上(需要把假阳性率压到 1% 左右)。Keep the prior ≤ 5% and raise the posterior above 80% by lowering only the false-positive rate (it has to drop to about 1%).
- 让真阳性率和假阳性率相等(差 < 3%):后验等于先验,这个传感器等于没装。Make the true- and false-positive rates equal (within 3%): the posterior equals the prior and the sensor is worthless.
用公式写出刚才看到的(\(x\) 是"前面有门",\(z\) 是"检测器报告有门"):
The formula for what you just saw (\(x\) is "a door is ahead", \(z\) is "the detector says door"):
提示:悬停公式里带底色的符号,FIG 1 的对应区域会高亮:先验是两块的宽度,似然是高度,证据 \(p(z)\) 是所有深色区域的总面积。Tip: hover the shaded symbols; the matching part of FIG 1 lights up. The prior is the slabs' widths, the likelihood the heights, and the evidence \(p(z)\) is the total dark area.
用"自然频数"来想最不容易错:1000 个位置里,20% 是门,共 200 扇,检测器报出 180 个;800 个是墙,10% 误报,80 个。一共响了 260 次,其中 180 次是真门,所以响了之后是门的概率是 \(180/260 \approx 69\%\),不是 90%。稀有的东西,即使检测器很准,误报也能淹没真报。
"Natural frequencies" are the safest way to think. Of 1000 positions, 20% are doors, 200 of them, and the detector flags 180. 800 are walls with 10% false alarms: 80. The detector fires 260 times, 180 of them at real doors, so the chance of a door given a firing is \(180/260 \approx 69\%\), not 90%. For rare things, even an accurate detector's false alarms can swamp the true hits.
3动手玩:一条让人迷路的走廊Play: a corridor that makes you lost
现在把贝叶斯用在定位上。环形走廊分成 16 格,有 5 扇门(黄色)。机器人不知道自己在哪格,所以信念一开始是均匀的。它有一个"门传感器":站在门前,以概率 \(p_{\text{hit}}\) 报告"门";站在墙前,以概率 \(p_{\text{false}}\) 错报"门"。
每次感知,更新规则是对每一格 \(i\) 做乘法再归一化:新信念 ∝ 似然 × 旧信念。每次前进一格,整个信念跟着平移一格(这里假定走得很准;加上运动噪声是 4.7 节的事)。目标:让信念在真实位置(蓝色圆点,但机器人自己并不知道)上积累起来。
先点"听到 门",看看均匀信念变成了什么样子;再试着前进几步,每走一步就感知一次。
Now use Bayes for localization. A circular corridor has 16 cells and 5 doors (amber). The robot does not know which cell it is in, so its belief starts uniform. It carries a "door sensor": in front of a door it says "door" with probability \(p_{\text{hit}}\); in front of a wall it falsely says "door" with probability \(p_{\text{false}}\).
Each sensing step multiplies every cell \(i\) by its likelihood and renormalizes: new belief ∝ likelihood × old belief. Each move shifts the whole belief one cell along (assuming perfect motion here; adding motion noise is the job of Lesson 4.7). The goal: make belief pile up on the true cell (the blue dot, which the robot itself does not know).
First press "Hear door" and see what the uniform belief turns into; then move a few steps, sensing at each.
- 在均匀信念上点一次"听到 门":信念变成了几个峰?为什么单凭一次读数定不了位?From the uniform belief press "Hear door" once: how many peaks appear, and why can one reading not localize you?
- 重置后,用"感知"和"前进"交替进行,让信念在真实位置上超过 80%。After a reset, alternate "Sense" and "Move" until the belief on the true cell exceeds 80%.
- 把 \(p_{\text{false}}\) 调到 0.30 以上,仍然把真实位置的信念推到 80% 以上。读数越不可靠,需要的步数越多。Raise \(p_{\text{false}}\) to 0.30 or more and still push the true cell's belief above 80%. The less reliable the sensor, the more steps it takes.
没有哪一个读数是决定性的:每个读数只是把信念乘上一个权重。几个并不可靠的读数乘在一起,却足以让一个峰从人群中突出来。这就是为什么机器人可以靠一堆噪声很大的传感器,把自己定位得很准。
No single reading is decisive: each one just multiplies the belief by a weight. Several unreliable readings multiplied together are enough to make one peak stand out from the crowd. That is how a robot can localize accurately from a pile of noisy sensors.
4第二个视角:连续的检测分数与后验曲线A second view: continuous detection scores and the posterior curve
真实的检测器很少只吐"有/无",而是给一个连续的分数 \(s\)(神经网络的置信度,或者特征匹配的内点数)。假设有路标时分数服从 \(\mathcal N(\mu_1, \sigma^2)\),没有路标时服从 \(\mathcal N(\mu_0, \sigma^2)\)(图中取 \(\mu_0 = 0\),\(\mu_1\) 可调)。这时"似然"是两条高斯曲线,贝叶斯定理逐点把它们换成后验概率 \(p(\text{路标} \mid s)\)。
神奇的是,后验随 \(s\) 变化的曲线是一条漂亮的 S 形(sigmoid)。更神奇的是:先验只是把这条 S 形曲线左右平移,并不改变形状。上图的两条曲线是"先验 × 似然"(面积已按先验缩放),下图是后验。
Real detectors rarely emit just "yes/no"; they give a continuous score \(s\) (a network's confidence, or the inlier count of a feature match). Suppose the score is \(\mathcal N(\mu_1, \sigma^2)\) when the landmark is present and \(\mathcal N(\mu_0, \sigma^2)\) when it is not (the figure uses \(\mu_0 = 0\) and lets you tune \(\mu_1\)). The "likelihoods" are now two Gaussian curves, and Bayes' theorem converts them point by point into the posterior \(p(\text{landmark} \mid s)\).
Remarkably, the posterior as a function of \(s\) is a neat S-shaped (sigmoid) curve. Even more remarkably, the prior merely slides this S-curve left or right without changing its shape. The upper plot shows "prior × likelihood" (each curve's area scaled by its prior); the lower plot shows the posterior.
- 把先验 \(\pi\) 降到 5% 以下,再把 \(s\) 拖到两个均值正中间(\(s = \mu_1/2\),默认是 1.5):后验是多少?阈值分数往哪边挪了?Lower the prior \(\pi\) below 5%, then drag \(s\) to the midpoint of the two means (\(s = \mu_1/2\), 1.5 by default): what is the posterior, and which way did the threshold move?
- 让后验从 10% 升到 90% 只需要 \(s\) 变化不到 1:增大均值间距或减小 \(\sigma\),使斜率 \((\mu_1-\mu_0)/\sigma^2 \ge 4.4\)。Make the posterior go from 10% to 90% over a change in \(s\) of less than 1: widen the mean gap or shrink \(\sigma\) so the slope \((\mu_1-\mu_0)/\sigma^2 \ge 4.4\).
把概率换成几率 \(\text{odds} = p/(1-p)\),再取对数,贝叶斯定理就变成加法:\(\ln\text{后验几率} = \ln\text{先验几率} + \ln\text{似然比}\)。对上面两条等方差的高斯,似然比的对数恰好是 \(s\) 的一次函数,所以后验是 sigmoid:\(1/(1+e^{-t})\),\(t\) 是线性的。这也是逻辑回归和神经网络最后一层 sigmoid 的来历。
Turn probability into odds, \(p/(1-p)\), and take logs: Bayes' theorem becomes addition, \(\ln\text{posterior odds} = \ln\text{prior odds} + \ln\text{likelihood ratio}\). For the two equal-variance Gaussians above the log likelihood ratio is exactly a linear function of \(s\), so the posterior is a sigmoid \(1/(1+e^{-t})\) with \(t\) linear. This is also where logistic regression and the final sigmoid of a neural network come from.
5再看数学The math
提示:悬停带底色的符号,FIG 1 中对应的区域会高亮。Tip: hover the shaded symbols and the matching region of FIG 1 lights up.
条件概率与乘法法则Conditional probability and the product rule
"在 \(B\) 已经发生的前提下 \(A\) 的概率",是把世界限制在 \(B\) 的范围内,再看 \(A\) 占多大比例:
"The probability of \(A\) given that \(B\) happened" restricts the world to \(B\) and asks what fraction of it is \(A\):
最后那个等号的两边都是同一块面积 \(P(A, B)\),只是切的方向不同。这就是 FIG 1 为什么能先左右分、再上下分,也能反过来。
Both sides of the last equality are the same area \(P(A, B)\), cut in different orders. That is why FIG 1 works whether you split left/right first and then top/bottom, or the other way around.
全概率公式与贝叶斯定理Total probability and Bayes' theorem
读数 \(z\) 可能由状态 \(x\) 的各种取值产生,把它们的贡献加起来(离散的 \(x\) 求和,连续的 \(x\) 积分),就是读数出现的总概率("证据")。把乘法法则的两种写法相除,得到贝叶斯定理:
A reading \(z\) can arise from any value of the state \(x\); add up all the contributions (a sum for discrete \(x\), an integral for continuous \(x\)) to get the total probability of the reading (the "evidence"). Dividing the two forms of the product rule gives Bayes' theorem:
| 名称Name | 符号Symbol | 机器人里的意思Robot meaning |
|---|---|---|
| 先验Prior | \(p(x)\) = \(\mathrm{bel}^-(x)\) | 读数到来之前我对状态的信念Belief about the state before the reading |
| 似然Likelihood | \(p(z \mid x)\) | 传感器模型:若状态是 \(x\),会读到 \(z\) 的概率(4.1 的光束模型就是它)Sensor model: chance of reading \(z\) if the state is \(x\) (the beam model of 4.1) |
| 证据Evidence | \(p(z)\) | 不管状态如何,读到 \(z\) 的总概率,只起归一化作用Total chance of reading \(z\) whatever the state is; it only normalizes |
| 后验Posterior | \(p(x \mid z)\) = \(\mathrm{bel}(x)\) | 看到读数之后我对状态的信念Belief after seeing the reading |
注意 \(\eta\) 不依赖 \(x\):在 FIG 2 里,每一格先乘似然,最后统一除以总和。你可以先忽略证据,算出"正比于后验"的数,最后再归一化。这也是 4.1 节"误区 3"里归一化的用处。
Note that \(\eta\) does not depend on \(x\): in FIG 2 every cell is multiplied by its likelihood and the result is divided by the grand total at the end. You may ignore the evidence, compute numbers proportional to the posterior, and normalize last. This is what Pitfall 3 of Lesson 4.1 was for.
几率形式与多个读数Odds form and several readings
只有两个假设(有门 / 没门)时,用几率更省事。如果多个读数在给定状态后条件独立,似然就直接相乘,这便是递推的"滤波器"形式:
With just two hypotheses (door / no door) odds are handier. If several readings are conditionally independent given the state, their likelihoods simply multiply, which is the recursive "filter" form:
验算 FIG 1 的默认值:先验 0.2(几率 1:4),真阳性率 0.9、假阳性率 0.1(似然比 9),所以后验几率是 \(9 \times 1/4 = 9/4\),后验 \(= 9/13 \approx 0.692\),与面积图一致。
Check the default of FIG 1: prior 0.2 (odds 1:4), true-positive 0.9 and false-positive 0.1 (likelihood ratio 9), so the posterior odds are \(9 \times 1/4 = 9/4\) and the posterior is \(9/13 \approx 0.692\), matching the area picture.
6机器人中的它:路标检测里的"重复计票"In robotics: "counting the same vote twice" in landmark detection
机器人每秒处理 30 帧图像,每帧都说"我看到了路标 A",每次单独看似然比都是 2。如果把它们当成独立证据一路乘下去,信念很快就会冲到 99.99%。可如果机器人是停着不动,或者摄像头缓冲区把同一帧重复送了好几次,这些"证据"其实是同一份信息的回声:它们的误差不是独立的,第二次看到的并没有带来新东西。
在 FIG 4 里,每一"帧"包含若干个"读数"。每帧只有第一个读数携带新信息,其余是回声。比较"天真地每个读数都乘一次"和"每帧只乘一次"的后验。
A robot processes 30 images a second, and each says "I see landmark A", each with a likelihood ratio of 2. Multiply them as independent evidence and the belief soars to 99.99% in no time. But if the robot is standing still, or the camera buffer re-sends the same frame several times, these "pieces of evidence" are echoes of one piece of information: their errors are not independent, and the second look brings nothing new.
In FIG 4 each "frame" contains several "readings". Only the first reading of a frame carries new information; the rest are echoes. Compare the posterior from "naively multiplying every reading" with "multiplying once per frame".
- 设 1 帧、每帧重复 8 次:天真的后验冲到 99% 以上,而真实证据只有一个读数。Set 1 frame repeated 8 times: the naive posterior shoots above 99% while the real evidence is a single reading.
- 把"每帧重复次数"设为 1:两条线重合,因为这时各读数确实独立。Set "repeats per frame" to 1: the two lines coincide because the readings really are independent then.
占据栅格建图 (occupancy grid) 的每个格子,维护的就是"被占用"的对数几率:每来一束激光,只需加上一个常数(命中加 \(+l_{\text{occ}}\),穿过加 \(l_{\text{free}}\)),非常便宜。同样的原因,SLAM 里要小心"回环检测"的证据被重复使用,滤波器里则要避免把同一个传感器数据既用来更新、又当作新的先验再用一次(这叫数据乱伦,data incest,分布式融合里尤其常见)。
Every cell of an occupancy grid keeps the log-odds of "occupied": each laser beam just adds a constant (a hit adds \(+l_{\text{occ}}\), a pass-through adds \(l_{\text{free}}\)), which is very cheap. For the same reason SLAM must take care not to reuse loop-closure evidence twice, and filters must avoid using the same sensor data both for an update and again as part of the next prior (known as data incest, especially common in distributed fusion).
7常见误区Common pitfalls
"检测器 99% 准确,所以它报告有门时,99% 是真的门。"这是忽略基础比率。传感器说明书给的是似然 \(p(z \mid x)\),机器人要的是后验 \(p(x \mid z)\),它们之间隔着先验。医生把"检测阳性"误当成"患病概率 99%"是同一个错误。
"The detector is 99% accurate, so when it says door, it is a door 99% of the time." This is base-rate neglect. The datasheet gives the likelihood \(p(z \mid x)\); the robot needs the posterior \(p(x \mid z)\); the prior sits between them. A doctor reading "tested positive" as "99% chance of being ill" makes exactly the same error.
\(p(x \mid z) \propto p(z \mid x)\,p(x)\):只要 \(p(x) = 0\),无论读数多么支持它,后验仍是 0。FIG 2 里如果某格的信念被压成 0(比如传感器偶尔出错,又把真实位置"否决"了),机器人就再也找不回自己。实践中永远给每个状态留一点点概率下限,也永远别把传感器模型写成 100% 准确。
\(p(x \mid z) \propto p(z \mid x)\,p(x)\): if \(p(x) = 0\), the posterior stays 0 however strongly the reading supports it. In FIG 2, if a cell's belief is crushed to 0 (say a rare sensor error "vetoes" the true position), the robot can never recover. In practice always leave every state a small probability floor, and never model a sensor as 100% accurate.
似然相乘的前提是读数在给定状态后条件独立。同一帧的多次处理、同一个传感器缓慢变化的偏差、几个传感器共用同一个错误的外参,都违反这一点。结果是信念过度自信:峰很尖,却是错的。FIG 4 是它最干净的版本。补救办法是只在独立的时间间隔上更新、给协方差加余量,或者显式建模相关性。
Multiplying likelihoods assumes the readings are conditionally independent given the state. Reprocessing the same frame, a slowly drifting bias in one sensor, or several sensors sharing the same wrong extrinsic calibration all violate this. The result is an overconfident belief: a sharp peak that may be wrong. FIG 4 is its cleanest version. Remedies: update only at independent intervals, inflate the covariance, or model the correlation explicitly.
似然 × 先验之后,所有格子加起来一般不是 1。不除以总和,信念就不是概率分布,下一次更新会越乘越小(或越大),最终下溢。归一化之前如果总和接近 0,说明读数与当前信念严重矛盾,这本身就是一个有用的"我迷路了"的信号。
After likelihood × prior, the cells generally do not add to 1. Skip the division and the belief is not a distribution; the next update shrinks (or grows) it again and again until it underflows. If the total before normalizing is nearly 0, the reading is in serious conflict with the current belief, which is itself a useful "I am lost" signal.
8小练习Check yourself
9延伸阅读Further reading
- S. Thrun, W. Burgard & D. Fox, Probabilistic Robotics, MIT Press, 2005, Sec. 2.3 — 贝叶斯规则与"门是否打开"的经典例子;第 4 章讲离散的直方图滤波,第 7 章是用它做定位Bayes rule and the classic "is the door open" example; Ch. 4 covers the discrete histogram filter and Ch. 7 uses it for localization
- 3Blue1Brown, Bayes theorem, the geometry of changing beliefs (video) — 用面积图讲贝叶斯,和本节 FIG 1 的思路一致Bayes explained with area diagrams, in the same spirit as FIG 1
- G. Gigerenzer & U. Hoffrage, "How to Improve Bayesian Reasoning Without Instruction: Frequency Formats", Psychological Review, 1995 — 为什么"自然频数"比百分比好懂Why natural frequencies are easier to reason with than percentages
- T. D. Barfoot, State Estimation for Robotics, Cambridge University Press, 2017, Ch. 2 — 贝叶斯推断与估计的概率论基础The probability background of Bayesian inference and estimation
- C. M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006, Sec. 1.2.3 — 贝叶斯观点下的概率Probabilities from a Bayesian viewpoint