期望、方差与协方差Expectation, Variance & Covariance
一整个分布太"重"了,机器人通常只想用几个数概括它:均值回答"最好的单点猜测是什么",方差回答"这个猜测有多不稳",协方差回答"两个误差是同涨同落,还是此消彼长"。把协方差排成矩阵,它在平面上就是一个椭圆:椭圆的朝向和长短,就是机器人"对自己位置的不确定性"的形状。 A whole distribution is heavy, and a robot usually wants just a few numbers to summarize it: the mean answers "what is the best single guess", the variance "how shaky is that guess", and the covariance "do two errors rise and fall together, or trade off". Arranged in a matrix, the covariance is an ellipse in the plane, and the ellipse's orientation and size are the shape of the robot's uncertainty about its own position.
1为什么机器人需要它Why robots need this
机器人的状态估计器每一步吐出的,不是一个位置,而是一个位置加一个"可信度":均值 \(\hat{\mathbf x}\),以及告诉你它大概拿不准多少、往哪个方向拿不准的协方差 \(\Sigma\)。规划器要根据 \(\Sigma\) 判断"这条路离障碍物够不够远";传感器融合要根据 \(\Sigma\) 决定"该信 GPS 还是该信里程计";卡尔曼滤波的整个递推,就是在不断地更新这一对 \((\hat{\mathbf x}, \Sigma)\)。
协方差里最有用、也最容易被忽视的是非对角线元素。位置的 \(x\) 和 \(y\) 误差如果不相关,不确定性是一个"正放"的椭圆;如果相关,椭圆就是斜的:这时知道了 \(x\) 的误差,就能顺便推断出 \(y\) 的误差。4.8 节里"只测位置、速度也变准了"的魔术,全靠这几个非对角元素。
A robot's state estimator emits not just a position but a position plus a measure of how far to trust it: a mean \(\hat{\mathbf x}\) and a covariance \(\Sigma\) saying roughly how unsure it is and in which direction. A planner reads \(\Sigma\) to decide "is this path far enough from the obstacle"; sensor fusion reads \(\Sigma\) to decide "trust the GPS or the odometry"; the entire Kalman recursion is an endless update of the pair \((\hat{\mathbf x}, \Sigma)\).
The most useful, and most overlooked, part of the covariance is its off-diagonal entries. If the \(x\) and \(y\) errors are uncorrelated the uncertainty is an upright ellipse; if they are correlated the ellipse is tilted, and knowing the \(x\) error lets you infer part of the \(y\) error. The trick of Lesson 4.8, "measure position only and velocity gets better too", lives entirely in a few off-diagonal entries.
2直觉:均值是平衡点,方差是散开的程度Intuition: the mean is a balance point, variance is spread
把分布想成一根杠杆:每根柱子是一个砝码,位置是 \(x\),重量是概率 \(p(x)\)。期望(均值)\(E[X]\) 就是让杠杆保持平衡的支点位置:\(\sum x\,p(x)\)。离支点远的砝码"力矩"更大,所以一个很远但很轻的柱子,也能把均值拉过去不少。
均值不是"最可能的值"(众数),也不是"中间的值"(中位数),三者只在对称单峰分布里重合。拖动下面的柱子,再拖动那个"猜测" \(c\):如果你要最小化平方误差,最优的猜测是均值;如果要最小化绝对误差,最优的猜测是中位数。
Think of a distribution as a lever: each bar is a weight at position \(x\) with mass \(p(x)\). The expectation (mean) \(E[X]\) is the fulcrum position that balances the lever: \(\sum x\,p(x)\). Weights far from the fulcrum have more torque, so even a faraway but light bar can pull the mean a long way.
The mean is neither "the most likely value" (the mode) nor "the middle value" (the median); the three coincide only for symmetric unimodal distributions. Drag the bars below, and then the "guess" \(c\): to minimize squared error the best guess is the mean, to minimize absolute error it is the median.
- 选"两簇"预设:均值恰好落在一个概率几乎为 0 的位置。这个"期望值"机器人永远不会真的观测到。Pick the "two clusters" preset: the mean lands on a spot with almost zero probability. The robot will never actually observe this "expected value".
- 做出一个右偏分布,使均值比中位数大 1 以上:一个很远的轻柱子拽走了均值,却没怎么动中位数。Build a right-skewed distribution whose mean is more than 1 above the median: a faraway light bar drags the mean but barely moves the median.
- 把猜测 \(c\) 拖到均值上(误差 < 0.1),看平方损失降到最低;再拖到中位数,看绝对损失最低。Drag the guess \(c\) onto the mean (within 0.1) and watch the squared loss hit its minimum; then onto the median for the absolute loss.
均值是平方误差意义下的最佳单点猜测,这正是最小二乘(1.13)和卡尔曼滤波"最小均方误差估计"的根源。而它的代价就是对离群点敏感:一个很远的值,因为"平方"被放大了。方差正是这个最小的平方误差:\(\mathrm{Var}(X) = \min_c E[(X-c)^2]\)。
The mean is the best single guess in the squared-error sense, which is the root of least squares (1.13) and of the Kalman filter's "minimum mean-square error" estimate. The price is sensitivity to outliers: a faraway value is magnified by the square. And the variance is exactly that minimal squared error: \(\mathrm{Var}(X) = \min_c E[(X-c)^2]\).
3动手玩:方差是"平均方块"Play: variance is the "average square"
方差 \(\mathrm{Var}(X) = E[(X-\mu)^2]\) 有一个很好的几何意义:把每个样本到均值的距离当作正方形的边长,方差就是这些正方形面积的平均值;标准差 \(\sigma\) 则是"平均方块"的边长。距离平方使得远处的点贡献大得多:距离翻倍,面积是四倍。
下面从 \(\mathcal N(0, \sigma^2)\) 里抽样,每个点长出一个方块。注意样本很少时,样本方差抖得厉害,甚至系统性偏小:那是除以 \(N\) 还是 \(N-1\) 的问题,下方的"重复实验"会告诉你答案。
The variance \(\mathrm{Var}(X) = E[(X-\mu)^2]\) has a neat geometric meaning: take each sample's distance to the mean as the side of a square, and the variance is the average area of those squares; the standard deviation \(\sigma\) is the side of the "average square". Squaring makes far points count much more: double the distance, quadruple the area.
Below we draw samples from \(\mathcal N(0, \sigma^2)\) and grow a square on each point. With few samples the sample variance jitters wildly and is even systematically small: whether to divide by \(N\) or \(N-1\) is the question, and the "repeated experiment" readout settles it.
- 把样本数降到 4 以下,多点几次"换一组样本":平均方块的大小跳动多大?Drop the sample count to 4 or less and press "New samples" several times: how much does the average square jump around?
- N = 5 时,把"除以"在 N 和 N−1 之间切换,对照"重复实验"那一行:哪一个平均起来等于真实的 \(\sigma^2\)?With N = 5 toggle "Divide by" between N and N−1 and compare with the "repeated experiment" row: which one averages out to the true \(\sigma^2\)?
样本方差用的是"到样本均值的距离",而样本均值本身就是这几个点的"重心",天然离它们更近,所以平均方块偏小,除以 \(N\) 会系统性低估。除以 \(N-1\)(贝塞尔修正)刚好补回来,期望正好是 \(\sigma^2\)。当 \(N\) 很大时,两者几乎没差别。
The sample variance measures distances to the sample mean, and the sample mean is the "center of gravity" of those very points, so they sit unusually close to it. The average square comes out small, and dividing by \(N\) underestimates systematically. Dividing by \(N-1\) (Bessel's correction) repairs that exactly: the expectation is \(\sigma^2\). For large \(N\) the difference vanishes.
4第二个视角:协方差矩阵就是一个椭圆A second view: the covariance matrix is an ellipse
两个变量时,方差的"方块"变成"长方形":\(\mathrm{Cov}(X, Y) = E[(X-\mu_x)(Y-\mu_y)]\) 是每个点 \((x, y)\) 与均值之间那个带符号的长方形面积的平均。点落在右上或左下象限,长方形面积为正;落在左上或右下,为负。如果正的更多,\(X\) 和 \(Y\) 就倾向于同涨同落,协方差为正,点云向右上方倾斜。
把 \(\sigma_x^2, \sigma_y^2, \mathrm{Cov}\) 摆成矩阵 \(\Sigma\),点云的形状就由它决定:椭圆的两条主轴是 \(\Sigma\) 的特征向量(1.11 节),半轴长是特征值的平方根。再选一个单位方向 \(\mathbf u\),点云投影到这个方向上的方差是 \(\mathbf u^\top \Sigma\, \mathbf u\)(点积,1.7 节)。拖动椭圆的两个把手改变形状,再拖动那个转盘上的方向把手。
With two variables the variance's "square" becomes a "rectangle": \(\mathrm{Cov}(X, Y) = E[(X-\mu_x)(Y-\mu_y)]\) is the average of the signed rectangle area between each point \((x, y)\) and the mean. Points in the upper-right or lower-left quadrant give positive area, upper-left or lower-right negative. If positives dominate, \(X\) and \(Y\) tend to rise and fall together, the covariance is positive, and the cloud tilts up and to the right.
Arrange \(\sigma_x^2, \sigma_y^2, \mathrm{Cov}\) into a matrix \(\Sigma\) and it dictates the cloud's shape: the ellipse's two principal axes are the eigenvectors of \(\Sigma\) (Lesson 1.11), and its semi-axes are the square roots of the eigenvalues. Pick a unit direction \(\mathbf u\), and the variance of the cloud projected onto it is \(\mathbf u^\top \Sigma\, \mathbf u\) (a dot product, Lesson 1.7). Drag the ellipse's two handles to reshape it, then the direction handle on the dial.
- 做出强负相关:\(\rho \le -0.7\)。点云向哪边倾斜?Make a strong negative correlation: \(\rho \le -0.7\). Which way does the cloud tilt?
- 让 \(x\)、\(y\) 不相关(\(|\rho| < 0.03\)),但方差差别很大(比值 > 2):椭圆摆正,只是被拉扁。Make \(x\) and \(y\) uncorrelated (\(|\rho| < 0.03\)) with very different variances (ratio > 2): the ellipse is upright, just squashed.
- 把方向 \(\mathbf u\) 转到主轴上:投影方差等于最大特征值 \(\lambda_1\)。转到垂直方向,则等于 \(\lambda_2\)。Rotate \(\mathbf u\) onto the major axis: the projected variance equals the largest eigenvalue \(\lambda_1\). Perpendicular to it you get \(\lambda_2\).
协方差矩阵把"在每个方向上有多不确定"装进了一个矩阵:沿方向 \(\mathbf u\) 的方差是 \(\mathbf u^\top\Sigma\mathbf u\)。最不确定的方向是第一特征向量,最确定的是最后一个。只看 \(\sigma_x\)、\(\sigma_y\) 会漏掉倾斜:同样的 \(\sigma_x, \sigma_y\),不同的 \(\rho\),是完全不同的椭圆。
The covariance matrix packs "how uncertain am I in each direction" into one matrix: the variance along \(\mathbf u\) is \(\mathbf u^\top\Sigma\mathbf u\). The most uncertain direction is the first eigenvector, the least uncertain the last. Looking only at \(\sigma_x\) and \(\sigma_y\) misses the tilt: the same \(\sigma_x, \sigma_y\) with a different \(\rho\) is an entirely different ellipse.
不相关,不等于独立Uncorrelated is not independent
相关系数 \(\rho\) 只度量线性关系。把 \(y\) 完全由 \(x\) 决定的关系(比如抛物线、圆)放进来,\(\rho\) 可以恰好是 0。用下面这个小实验对比 \(\rho\) 与一个能捕捉任意形状依赖的量:互信息(把平面切成 8×8 的格子,用频数估计;"基线"是把 \(y\) 随机打乱后的值,代表"真正独立"时估计误差带来的那一点点)。
The correlation coefficient \(\rho\) measures only linear relationships. Put in a relation where \(y\) is completely determined by \(x\) (a parabola, a circle) and \(\rho\) can come out exactly 0. The mini-lab below compares \(\rho\) with a quantity that catches dependence of any shape: mutual information (the plane cut into 8×8 cells and estimated from counts; the "baseline" is the value after randomly shuffling \(y\), i.e. the small amount that estimation error adds when truly independent).
- 找到一种数据:\(|\rho| < 0.1\),可互信息却远高于基线(3 倍以上)。Find a dataset with \(|\rho| < 0.1\) yet mutual information far above the baseline (3× or more).
5再看数学The math
提示:悬停带底色的符号,FIG 1、FIG 2、FIG 3 中对应的元素会高亮。Tip: hover the shaded symbols and the matching elements of FIG 1, FIG 2 and FIG 3 light up.
期望Expectation
第一行前两式分别是离散情形与连续情形(\(f\) 是概率密度)。第二行的线性是概率里最好用的性质:不需要知道 \(X, Y\) 的联合分布,独立与否都成立。机器人里,\(E[A\mathbf x + \mathbf b] = A\,E[\mathbf x] + \mathbf b\),这就是"均值穿过线性映射"(4.5)。
The first two formulas of the first line are the discrete and the continuous case (\(f\) is the density). The linearity in the second line is the most useful property in probability: no knowledge of the joint distribution of \(X, Y\) is needed, and it holds whether or not they are independent. For robots, \(E[A\mathbf x + \mathbf b] = A\,E[\mathbf x] + \mathbf b\), which is "the mean passes through a linear map" (Lesson 4.5).
方差与协方差Variance and covariance
独立 \(\Rightarrow\) 协方差为 0,反过来不成立(FIG 4)。最后一行说明:两个误差相加时,方差是否相加,取决于它们是否相关。若 \(\rho = -1\),一个变大必伴随另一个变小,它们的和甚至可以完全抵消;这正是两个传感器误差负相关时可以融合得特别好的原因。
Independent \(\Rightarrow\) zero covariance, but not conversely (FIG 4). The last line says: when two errors add, whether their variances add depends on their correlation. With \(\rho = -1\) one rising forces the other falling, and the sum can cancel entirely; this is why two sensors with negatively correlated errors can fuse especially well.
协方差矩阵The covariance matrix
对随机向量 \(\mathbf x \in \mathbb R^n\),把所有两两协方差排成矩阵:
For a random vector \(\mathbf x \in \mathbb R^n\), arrange all pairwise covariances in a matrix:
\(\Sigma\) 对称、半正定(任何方向的方差都不为负)。对称矩阵可以对角化(1.11):\(Q\) 的列是主轴方向,\(\Lambda = \mathrm{diag}(\lambda_1, \lambda_2)\) 是沿主轴的方差。第二行(点积、二次型)说明沿任一方向的方差都夹在两个特征值之间,最大值在主轴上取到。
\(\Sigma\) is symmetric and positive semi-definite (no direction has negative variance). A symmetric matrix diagonalizes (1.11): the columns of \(Q\) are the principal directions and \(\Lambda = \mathrm{diag}(\lambda_1, \lambda_2)\) are the variances along them. The second line (a dot product, a quadratic form) says the variance along any direction lies between the two eigenvalues, attaining the maximum on the principal axis.
把 \(N\) 个样本的 \(x\) 减去均值,排成一个 \(N\) 维向量 \(\tilde{\mathbf x}\);\(y\) 同理得 \(\tilde{\mathbf y}\)。则 \(\rho = \tilde{\mathbf x}\cdot\tilde{\mathbf y} / (\|\tilde{\mathbf x}\|\|\tilde{\mathbf y}\|)\),正是 1.7 节的夹角余弦。\(\rho = \pm 1\) 就是两个向量共线,\(\rho = 0\) 就是它们垂直。这也解释了为什么 \(|\rho| \le 1\)(柯西-施瓦茨不等式)。
Subtract the mean from the \(x\) of \(N\) samples and stack them into an \(N\)-dimensional vector \(\tilde{\mathbf x}\); likewise \(\tilde{\mathbf y}\). Then \(\rho = \tilde{\mathbf x}\cdot\tilde{\mathbf y} / (\|\tilde{\mathbf x}\|\|\tilde{\mathbf y}\|)\), exactly the cosine of Lesson 1.7. \(\rho = \pm 1\) means the vectors are collinear, \(\rho = 0\) that they are perpendicular. This also explains \(|\rho| \le 1\) (Cauchy–Schwarz).
6机器人中的它:定位误差的"形状"In robotics: the "shape" of a localization error
室内的 UWB 定位、或者靠两个已知的信标做距离测量,是一个非常干净的例子:机器人量到自己到信标 A 和 B 的距离 \(r_1, r_2\),每个距离有高斯噪声 \(\sigma_r\),然后求两个圆的交点作为位置。每次测量得到一个交点,重复很多次,就得到一团点云。它的形状取决于信标的几何:两个信标与机器人夹角接近 90° 时,点云接近圆;两个信标几乎在同一条射线上时,点云在垂直于这条射线的方向上被拉得很长:两个圆在机器人处几乎相切,两次测距都只约束了沿射线的距离,沿切线方向几乎没有约束。这正是 GPS 里的"几何精度因子" (DOP)。
图里同时画出样本协方差(直接从点云算出,黄色)和用雅可比算出的理论预测 \(\sigma_r^2 (J^\top J)^{-1}\)(紫色虚线,下一节 4.5 的内容);噪声不大时它们重合。拖动信标和机器人。
Indoor UWB positioning, or measuring ranges to two known beacons, is a very clean example. The robot measures its distances \(r_1, r_2\) to beacons A and B, each with Gaussian noise \(\sigma_r\), and takes the intersection of the two circles as its position. Each measurement gives one intersection; repeat many times and you get a cloud. Its shape depends on the beacon geometry: when the two beacons are about 90° apart as seen from the robot the cloud is nearly round; when they are almost on the same ray the cloud is stretched long perpendicular to that ray: the two circles are nearly tangent at the robot, both ranges constrain only the distance along the ray, and almost nothing constrains the tangent direction. This is the "dilution of precision" (DOP) of GPS.
The figure draws both the sample covariance (computed straight from the cloud, amber) and the theoretical prediction \(\sigma_r^2 (J^\top J)^{-1}\) from a Jacobian (dashed purple, the topic of the next lesson, 4.5); for small noise they coincide. Drag the beacons and the robot.
- 把两个信标摆在机器人同一侧、几乎同一方向(夹角 < 15°):椭圆被拉成一条长线。谁的方差大?Put the two beacons on the same side, in almost the same direction from the robot (angle < 15°): the ellipse is stretched into a long line. Which variance explodes?
- 再把夹角调到接近 90°(85° 到 95°):椭圆接近圆,精度最好。Now bring the angle close to 90° (85° to 95°): the ellipse is nearly round and accuracy is best.
GPS 接收机会给出 HDOP/VDOP 数字,就是这里"几何放大"的程度:卫星分布得越开,DOP 越小。规划器用估计的 \(\Sigma\) 给轨迹加"安全膨胀"(让机器人在不确定性椭圆大的地方绕远些);SLAM 的"信息矩阵" \(\Sigma^{-1}\) 里,哪个方向的信息量大、哪个方向弱,一目了然。
A GPS receiver reports HDOP/VDOP numbers: exactly this "geometric amplification". The more spread out the satellites, the smaller the DOP. Planners use the estimated \(\Sigma\) to inflate trajectories (staying further from obstacles where the ellipse is large); in SLAM's "information matrix" \(\Sigma^{-1}\) you can see at a glance which directions are well constrained and which are weak.
7常见误区Common pitfalls
两簇分布的均值落在两簇之间的空地上,那里的概率几乎为 0(FIG 1)。机器人若在"两种可能"(比如面前是左门还是右门)之间拿不准,把两者平均成"中间"去规划,就会一头撞在墙上。多峰分布不要只用均值和方差概括。
The mean of a two-cluster distribution sits in the empty gap between them, where the probability is almost 0 (FIG 1). A robot torn between "two possibilities" (say the left door or the right door) that averages them into "the middle" for planning will drive into the wall. Do not summarize multimodal distributions by mean and variance alone.
"\(\sigma_x = 0.3\),\(\sigma_y = 0.3\),位置很准。"如果 \(\rho = 0.95\),椭圆沿 45° 方向的半轴长是 \(0.3\sqrt{1.95} \approx 0.42\),垂直方向仅 \(0.3\sqrt{0.05} \approx 0.07\)。实际精度取决于你关心的方向。反过来,丢掉非对角元素(假设各分量独立)会让滤波器对相关的误差过度自信。
"\(\sigma_x = 0.3\), \(\sigma_y = 0.3\), so the position is accurate." With \(\rho = 0.95\) the semi-axis along 45° is \(0.3\sqrt{1.95} \approx 0.42\) while across it only \(0.3\sqrt{0.05} \approx 0.07\). The real accuracy depends on the direction you care about. Conversely, dropping the off-diagonals (assuming independent components) makes a filter overconfident about correlated errors.
不相关只说明没有线性关系(FIG 4)。相关也不说明谁影响谁:机器人估计的 \(x\) 与 \(y\) 误差相关,通常只是因为它们共享同一个航向误差。
Uncorrelated only rules out a linear relationship (FIG 4). Correlated does not say who influences whom: the robot's estimated \(x\) and \(y\) errors are usually correlated simply because they share the same heading error.
用很少的样本估计方差(比如标定传感器噪声只测了 5 次)时,除以 \(N\) 会系统性偏小(FIG 2);还要记得,样本均值和方差本身也有抖动,几个样本给出的 \(\sigma\) 可能偏离真值一大截。
When estimating a variance from very few samples (say calibrating sensor noise from just 5 readings), dividing by \(N\) is biased low (FIG 2); remember also that the sample mean and variance themselves jitter, so a handful of samples can give a \(\sigma\) far from the truth.
8小练习Check yourself
9延伸阅读Further reading
- S. Thrun, W. Burgard & D. Fox, Probabilistic Robotics, MIT Press, 2005, Sec. 2.2 — 期望、协方差在机器人估计里的写法Expectation and covariance as used in robot estimation
- T. D. Barfoot, State Estimation for Robotics, Cambridge University Press, 2017, Ch. 2 — 矩、协方差、相关性的矩阵形式Moments, covariance and correlation in matrix form
- C. M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006, Sec. 1.2.2 — 期望与协方差,含向量情形Expectations and covariances, including the vector case
- J. Blitzstein & J. Hwang, Introduction to Probability, CRC Press — 第 4 章(期望)与第 7 章(联合分布、协方差与相关)Ch. 4 (expectation) and Ch. 7 (joint distributions, covariance and correlation)
- G. Strang, Introduction to Linear Algebra, Wellesley–Cambridge Press — 特征值与对称矩阵:理解协方差椭圆的线性代数基础Eigenvalues and symmetric matrices: the linear algebra behind covariance ellipses