Python中的EWMA波动-避免循环 [英] EWMA Volatility in Python - Avoiding loops

查看:190
本文介绍了Python中的EWMA波动-避免循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a time series that looks like this (a slice):

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波动率,我实现了以下功能:

For calculating the EWMA Volatility, I implemented the following functions:

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)

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

The function works properly, but with a large time series the process gets slow because of the for loop.

是否有另一种方法可以在整个系列中调用此函数?

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

推荐答案

我猜您真正要问的是避免使用循环,但是pandas apply()并不能解决此问题,因为您仍在循环您的每一列数据框.我花了很长时间探讨了这个主题,在用尽所有选项之后,我最终将MatLab矩阵计算转换为Python代码,并且它以矩阵形式完美地完成了衰减计算.下面的代码,假设df_tmp是每个价格指数具有多个列的时间序列.

I guess what you really asked is to avoid using loop, but the pandas apply() does not solve this problem, because you still loop around each column in your dataframe. I explored this topic a while ago, after exhausting my options, I end up converting a MatLab matrix calculation to Python code and it does the vol with decay calculation perfectly in matrix form. Code in the following, assuming df_tmp is the time series that has multiple columns for each price index.

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是corr矩阵,而temp是协方差矩阵.

sigma is volatility, R is corr matrix and temp is covariance matrix.

这篇关于Python中的EWMA波动-避免循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆