Python - 计算加权滚动标准差

时间:2016-10-21 16:14:50

标签: python pandas standard-deviation

我有一分钟一分钟的熊猫数据帧df。我希望将加权应用于Return并计算滚动加权标准差,窗口= 10.我可以计算非加权标准,年化时间为:

df_spy['10mVol'] = df_spy['Return'].rolling(center=False,window=10).std()*(1440*252)**(0.5)*100

另一个问题是在Numpy中要求加权std,但我对滚动加权的stdev感到好奇。 (Weighted standard deviation in NumPy?

计算加权标准差的公式为: https://math.stackexchange.com/questions/320441/standard-deviation-of-the-weighted-mean

weighting   Midpoint   Return      10mVol   Weighted
0.2         215.6700    NaN         NaN      NaN
0.8         215.8400    -0.000788   NaN     -0.000630
0.8         216.0600    -0.001019   NaN     -0.000815

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

据我所知,rolling方法之后的链式函数是一个接受数组并给出数字的函数。为每个窗口计算该函数。因此,如果我们有一个计算加权标准的函数,我们可以使用它与lambda函数来获得滚动加权标准。这是我的看法。 (我希望我没有用你提供的加权标准计算出错)

import pandas as pd
import numpy as np


def weighted_std(values, weights):
    # For simplicity, assume len(values) == len(weights)
    # assume all weights > 0
    sum_of_weights = np.sum(weights)
    weighted_average = np.sum(values * weights) / sum_of_weights
    n = len(weights)
    numerator = np.sum(n * weights * (values - weighted_average) ** 2.0)
    denominator = (n - 1) * sum_of_weights
    weighted_std = np.sqrt(numerator / denominator)
    return weighted_std


def rolling_std(s, weights):
    window_size = len(weights)
    return s.rolling(center=False, window=window_size).apply(lambda win: weighted_std(win, weights))

s = pd.Series(np.random.random([10]))  # generate random data
w = np.array([1., 3., 5.])  # choose weights
print(s.values)
print(rolling_std(s, w).values)

示例输出:

[ 0.08101966  0.57133241  0.29491028  0.25139964  0.26151065  0.45768199
  0.94459935  0.21534497  0.35999294  0.60242746]
[        nan         nan  0.19701963  0.11936639  0.01539041  0.12097725
  0.33346742  0.40784167  0.25884732  0.17709334]

此处lambda win: weighted_std(win, weights)是一个函数,它将数组作为输入并返回一个数字。