均值方差增量计算

单次计算

μ = ∑ i = 1 n x i n \mu = \frac{\sum_{i=1}^{n} x_i}{n} μ=n∑i=1nxi
σ 2 = ∑ i = 1 n ( x i − μ ) 2 n = ∑ i = 1 n x i 2 − 2 ∑ i = 1 n x i μ + n μ 2 n = ∑ i = 1 n x i 2 − n μ 2 n = ∑ i = 1 n x i 2 n − μ 2 \begin{array}{ll} \sigma^2 &= \frac{\sum_{i=1}^{n}(x_i - \mu)^2}{n} \\ &= \frac{\sum_{i=1}^{n} x_i^2 -2\sum_{i=1}^{n} x_i\mu + n\mu^2}{n} \\ &= \frac{\sum_{i=1}^{n} x_i^2 - n\mu^2}{n} \\ &= \frac{\sum_{i=1}^{n} x_i^2}{n} - \mu^2 \end{array} σ2=n∑i=1n(xi−μ)2=n∑i=1nxi2−2∑i=1nxiμ+nμ2=n∑i=1nxi2−nμ2=n∑i=1nxi2−μ2

增量计算

指标 第一批次 第二批次 合并
总数 n 1 n_1 n1 n 2 n_2 n2 n 1 + n 2 n_1+n_2 n1+n2
均值 μ 1 \mu_1 μ1 μ 2 \mu_2 μ2 n 1 μ 1 + n 2 μ 2 n 1 + n 2 \frac{n_1 \mu_1 + n_2\mu_2}{n_1 + n_2} n1+n2n1μ1+n2μ2
方差 σ 1 \sigma_1 σ1 σ 2 \sigma_2 σ2 ?
∑ x i 2 \sum x_i^2 ∑xi2 n 1 σ 1 2 + n 1 μ 1 2 n_1 \sigma_1^2 + n_1 \mu_1^2 n1σ12+n1μ12 n 2 σ 2 2 + n 2 μ 2 2 n_2 \sigma_2^2 + n_2 \mu_2^2 n2σ22+n2μ22 n 1 σ 1 2 + n 1 μ 1 2 + n 2 σ 2 2 + n 2 μ 2 2 n_1 \sigma_1^2 + n_1 \mu_1^2 + n_2 \sigma_2^2 + n_2 \mu_2^2 n1σ12+n1μ12+n2σ22+n2μ22

σ 2 = ∑ i = 1 n x i 2 n − μ 2 = n 1 σ 1 2 + n 1 μ 1 2 + n 2 σ 2 2 + n 2 μ 2 2 n 1 + n 2 − ( n 1 μ 1 + n 2 μ 2 n 1 + n 2 ) 2 = ( n 1 + n 2 ) ( n 1 σ 1 2 + n 1 μ 1 2 + n 2 σ 2 2 + n 2 μ 2 2 ) − ( n 1 μ 1 + n 2 μ 2 ) 2 ( n 1 + n 2 ) 2 = n 1 σ 1 2 + n 2 σ 2 2 n 1 + n 2 + n 1 n 2 μ 1 2 + n 1 n 2 μ 2 2 − 2 n 1 n 2 μ 1 μ 2 ( n 1 + n 2 ) 2 = n 1 σ 1 2 + n 2 σ 2 2 n 1 + n 2 + n 1 n 2 ( μ 1 − μ 2 ) 2 ( n 1 + n 2 ) 2 \begin{array}{ll} \sigma^2 &= \frac{\sum_{i=1}^{n} x_i^2}{n} - \mu^2 \\ &= \frac{n_1 \sigma_1^2 + n_1 \mu_1^2 + n_2 \sigma_2^2 + n_2 \mu_2^2}{n_1+n_2} - (\frac{n_1 \mu_1 + n_2\mu_2}{n_1 + n_2})^2 \\ &= \frac{(n_1 + n_2)(n_1 \sigma_1^2 + n_1 \mu_1^2 + n_2 \sigma_2^2 + n_2 \mu_2^2) - (n_1 \mu_1 + n_2\mu_2)^2}{(n_1 + n_2)^2} \\ &= \frac{n_1 \sigma_1^2 + n_2 \sigma_2^2}{n_1 + n_2} + \frac{ n_1n_2\mu_1^2 + n_1n_2\mu_2^2 - 2n_1n_2\mu_1\mu_2}{(n_1 +n_2)^2} \\ &= \frac{n_1 \sigma_1^2 + n_2 \sigma_2^2}{n_1 + n_2} + \frac{ n_1n_2(\mu_1 - \mu_2)^2 }{(n_1 +n_2)^2} \end{array} σ2=n∑i=1nxi2−μ2=n1+n2n1σ12+n1μ12+n2σ22+n2μ22−(n1+n2n1μ1+n2μ2)2=(n1+n2)2(n1+n2)(n1σ12+n1μ12+n2σ22+n2μ22)−(n1μ1+n2μ2)2=n1+n2n1σ12+n2σ22+(n1+n2)2n1n2μ12+n1n2μ22−2n1n2μ1μ2=n1+n2n1σ12+n2σ22+(n1+n2)2n1n2(μ1−μ2)2

方差的增量来自均值漂移

相关推荐
_OP_CHEN1 小时前
【算法基础篇】(五十七)线性代数之矩阵乘法从入门到实战:手撕模板 + 真题详解
线性代数·算法·矩阵·蓝桥杯·c/c++·矩阵乘法·acm/icpc
天天爱吃肉82181 小时前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
im_AMBER1 小时前
Leetcode 114 链表中的下一个更大节点 | 删除排序链表中的重复元素 II
算法·leetcode
xhbaitxl1 小时前
算法学习day38-动态规划
学习·算法·动态规划
多恩Stone1 小时前
【3D AICG 系列-6】OmniPart 训练流程梳理
人工智能·pytorch·算法·3d·aigc
历程里程碑2 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
pp起床2 小时前
贪心算法 | part02
算法·leetcode·贪心算法
sin_hielo2 小时前
leetcode 1653
数据结构·算法·leetcode
2501_901147832 小时前
面试必看:优势洗牌
笔记·学习·算法·面试·职场和发展
YuTaoShao2 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法二)排序 + 二分查找
数据结构·算法·leetcode