具有不同功能和不同时限的移动平均线 [英] A moving average with different functions and varying time-frames

查看:92
本文介绍了具有不同功能和不同时限的移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有8个变量的矩阵时间序列数据,这些变量具有大约2500点(〜10年的星期一至星期五),并希望在移动平均值"的基础上计算均值,方差,偏度和峰度.

I have a matrix time-series data for 8 variables with about 2500 points (~10 years of mon-fri) and would like to calculate the mean, variance, skewness and kurtosis on a 'moving average' basis.

让我们说frames = [100 252 504 756]-我想每天在每个(时间)框架上计算上述四个函数-因此,对于100天框架,第300天的收益为[mean variance skewness kurtosis]从日期day-201-day300(总共100天)开始...依此类推.

Lets say frames = [100 252 504 756] - I would like calculate the four functions above on over each of the (time-)frames, on a daily basis - so the return for day 300 in the case with 100 day-frame, would be [mean variance skewness kurtosis] from the period day201-day300 (100 days in total)... and so on.

我知道这意味着我将获得一个数组输出,并且第一个frame天数将是NaN,但是我无法弄清楚完成该操作所需的索引...

I know this means I would get an array output, and the the first frame number of days would be NaNs, but I can't figure out the required indexing to get this done...

推荐答案

这是一个有趣的问题,因为我认为最优解的均值不同于其他样本统计信息.

This is an interesting question because I think the optimal solution is different for the mean than it is for the other sample statistics.

我在下面提供了一个仿真示例,您可以进行练习.

I've provided a simulation example below that you can work through.

首先,选择一些任意参数并模拟一些数据:

First, choose some arbitrary parameters and simulate some data:

%#Set some arbitrary parameters
T = 100; N = 5;
WindowLength = 10;

%#Simulate some data
X = randn(T, N);

对于平均值,请使用filter获得移动平均值:

For the mean, use filter to obtain a moving average:

MeanMA = filter(ones(1, WindowLength) / WindowLength, 1, X);
MeanMA(1:WindowLength-1, :) = nan;

我原本以为可以使用conv来解决此问题,如下所示:

I had originally thought to solve this problem using conv as follows:

MeanMA = nan(T, N);
for n = 1:N
    MeanMA(WindowLength:T, n) = conv(X(:, n), ones(WindowLength, 1), 'valid');
end
MeanMA = (1/WindowLength) * MeanMA;

但是正如@PhilGoddard在评论中指出的那样,filter方法避免了循环.

But as @PhilGoddard pointed out in the comments, the filter approach avoids the need for the loop.

还请注意,我已选择使输出矩阵中的日期与X中的日期相对应,因此在以后的工作中,您可以对两者使用相同的下标.因此,MeanMA中的第一个WindowLength-1观测值将是nan.

Also note that I've chosen to make the dates in the output matrix correspond to the dates in X so in later work you can use the same subscripts for both. Thus, the first WindowLength-1 observations in MeanMA will be nan.

对于方差,我看不到如何使用filterconv甚至是运行总和来提高效率,所以我在每次迭代时手动执行计算:

For the variance, I can't see how to use either filter or conv or even a running sum to make things more efficient, so instead I perform the calculation manually at each iteration:

VarianceMA = nan(T, N);
for t = WindowLength:T
    VarianceMA(t, :) = var(X(t-WindowLength+1:t, :));
end

通过利用我们已经计算出平均移动平均值的事实,我们可以稍微加快速度.只需将上面的内部循环行替换为:

We could speed things up slightly by exploiting the fact that we have already calculated the mean moving average. Simply replace the within loop line in the above with:

VarianceMA(t, :) = (1/(WindowLength-1)) * sum((bsxfun(@minus, X(t-WindowLength+1:t, :), MeanMA(t, :))).^2);

但是,我怀疑这会带来很大的不同.

However, I doubt this will make much difference.

如果其他人可以看到使用filterconv的巧妙方法来获取移动窗口方差,我将非常有兴趣看到它.

If anyone else can see a clever way to use filter or conv to get the moving window variance I'd be very interested to see it.

我将偏度和峰度留给OP,因为它们本质上与方差示例相同,但具有适当的功能.

I leave the case of skewness and kurtosis to the OP, since they are essentially just the same as the variance example, but with the appropriate function.

最后一点:如果将上述内容转换为通用函数,则可以传入匿名函数作为参数之一,那么您将拥有一个移动平均值例程,该例程可用于任意选择转换.

A final point: if you were converting the above into a general function, you could pass in an anonymous function as one of the arguments, then you would have a moving average routine that works for arbitrary choice of transformations.

最后一点:对于一系列窗口长度,只需为每个窗口长度循环遍历整个代码块.

Final, final point: For a sequence of window lengths, simply loop over the entire code block for each window length.

这篇关于具有不同功能和不同时限的移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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