So I have a dataset with (business) daily data from 2008 to 2011 for 5 variables stored in a pandas DataFrame called storeddata. I want to compute a exponentially-weighted moving average covariance matrix for each day. I have an initiation period (let's say from 2008-01-01 to 2009-01-01) for which I compute the covariance matrix, a 5x5 matrix, called covmat1.
Now I want to iterate through the storeddata-DataFrame and after 2009-01-01 update a dict called covfinaldict with a 5x5 matrix for each business day. I have tried with this block of code:
from pandas.tseries.offsets import BDay
import pandas as pd
import numpy as np
import datetime as dt
zeromatrix=np.mat(np.zeros((5,5)))
for row in storeddata.iterrows():
if row[0]>dt.datetime(2009,01,01):
mat1=covfinaldict[row[0]-BDay(1)]
for x in range(5):
for y in range(5):
if x==y:
zeromatrix[x,y]=sqrt(0.93*(mat1[x,y]**2)+0.07*(row[1][x+1]**2))
else:
zeromatrix[x,y]=0.93*mat1[x,y]+0.07*row[1][x+1]*row[1][y+1]
covfinaldict[row[0]]=zeromatrix
else:
covfinaldict[row[0]]=covmat1
The formulas are an approximation of an exponentially-weighted moving average, the details of which are irrelevant. The computation does not work in the sense that I get the same 5x5 matrix (although it is different from covmat1) for each day after 2009-01-01. Furthermore, if I run sqrt(0.93*(mat1[x,y]**2)+0.07*(row[1][x+1]**2)) in console I get 0.1613, if I run zeromatrix[x,y] I get 0.158 (x=4, y=4). This makes no sense to me. How can the left hand side of an equation not be equal to the right hand side? Explanations are much appreciated.