我尝试将rollappy与一个需要2个参数的公式一起使用。据我所知,计算kendall-tau相关性的唯一方法(除非您从头开始创建公式),包括标准领带校正:
>>> import scipy
>>> x = [5.05, 6.75, 3.21, 2.66]
>>> y = [1.65, 26.5, -5.93, 7.96]
>>> z = [1.65, 2.64, 2.64, 6.95]
>>> print scipy.stats.stats.kendalltau(x, y)[0]
0.333333333333
我还知道rollappy和接受两个参数的问题,如本文所述:
不过,我仍在努力寻找一种方法,在滚动的基础上对具有多个列的数据帧进行kendalltau计算。在
我的数据帧是这样的
^{pr2}$尝试创建一个函数来执行此操作
In [1]:function(A, 3) # A is df, 3 is the rolling window
Out[2]:
A B C AB AC BC
1 1 5 2 NaN NaN NaN
2 2 4 4 NaN NaN NaN
3 3 3 1 -0.99 -0.33 0.33
4 4 2 2 -0.99 -0.33 0.33
5 5 1 4 -0.99 0.99 -0.99
在一个非常初步的方法中,我想到了这样定义函数:
def tau1(x):
y = np.array(A['A']) # keep one column fix and run it in the other two
tau, p_value = sp.stats.kendalltau(x, y)
return tau
A['AB'] = pd.rolling_apply(A['B'], 3, lambda x: tau1(x))
当然没用。我得到了:
ValueError: all keys need to be the same shape
我明白这不是一个小问题。我很感激你的意见。在
As of Pandas 0.14,
rolling_apply
只向函数传递NumPy数组。一种可能的解决方法是将np.arange(len(A))
作为第一个参数传递给rolling_apply
,这样tau
函数接收到要使用的行的索引。然后在tau
函数中返回包含所需的所有行的数据帧。在
^{pr2}$
收益率
相关问题 更多 >
编程相关推荐