Python避免循环中的EWMA波动性

2024-04-19 16:20:11 发布

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

我有一个时间序列是这样的(一个切片):

Date         3         7           10
2015-02-13   0.00021  -0.00078927  0.00407473
2015-02-16   0.0      -0.00343163  0.0
2015-02-17   0.0       0.0049406   0.00159753
2015-02-18   0.00117  -0.00123565 -0.00031423
2015-02-19   0.00091  -0.00253578 -0.00106207
2015-02-20   0.00086   0.00113476  0.00612649
2015-02-23  -0.0011   -0.00403307 -0.00030327
2015-02-24  -0.00179   0.00043229  0.00275874
2015-02-25   0.00035   0.00186069 -0.00076578
2015-02-26  -0.00032  -0.01435613 -0.00147597
2015-02-27  -0.00288  -0.0001786  -0.00295631

为了计算EWMA波动率,我实现了以下函数:

^{pr2}$

该函数工作正常,但对于较大的时间序列,由于for循环,该过程会变慢。在

有没有其他方法可以在这个系列中调用这个函数?在


Tags: 方法函数fordate过程时间切片序列
2条回答

我想您真正要求的是避免使用loop,但是pandas apply()并不能解决这个问题,因为您仍然会循环数据帧中的每一列。不久前我探讨过这个主题,在用尽了我的选择之后,我最终将一个MatLab矩阵计算转换成Python代码,它以矩阵形式完美地完成了vol和decay计算。下面的代码,假设df_tmp是每个价格指数有多个列的时间序列。在

decay_factor = 0.94
decay_f = np.arange(df_tmp.shape[0], 0, -1)
decay_f = decay_factor ** decay_f
decay_sum = sum(decay_f)
w = decay_f / decay_sum
avg_weight = np.ones(df_tmp.shape[0]) / df_tmp.shape[0]
T, N = df_tmp.shape
temp = df_tmp - df_tmp * np.tile(avg_weight, (4422, 1)).T
temp = np.dot(temp.T, temp * np.tile(w, (4422, 1)).T)
temp = 0.5 * (temp + temp.T)
R = np.diag(temp)
sigma = np.sqrt(R)
R = temp / np.sqrt(np.dot(R, R.T))

sigma是波动率,R是校正矩阵,temp是协方差矩阵。在

我认为你的功能是技术上最正确的方法。 我只是想建议你用“申请”,而不是“为”自己。在

Is there another approach to calling this function over the series?

Vol[facId] = R.apply(CalculateEWMAVol(R[facId], Lambda)

我希望它能有用。在

相关问题 更多 >