简体   繁体   English

时间序列的加权两侧移动平均线 python

[英]Weighted two sided moving average for time series python

I have a time series and I want to get from it a weighted two sided moving average.我有一个时间序列,我想从中得到一个加权的双边移动平均线。 That is, I want to create a function that takes one observation of the original time series, considers n numbers both at the left and at the right of that center value (that's why is two sided) and calculates the weighted average of that.也就是说,我想创建一个 function 对原始时间序列进行一次观察,考虑该中心值左右两侧的n数字(这就是为什么是两侧)并计算其加权平均值。

By weighted I mean that each value both at the left and right will be multiplied by a weight that gets smaller the further from the center observation that value is.加权我的意思是左侧和右侧的每个值都将乘以一个权重,该权重离中心观察值越远,该值越小。

Once this is done for the first value I choose to have as my center, the function moves to the next one to the right and does the same, and so on.一旦对我选择作为中心的第一个值完成此操作,function 将移动到右侧的下一个值并执行相同的操作,依此类推。

The equation of the filter I'm trying to apply is:我试图应用的过滤器方程是:

在此处输入图像描述

As an example please consider the following time series:例如,请考虑以下时间序列:

X = [0, 0, 0, 3, 4, 5, 6, 7, 0, 0, 0]

For β = 0.5 and starting to apply the filter in the third observation, I would have:对于β = 0.5并开始在第三次观察中应用过滤器,我将拥有:

在此处输入图像描述

But the function should be applied in an for loop that iterates over each value of X starting and finishing in the position that I want.但是 function 应该应用在一个 for 循环中,该循环遍历我想要的 position 中开始和结束的每个 X 值。

Thanks!谢谢!

What you're doing is called a "convolution".您正在做的事情称为“卷积”。 numpy can do this: numpy 可以这样做:

import numpy as np
X = np.array([0, 0, 0, 3, 4, 5, 6, 7, 0, 0, 0])
wts = np.array([.125, .25, .5, 1, .5, .25, .125])
print(np.convolve(X,wts))

Output: Output:

[ 0.     0.     0.     0.375  1.25   3.125  7.    10.375 12.5   13.375
 11.75   5.625  2.5    0.875  0.     0.     0.   ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM