滚动方差算法 [英] Rolling variance algorithm

查看:332
本文介绍了滚动方差算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种有效的,数值稳定的算法来计算滚动方差(例如,20个周期的滚动窗口的方差)。我知道 Welford算法,它可以有效地计算数字流的运行方差(只需要通过一次),但是不确定是否可以将其用于滚动窗口。我还希望该解决方案能够避免John D在本文顶部讨论的准确性问题。厨师任何语言的解决方案都可以。

I'm trying to find an efficient, numerically stable algorithm to calculate a rolling variance (for instance, a variance over a 20-period rolling window). I'm aware of the Welford algorithm that efficiently computes the running variance for a stream of numbers (it requires only one pass), but am not sure if this can be adapted for a rolling window. I would also like the solution to avoid the accuracy problems discussed at the top of this article by John D. Cook. A solution in any language is fine.

推荐答案

我也遇到了这个问题。在计算运行累积方差方面有很多不错的文章,例如John Cooke的准确计算运行方差文章和Digital Explorations的文章,用于计算样本和总体方差,协方差和相关系数。只是找不到适合滚动窗口的任何内容。

I've run across this problem as well. There are some great posts out there in computing the running cumulative variance such as John Cooke's Accurately computing running variance post and the post from Digital explorations, Python code for computing sample and population variances, covariance and correlation coefficient. Just could not find any that were adapted to a rolling window.

运行标准偏差对于使滚动窗口公式正常工作至关重要。吉姆采用了值平方差的幂和,而韦尔福德采用的是均值平方差之和。公式如下:

The Running Standard Deviations post by Subluminal Messages was critical in getting the rolling window formula to work. Jim takes the power sum of the squared differences of the values versus Welford’s approach of using the sum of the squared differences of the mean. Formula as follows:


PSA今天= PSA(昨天)+((((x今天* x今天)-x昨天))/ n

PSA today = PSA(yesterday) + (((x today * x today) - x yesterday)) / n


  • x =时间序列中的值

  • n =您分析过的值数

但是,要将Power Sum Average公式转换为开窗变量,您需要调整公式如下:

But, to convert the Power Sum Average formula to a windowed variety you need tweak the formula to the following:


今天的PSA =昨天的PSA +((((x今天* x今天)-(x昨天* x昨天) / n

PSA today = PSA yesterday + (((x today * x today) - (x yesterday * x Yesterday) / n


  • x =时间序列中的值

  • n =您拥有的值数

您还需要滚动简单移动平均线公式:

You'll also need the Rolling Simple Moving Average formula:


今天的SMA =昨天的SMA +((今天x-今天x-n)/ n

SMA today = SMA yesterday + ((x today - x today - n) / n


  • x =时间序列中的值

  • n =用于滚动窗口的时间段。

从那里您可以计算滚动人口方差:

From there you can compute the Rolling Population Variance:


今天的人口变量=(今天的PSA * n-n * SMA的今天* SMA的今天)/ n

Population Var today = (PSA today * n - n * SMA today * SMA today) / n

或滚动样本方差:


今天的样本变量=(今天的PSA * n-n *今天的SMA * SMA今天)/(n-1)

Sample Var today = (PSA today * n - n * SMA today * SMA today) / (n - 1)

几年前,我在博客中介绍了此主题以及示例Python代码,运行差异

I've covered this topic along with sample Python code in a blog post a few years back, Running Variance.

希望这会有所帮助。


请注意:我在Latex中提供了所有博客文章和数学公式
的链接(图片)为此答案。但是,由于我的声誉低(<
10);我仅限于2个超链接,而且绝对没有图片。对不起,
。希望这不会影响内容。

Please note: I provided links to all the blog posts and math formulas in Latex (images) for this answer. But, due to my low reputation (< 10); I'm limited to only 2 hyperlinks and absolutely no images. Sorry about this. Hope this doesn't take away from the content.

这篇关于滚动方差算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆