如何在python中使用滚动来计算权重

问题描述

我们有一个向量“ vec_df”和一个函数“ cal_weight”来计算向量的权重。如何在python中使用滚动来计算权重

示例

 # window can be changed to any value e.g 20 30
 # normal case
 window = 20
 df = vec_df.head(window).transpose()
 return_vec = df.to_numpy()
 weights = cal_weight(return_vec)

 # rolling case,please comment how to fix
 window = 20  
 weights = vec_df.rolling(window).apply(cal_weight)

数据

1. function to calculate weight with a parameter vector
def cal_weight(vec):
    .....
    return weights


2. vector (the columns and rows are not fixed)
            vec_A       vec_B       vec_C       vec_D
Date                
2012-06-08  -0.000239   0.001489    0.014369    0.015042
2012-06-11  0.004552    0.004072    -0.025295   -0.015767
2012-06-12  -0.009144   0.007080    0.013495    0.008736
2012-06-13  0.009870    0.004218    -0.005463   -0.006942
2012-06-14  -0.001272   0.004010    0.007209    -0.001101
... ... ... ... ...
2020-08-17  0.002513    0.021694    0.006606    -0.002611
2020-08-18  0.007152    0.009008    0.005754    0.008333
2020-08-19  -0.006252   -0.031565   -0.006067   0.001255
2020-08-20  0.009039    0.006914    0.023271    0.022190
2020-08-21  0.005992    -0.008011   -0.007270   0.051532
2065 rows × 4 columns

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)