Seb*_*Seb 5 python algorithm statistics variance batch-updates
我想扩展 Welford 的在线算法,以便能够使用多个数字(批量)进行更新,而不是一次只更新一个: https: //en.wikipedia.org/wiki/Algorithms_for_calculate_variance
我尝试从 wiki 页面更新算法,如下所示:
# my attempt.
def update1(existingAggregate, newValues):
(count, mean, M2) = existingAggregate
count += len(newValues)
delta = np.sum(np.subtract(newValues, [mean] * len(newValues)))
mean += delta / count
delta2 = np.sum(np.subtract(newValues, [mean] * len(newValues)))
M2 += delta * delta2
return (count, mean, M2)
# The original two functions from wikipedia.
def update(existingAggregate, newValue):
(count, mean, M2) = existingAggregate
count += 1
delta = newValue - mean
mean += delta / count
delta2 = newValue - mean
M2 += delta * delta2
def finalize(existingAggregate):
(count, mean, M2) = existingAggregate
(mean, variance, sampleVariance) = (mean, M2/count, M2/(count - 1))
if count < 2:
return float('nan')
else:
return (mean, variance, sampleVariance)
Run Code Online (Sandbox Code Playgroud)
但是,我一定理解不正确,因为结果是错误的:
# example x that might have led to an a = (2, 2.0, 2.0).
x = [1.0, 3.0]
mean = np.mean(x)
count = len(x)
m2 = np.sum(np.subtract(x, [mean] * count)**2)
a = (count, mean, m2)
print(a)
# new batch of values.
b = [5, 3]
Run Code Online (Sandbox Code Playgroud)
请注意,a = (2, 2.0, 2.0) 表示我们有 2 个观测值,它们的平均值为 2.0。
# update one at a time.
temp = update(a, newValues[0])
result_single = update(temp, newValues[1])
print(finalize(result_single))
# update with my faulty batch function.
result_batch = update1(a, newValues)
print(finalize(result_batch))
Run Code Online (Sandbox Code Playgroud)
正确的输出应该是应用单个数字更新两次的输出:
(3.0, 2.0, 2.6666666666666665)
(3.0, 2.5, 3.3333333333333335)
Run Code Online (Sandbox Code Playgroud)
关于正确的方差更新,我缺少什么?我是否还需要以某种方式更新 Finalize 函数?
我需要这样做的原因是因为我正在处理非常大的每月文件(具有不同数量的观察结果)并且我需要获得每年的平均值和方差。
感谢尼科的澄清,我明白了!问题是我对增量求和,然后乘以得到 M2,但必须对增量的乘积求和。这是正确的批处理函数,它能够接受单个数字以及批量:
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
def update(existingAggregate, newValues):
if isinstance(newValues, (int, float, complex)):
# Handle single digits.
newValues = [newValues]
(count, mean, M2) = existingAggregate
count += len(newValues)
# newvalues - oldMean
delta = np.subtract(newValues, [mean] * len(newValues))
mean += np.sum(delta / count)
# newvalues - newMeant
delta2 = np.subtract(newValues, [mean] * len(newValues))
M2 += np.sum(delta * delta2)
return (count, mean, M2)
def finalize(existingAggregate):
(count, mean, M2) = existingAggregate
(mean, variance, sampleVariance) = (mean, M2/count, M2/(count - 1))
if count < 2:
return float('nan')
else:
return (mean, variance, sampleVariance)
Run Code Online (Sandbox Code Playgroud)
用法示例:
x = [1.0, 3.0]
mean = np.mean(x)
count = len(x)
m2 = np.sum(np.subtract(x, [mean] * count)**2)
a = (count, mean, m2)
print(a)
# new batch of values.
b = [5, 3]
result_batch = update(a, b)
result_batch1 = update(a, b[0])
print(finalize(result_batch))
print(finalize(result_batch1))
Run Code Online (Sandbox Code Playgroud)
而且它确实更快:
import timeit
x = random.sample(range(1, 10000), 1000)
# ...
b = random.sample(range(1, 10000), 1000)
start_time = timeit.default_timer()
result_batch = update(a, b)
print(f'{timeit.default_timer() - start_time:.4f}')
print(*(f'{x:.2f}' for x in finalize(result_batch)))
start_time = timeit.default_timer()
for i in b:
a = update1(a, i)
print(f'{timeit.default_timer() - start_time:.4f}')
print(*(f'{x:.2f}' for x in finalize(result_batch)))
Run Code Online (Sandbox Code Playgroud)
结果:
0.0010
5008.36 8423224.68 8427438.40
0.0031
5008.36 8423224.68 8427438.40
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2322 次 |
最近记录: |