计算数字集均匀性或差异的快速方法

发布于 2024-10-03 16:27:15 字数 73 浏览 0 评论 0原文

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

Hello
Assume I have the set of numbers I want a quick to calculate some measure of uniformity.
I know the variance is the most obvious answer but i am afraid the complexity of naive algorithm is too high
Anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2024-10-10 16:27:15

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

  1. 使用两个循环(一个用于计算平均值,另一个用于计算方差)
  2. 不是 数值稳定

一种好的算法,只有一个循环并且数值稳定,归功于D.高德纳(一如既往)。

来自维基百科

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),它返回计算出的方差远的。

"Intuitive" algorithms for calculating variance usually suffer one or both of the following:

  1. Use two loops (one for calculating the mean, the other for the variance)
  2. Are not numerically stable

A good algorithm, with only one loop and numerically stable is due to D. Knuth (as always).

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

You should invoke calculate_online_variance(x) for each point, and it returns the variance calculated so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文