[R] hep on arithmetic covariance conversion to log-covariance

Berend Hasselman bhh at xs4all.nl
Sat Oct 13 19:05:58 CEST 2012


On 13-10-2012, at 13:21, Andras Farkas wrote:

> Dear All,
>  
> is there a function in R that would help me convert a covariance matrix built based on arithmetic returns to a covariance matrix from log-returns?
>  
> As an example of the means and covariance from arithmetic: 
>  
> mu <-c(0.094,0.006,1.337,1.046,0.263)
> sigma <-matrix(c(0.0037,-0.0001,-0.0370,-0.0136,0.0026,-0.0001,0.0001,0.0008,-0.0015,-0.0011,-0.0370,0.0008,1.0466,0.7208,-0.0455,-0.0136,-0.0015,0.7208,1.1717,0.0346,0.0026,-0.0011,-0.0455,0.0346,0.0348),byrow=TRUE,ncol=5)
>  
> which I would like to conver to a covariance matrix from a log return. My solution probably should be similar to aprevious post at http://stackoverflow.com/questions/7663690/log-covariance-to-arithmetic-covariance-matrix-function, but the other way around? 
> I would greatly apreciate the help,

An answer can be found here: http://stats.stackexchange.com/questions/18607/function-to-convert-arithmetic-to-log-based-covariance-matrix
(Found by googling on the words: arithmetic covariance to log covariance).

R code:

# Convert arithmetic returns to log return3

logreturn <- function(am,asigma) {
    M <- 1/(1+am)

    S <- log( diag(M) %*% asigma %*% diag(M) + 1 )
    mu <- log(1+am) - diag(S)/2
    
    list(mean=mu, vcov=S)
}

# Convert log returns to arithmetic returns

linreturn <- function(mu,Sigma) {
  m <- exp(mu+diag(Sigma)/2)-1
  x1 <- outer(mu,mu,"+")
  x2 <- outer(diag(Sigma),diag(Sigma),"+")/2
  S <- exp(x1+x2)*(exp(Sigma)-1)
  list(mean=m,vcov=S)
}

mu <- c(0.094,0.006,1.337,1.046,0.263)
sigma <- matrix(c(0.0037,-0.0001,-0.0370,-0.0136,0.0026,-0.0001,0.0001,0.0008,-0.0015,
                  -0.0011,-0.0370,0.0008,1.0466,0.7208,-0.0455,-0.0136,-0.0015,0.7208,
                  1.1717,0.0346,0.0026,-0.0011,-0.0455,0.0346,0.0348),byrow=TRUE,ncol=5)

# Run both functions

z <- logreturn(mu, sigma)
z
linreturn(z$mean,z$vcov)

Result is:

# > z <- logreturn(mu, sigma)
# > z
# $mean
# [1] 0.088297346 0.005932669 0.761207033 0.592495120 0.222699189
# 
# $vcov
#               [,1]          [,2]          [,3]          [,4]          [,5]
# [1,]  3.086716e-03 -9.086663e-05 -0.0145776440 -0.0060945085  0.0018799417
# [2,] -9.086663e-05  9.880583e-05  0.0003402197 -0.0007290309 -0.0008661227
# [3,] -1.457764e-02  3.402197e-04  0.1753220459  0.1404118270 -0.0155352556
# [4,] -6.094509e-03 -7.290309e-04  0.1404118270  0.2467830943  0.0133007368
# [5,]  1.879942e-03 -8.661227e-04 -0.0155352556  0.0133007368  0.0215813095
# 
# > linreturn(z$mean,z$vcov)
# $mean
# [1] 0.094 0.006 1.337 1.046 0.263
# 
# $vcov
#         [,1]    [,2]    [,3]    [,4]    [,5]
# [1,]  0.0037 -0.0001 -0.0370 -0.0136  0.0026
# [2,] -0.0001  0.0001  0.0008 -0.0015 -0.0011
# [3,] -0.0370  0.0008  1.0466  0.7208 -0.0455
# [4,] -0.0136 -0.0015  0.7208  1.1717  0.0346
# [5,]  0.0026 -0.0011 -0.0455  0.0346  0.0348

Berend




More information about the R-help mailing list