移动加权平均法python代码
时间: 2023-07-02 17:22:56 浏览: 137
移动加权平均法(Moving Weighted Average)是一种常见的时间序列平滑方法,可以用来处理时间序列数据中的噪声,使得数据变得更加平滑。其python代码如下:
```python
def moving_weighted_average(data, weights):
"""
计算移动加权平均值
Parameters
----------
data : list
时间序列数据
weights : list
加权系数,必须是递增的,且总和为1
Returns
-------
result : list
移动加权平均值序列
"""
result = []
for i in range(len(data)):
if i < len(weights):
window = data[:i+1]
weights_ = weights[-i-1:]
else:
window = data[i-len(weights)+1:i+1]
weights_ = weights
result.append(sum([data*w for data, w in zip(window, weights_)])/sum(weights_))
return result
```
其中,`data` 是时间序列数据,`weights` 是加权系数,必须是递增的,且总和为1。该函数返回移动加权平均值序列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)