【问题标题】:Quick way to calculate uniformity or discrepancy of number set计算数集一致性或差异的快速方法
【发布时间】:2011-05-14 15:41:09
【问题描述】:

你好 假设我有一组数字,我想快速计算出某种均匀度。 我知道方差是最明显的答案,但我担心朴素算法的复杂性太高了 有人有什么建议吗?

【问题讨论】:

标签: algorithm math statistics uniform


【解决方案1】:

用于计算方差的“直观”算法通常会遇到以下一种或两种情况:

  1. 使用两个循环(一个用于计算均值,另一个用于计算方差)
  2. 不是numerically stable

由于D. Knuth(一如既往),只有一个循环且数值稳定的好算法。

From Wikipedia:

n = 0
mean = 0
M2 = 0
 def calculate_online_variance(x):
    n = n + 1
    delta = x - mean
    mean = mean + delta/n
    M2 = M2 + delta*(x - mean)  # This expression uses the new value of mean

    variance_n = M2/n
    variance = M2/(n - 1) #note on the first pass with n=1 this will fail (should return Inf)
    return variance

您应该为每个点调用calculate_online_variance(x),它会返回到目前为止计算的方差。

【讨论】:

  • 你提到缺乏数值稳定性,例子?我只是没有看到它会在使用mean(x)^2 - mean(x^2) 时蔓延。我可能遗漏了一些明显的东西。
  • @rcollyer 我指的是这个:en.wikipedia.org/wiki/…
  • 好吧,看来我需要真正阅读答案中发布的链接......无论哪种方式,gracias 和 +1。
  • @rcollyer 这样更好......我无法忍受和你争论数值稳定性:)
  • 在这种常见情况下,数值稳定性是一个问题:如果mean^2 >> variance,你减去两个大数得到一个小数,这会降低你的浮点精度。
猜你喜欢
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2010-12-15
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多