如何简单地计算python中时间序列的滚动/移动方差?

2024-05-15 01:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的时间序列,我正在努力估计移动窗口内的方差。更具体地说,我无法找出与实现滑动窗口函数的方式有关的一些问题。例如,当使用NumPy和window size=20时:

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) 

rolling_window(data, 20)
np.var(rolling_window(data, 20), -1)
datavar=np.var(rolling_window(data, 20), -1)

也许我错在什么地方了,在这个思路。 有人知道一个简单的方法吗? 欢迎任何帮助/建议。


Tags: 函数numpydatasizevardefnp方式
4条回答

Pandasrolling_meanrolling_std函数已被弃用,取而代之的是更通用的“滚动”框架。@elyase的示例可以修改为:

import pandas as pd
import numpy as np
%matplotlib inline

# some sample data
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)).cumsum()

#plot the time series
ts.plot(style='k--')

# calculate a 60 day rolling mean and plot
ts.rolling(window=60).mean().plot(style='k')

# add the 20 day rolling standard deviation:
ts.rolling(window=20).std().plot(style='b')

rolling函数支持许多不同的窗口类型,如文档所述here。可以对rolling对象调用许多函数,包括var和其他有趣的统计数据(skewkurtquantile等)。我一直坚持使用std,因为图与平均值在同一个图上,这在单位方面更合理。

尽管是一个老线程,但我将添加另一个由this修改的方法,该方法不依赖panda或python循环。本质上,使用numpy的跨步技巧,您可以首先创建一个具有跨步的数组视图,这样计算沿最后一个轴的函数统计信息就相当于执行滚动统计信息。我修改了原始代码,通过填充添加最后一个轴的开始,输出形状与输入形状相同。

import numpy as np

def rolling_window(a, window):
    pad = np.ones(len(a.shape), dtype=np.int32)
    pad[-1] = window-1
    pad = list(zip(pad, np.zeros(len(a.shape), dtype=np.int32)))
    a = np.pad(a, pad,mode='reflect')
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

a = np.arange(30).reshape((5,6))

# rolling mean along last axis
np.mean(rolling_window(a, 3), axis=-1)

# rolling var along last axis
np.var(rolling_window(a, 3), axis=-1)

# rolling median along last axis
np.median(rolling_window(a, 3), axis=-1)

尽管是一个老线程,但我将添加另一个由this修改的方法,它不依赖panda或python循环。本质上,使用numpy的跨步技巧,您可以首先创建一个带跨步的数组视图,这样计算沿最后一个轴的函数统计值就相当于执行滚动统计值。我修改了原始代码,通过填充添加最后一个轴的开始,输出形状与输入形状相同。

import numpy as np

def rolling_window(a, window):
    pad = np.ones(len(a.shape), dtype=np.int32)
    pad[-1] = window-1
    pad = list(zip(pad, np.zeros(len(a.shape), dtype=np.int32)))
    a = np.pad(a, pad,mode='reflect')
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

a = np.arange(30).reshape((5,6))

# rolling mean along last axis
np.mean(rolling_window(a, 3), axis=-1)

# rolling var along last axis
np.var(rolling_window(a, 3), axis=-1)

# rolling median along last axis
np.median(rolling_window(a, 3), axis=-1)

你应该看看pandas。例如:

import pandas as pd
import numpy as np

# some sample data
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)).cumsum()

#plot the time series
ts.plot(style='k--')

# calculate a 60 day rolling mean and plot
pd.rolling_mean(ts, 60).plot(style='k')

# add the 20 day rolling variance:
pd.rolling_std(ts, 20).plot(style='b')

enter image description here

相关问题 更多 >

    热门问题