我正在尝试使用pyfinance软件包并使用PandasRollingOLS进行滚动回归beta(使用min_window选项进行滚动)进行简单的线性回归。
它可以工作,但是我想在函数中有一个min_window。
我希望在rollingOLS函数中具有min_window,因为如果我们有一个90的窗口,它不会对前90个值执行OLS。我想执行一次OLS扩展,直到至少有12个观测值(min_window)时开始90个观测值,然后滚动90(窗口)我试图理解软件包的代码,但是我无法在代码中包含min_window。
我想要这种功能(这是PandasRollingOLS类的初始化):
def __init__(self, y, x=None, window=None, **min_window=None**, has_const=False, use_const=True):
我认为我应该更新下面发布的utils.rolling_windows上的代码,有人可以帮我吗?
def rolling_windows(a, window):
"""Creates rolling-window 'blocks' of length `window` from `a`.
Note that the orientation of rows/columns follows that of pandas.
Example
-------
import numpy as np
onedim = np.arange(20)
twodim = onedim.reshape((5,4))
print(twodim)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
print(rwindows(onedim, 3)[:5])
[[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]]
print(rwindows(twodim, 3)[:5])
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]]
"""
if window > a.shape[0]:
raise ValueError('Specified `window` length of {0} exceeds length of'
' `a`, {1}.'.format(window, a.shape[0]))
if isinstance(a, (Series, DataFrame)):
a = a.values
if a.ndim == 1:
a = a.reshape(-1, 1)
shape = (a.shape[0] - window + 1, window) + a.shape[1:]
strides = (a.strides[0],) + a.strides
windows = np.squeeze(np.lib.stride_tricks.as_strided(a, shape=shape,
strides=strides))
# In cases where window == len(a), we actually want to "unsqueeze" to 2d.
# I.e., we still want a "windowed" structure with 1 window.
if windows.ndim == 1:
windows = np.atleast_2d(windows)
return windows
谢谢大家!
亚历山德罗
答案 0 :(得分:0)
此刻,我自己正在使用PandasRollingOLS进行挣扎。我得出的临时结论是在回归之前简单地处理它,即在运行回归之前删除min_window值以下的每一列。
MSFT_MpComputerStatus
请注意,它要求您的数据框具有NaN(这就是为什么我想您要有一个min_window的原因):
min_window = 3
df.loc[:,~(df.rolling(min_window).count() < min_window).all()]
在Python专家偶然发现您的帖子之前,这可能是一个临时的(丑陋的)解决方案。