Please reply to the list. You will have more chance to get an answer.

Pascal


2013/7/18 G Girija <girijagun@gmail.com>

> Hi,
>
> Many thanks for your hint. Yes I made a mistake there.
>
> Also could you *please* help me in rectifying the following also?
>
> I am not able to ‘extract variance and covariance’
>
> As mentioned, there are 5 stocks and changed the code [
> http://www.forecastingfinancialrisk.com/3.html] as follows:
>
> > S<-cov(returns)
>
> > S
>
>              V1           V2           V3           V4           V5
>
> V1 0.0004951526 0.0001256519 0.0001651016 0.0003024310 0.0003516796
>
> V2 0.0001256519 0.0008076471 0.0001251208 0.0002005305 0.0002347568
>
> V3 0.0001651016 0.0001251208 0.0010000707 0.0003158028 0.0003682633
>
> V4 0.0003024310 0.0002005305 0.0003158028 0.0011405561 0.0008442661
>
> V5 0.0003516796 0.0002347568 0.0003682633 0.0008442661 0.0018536153
>
>
>
> > EWMA[1,]<-c(S)[c(1,10,5)]
>
> > a=t(as.numeric(returns$i)) %*% as.numeric(returns$i) ---*Here we need
>                                                                                                                                                            to change dataframe to numeric, that too only columns.*
>
> > str(a)
>
>  num [1, 1] 0
>
> > dim(a)
>
> [1] 1 1
>
> > dim(lambda*S)
>
>  [1] 5 5           ---------*As the dimensions are not same we can not do
> ‘%*%’*
>
> > dim((1-lambda)*a)
>
> [1] 1 1
>
>  for(i in 2:T)
>
> {
>
>   S<- lambda*S+ (1-lambda)*t(returns$i) %*% returns$i
>
>   EWMA[i,]=c(S)[c(1,4,2)]
>
> }
>
>
>
> *Original code as per* http://www.forecastingfinancialrisk.com/3.html is
>
> EWMA = *matrix*(*nrow*=T,*ncol*=3)   *# create a matrix to hold the covariance matrix for each t*
>
> lambda = 0.94
>
> S = *cov*(y)             *# initial (t=1) covariance matrix*
>
> EWMA[1,] = *c*(S)[*c*(1,4,2)]      *# extract the variances and covariance*
>
> *for* (i *in* 2:T){        *# loop though the sample*
>
>          S = lambda * S  + (1-lambda) * *t*(y[i]) %*% y[i]
>
>          EWMA[i,] = *c*(S)[*c*(1,4,2)]      *# convert matrix to vector     *
>
> }
>
>
>
> On Thu, Jul 18, 2013 at 5:54 AM, Pascal Oettli <kridox@ymail.com> wrote:
>
>> Hello,
>>
>> 1) In the provided example, you have 2 stock returns and
>>
>> > EWMA = matrix(nrow=T, ncol=3)
>>
>> See the number of columns (2+1)
>> Please modify the number of columns according to the number of stock
>> returns.
>>
>> 2) As you have 5 stock returns, the following cannot work
>>
>> > EWMA[1,] = c(S)[c(1,4,2)]
>>
>> You have to modify this part too.
>>
>>
>> 3) Do the same modification within the loop.
>>
>> Regards,
>> Pascal
>>
>>
>>
>> 2013/7/18 G Girija <girijagun@gmail.com>
>>
>>> hi,
>>>
>>> Could anyone help me in solving the following error:
>>> I have 5 stocks returns data (returns)
>>>
>>> EWMA = matrix(nrow=T,ncol=5)                  # create a matrix to hold
>>> the
>>> covariance matrix for each t
>>>
>>> lambda = 0.94
>>> S<-cov(returns)                                         # initial (t=1)
>>> covariance matrix
>>> EWMA[1,] = c(S)[c(1,4,2)]                  ---ERROR        # extract the
>>> variances and covariancefor (i in 2:T)
>>>
>>>
>>>    {            # loop though the sample
>>>         S<- lambda*S+(1-lambda)*t(returns[i])%*%returns[i]
>>>         EWMA[i,] = c(S)[c(1,4,2)]       # convert matrix to vector
>>>    }
>>>
>>> *ERROR as follows:*
>>>
>>> *> EWMA[1,]<-c(S)[c(1,4,2)]*
>>> *Error in EWMA[1, ] <- c(S)[c(1, 4, 2)] : *
>>> *  number of items to replace is not a multiple of replacement length*
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>

	[[alternative HTML version deleted]]

