[R] Multivariate EWMA covariance estimator?
Berend Hasselman
bhh at xs4all.nl
Sun Jun 2 19:57:37 CEST 2013
On 02-06-2013, at 19:45, Neuman Co <neumancohu at gmail.com> wrote:
> Again, a big thanks for your answer.
>
> On this webpage:
> http://financetrainingcourse.com/education/2010/03/master-class-calculating-value-at-risk-var-final-steps/
>
> I found, that I have to rescale by dividing the weights calculated in
> Step B2 by 1-?n
>
> The "?" is the lambda, since the webpage cannot display it, I also
> found it on another webpage, therefore, I changed my code to the
> following:
>
> dummy2<-lambda^(a-1)/(1-lambda^100)*t(datamatrix[(i-a),]-t(meanvalues))%*%(datamatrix[(i-a),]-t(meanvalues))
>
>
> Do you think this is correct?
>
I'm not going to do homework.
You have to scale the weights. So you need to scale lambda^(a-1) by their sum for a=1:100 if I understand correctly.
> One further question: You also told me, that I do not have to
> initialize my dummy2, what does this mean?
Exactly that: you don't have to initialize dummy2.
You are assigning a value to dummy2 for each value of a.
You are thus either creating a new object dummy2 or overwriting any existing object dummy2 and thus destroying a previous value.
> I wrote dummy2<-0 because I
> have to create this variable before using it for the loop?
>
You are not using it on the righthand side of an expression. You are assigning to it.
You only need to initialize dummy2 if you have an expression involving dummy2 on the righthand side e.g. dummy2 <- dummy2 + ……
Berend
> 2013/6/2 Berend Hasselman <bhh at xs4all.nl>:
>>
>> On 02-06-2013, at 19:03, Neuman Co <neumancohu at gmail.com> wrote:
>>
>>> Thanks a lot for your answer, one more question:
>>> I now use 100 values, so not infinity values. That means I cut some
>>> values off, so the weights will not sum up to one. With which factor
>>> do I have to multiply the (1-lambda)*summe2 to rescale it? So that I
>>> do not always underestimate the variance anymore?
>>>
>>
>> I don't know but maybe something like this
>>
>> 1/sum(lambda^((1:100)-1))/(1-lambda)
>>
>> which in your case is 1.000027
>>
>> Berend
>>
>>> 2013/6/2 Berend Hasselman <bhh at xs4all.nl>:
>>>>
>>>> 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)){
>>>>> meanvalues<-matrix(c(mean(eonreturn[(i-100):(i-1)]),mean(henkelreturn[(i-100):(i-1)]),mean(siemensreturn[(i-100):(i-1)]),mean(adidasreturn[(i-100):(i-1)])),4)
>>>>> 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
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Neumann, Conrad
>>
>
>
>
> --
> Neumann, Conrad
More information about the R-help
mailing list