# [R] Multivariate EWMA covariance estimator?

Berend Hasselman bhh at xs4all.nl
Sun Jun 2 16:35:43 CEST 2013

```On 02-06-2013, at 15:17, Neuman Co <neumancohu at gmail.com> wrote:

> Hi,
> since I want to calculate the VaR of a portfolio consiting of 4 assets
> (returns saved into "eonreturn","henkelreturn" and so on) I have to
> estimate the covariance matrix. I do not want to take the rectangular
> version with equal weights, but the exponentially weighted moving
> average in a multivariate version. I want to estimate a covariance
> matrix at every time point t. Then I want to comput the VaR at this
> time point t. Afterwards, I will look at the exceedances and do a
> backtest.
>
> I tried to implement it as follows (data attached):
>
> lambda<-0.9
>
> summe2<-0
> dummy2<-0
> covestiexpo<-list(NA)
> meanvalues<-NA
> for(i in 101:length(eonreturn)){
> for(a in 1:100){
> dummy2<-lambda^(a-1)*t(datamatrix[(i-a),]-t(meanvalues))%*%(datamatrix[(i-a),]-t(meanvalues))
> summe2<-summe2+dummy2
> }
> covestiexpo[[i]]<-(1-lambda)*summe2
> }
>
>
> So the covestieexpo[[101]] would be the covariance estimate for the
> 101th day, taking into account the last 100 observations. Now, the
> problem is, that there seems to be something wrong, since the
> covariance estimates are cleraly wrong, they seem to be too big. At
> the beginning, compared to the normal covariance estimate the
> difference is as follows:
>
> covestiexpo[[101]]
>            [,1]        [,2]        [,3]        [,4]
> [1,] 0.004559042 0.002346775 0.004379735 0.003068916
> [2,] 0.002346775 0.001978469 0.002536891 0.001909276
> [3,] 0.004379735 0.002536891 0.005531590 0.003259803
> [4,] 0.003068916 0.001909276 0.003259803 0.003140198
>
>
>
> compared to cov(datamatrix[1:100,])
>             [,1]         [,2]         [,3]        [,4]
> [1,] 0.0018118239 0.0007432779 0.0015301070 0.001119120
> [2,] 0.0007432779 0.0008355960 0.0009281029 0.000754449
> [3,] 0.0015301070 0.0009281029 0.0021073171 0.001269626
> [4,] 0.0011191199 0.0007544490 0.0012696257 0.001325716
>
> So already here, it is obvious, that something is not correct, if I
> look at a period far ahead:
>
> covestiexpo[[1200]]
>
>          [,1]      [,2]      [,3]      [,4]
> [1,] 0.5312575 0.1939061 0.3419379 0.2475233
> [2,] 0.1939061 0.3204951 0.2303478 0.2022423
> [3,] 0.3419379 0.2303478 0.5288435 0.2943051
> [4,] 0.2475233 0.2022423 0.2943051 0.4599648
>
>
> you can see, that the values are way too large, so where is my mistake?

Without actual data this is an unverifiable statement.
But you probably have to move the statement

summe2 <- 0

to inside the i-forloop just before the a-forloop.

summe2 <- 0
for(a in 1:100){
…

so that summe2 is initialized to 0 every time you use it as an accumulator in the a-forloop.
Furthermore there is no need to initialize dummy2. It gets overwritten continuously.

Berend

```