## Python中的EWMA波动性 - 避免循环内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (336)

``````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
``````

``````def CalculateEWMAVol (ReturnSeries, Lambda):
SampleSize = len(ReturnSeries)
Average = ReturnSeries.mean()

e = np.arange(SampleSize-1,-1,-1)
r = np.repeat(Lambda,SampleSize)
vecLambda = np.power(r,e)

sxxewm = (np.power(ReturnSeries-Average,2)*vecLambda).sum()
Vart = sxxewm/vecLambda.sum()
EWMAVol = math.sqrt(Vart)

return (EWMAVol)

def CalculateVol (R, Lambda):
Vol = pd.Series(index=R.columns)
for facId in R.columns:
Vol[facId] = CalculateEWMAVol(R[facId], Lambda)

return (Vol)
``````

### 1 个回答

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