Skip to content Skip to sidebar Skip to footer

EWMA Covariance Matrix In Pandas - Optimization

I would like to calculate the EWMA Covariance Matrix from a DataFrame of stock price returns using Pandas and have followed the methodology in PyPortfolioOpt. I like the flexibilit

Solution 1:

since you don't really care for ewm, i.e, you only take the last value. We can try matrix multiplication:

def ewma(df, alpha=0.94):
    weights = (1-alpha) ** np.arange(len(df))[::-1]

    # fillna with 0 here
    normalized = (df-df.mean()).fillna(0).to_numpy()
    
    out =  ((weights * normalized.T) @ normalized / weights.sum()
    
    return out

 # verify
 out = ewma(df)
 print(out[0,1] == ewma_cov_pairwise(df[0],df[1]) )
 # True

And this took about 150 ms on my system with df.shape==(2000,2000) while your code refuses to run within minutes :-).


Post a Comment for "EWMA Covariance Matrix In Pandas - Optimization"