# Function to convert arithmetic to log-based covariance matrix?

Is there a function in R that will take the mu and Sigma of an arithmetic-covariance matrix and return the mu and Sigma of a log-based covariance matrix?

I have the code for a function implementing the reverse -- from log-covariance to linear covariance -- in R (pasted below in case this is helpful). Note that this code implement's Meucci's math on Appendix page 5 of the attached.:

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)
}


Simulation code validating the above approach:

# Experiment with two assets

# initialize with average log returns and log-based covariance matrix
m1 <- c( .05 , .12 , .1 )
S1 <- matrix( c( .1 , .05 , .02 , .05 , .1 , .03 , .02 , .03 , .1 ), nrow = 3 )

# simulate log-return draws from log-based covariance matrix assuming normal distribution
set.seed(1001)
library(MASS)
logReturns <- MASS::mvrnorm(2000000,mu=m1,Sigma=S1)

# convert to arithmetic returns
arithmeticReturn = exp( logReturns ) - 1
colMeans( arithmeticReturn )
# create arithmetric based covariance matrix
var( arithmeticReturn )

# compare simulation results with linreturn function
linreturn( m1, S1 )


Alternatively, is there a function in MATLAB that performs the procedure? (I could analyze the open-source and port this to R.)

Thanks

-
Good question. Would this be better on the stats.SE site? Though it is R/Matlab in nature, it's primarily statistical in nature. If I were looking up answers to this question, I'd head to CV rather than SO. –  Iterator Nov 18 '11 at 21:33
Great point. I'll flag for the moderator to move this to CV –  Quant Guy Nov 18 '11 at 21:37
Glad you agree. If nothing else, I wanted to flag to move it because I much prefer to see math in LaTeX - an irritating shortcoming of SO (see this meta comment). –  Iterator Nov 18 '11 at 21:59
That code looks incorrect: it should not be subtracting 1 in the expression for m. To see why this is wrong, consider the case where mu is very negative: this would cause the exponential to be less than 1, creating a negative value for m, which is impossible. –  whuber Nov 18 '11 at 22:37
@whuber that formula is based on formula (7) of Meucci's paper : wilmott.com/pdfs/011119_meucci.pdf . I have also added simulation code showing that the above procedure is correct. R returns a zero when exp() evaluates a large negative number. Perhaps there are cases where this translation is invalid? –  Quant Guy Nov 18 '11 at 22:45

## migrated from stackoverflow.comNov 18 '11 at 22:07

This question came from our site for professional and enthusiast programmers.

If I have understood the code correctly (ignoring the "$-1$" in the computation of $m$), its input is an $n$-vector $\mu = (\mu_1, \ldots, \mu_n)$ and a symmetric $n$ by $n$ matrix $\Sigma = (\sigma_{ij})$. The output is an $n$-vector $m$ with

$$m_i = \exp(\mu_i + \sigma_{ii}/2)$$

and an $n$ by $n$ matrix $S$ with

$$S_{ij} = \exp(\mu_i + \mu_j + (\sigma_{ii}+\sigma_{jj})/2)(\exp(\sigma_{ij})-1) = m_i(\exp(\sigma_{ij})-1)m_j.$$

If this is correct, then we can solve readily for $\mu$ and $\Sigma$ in terms of $m$ and $S$ essentially by reversing these operations. Begin by forming the diagonal matrix $M$ whose diagonal entries are $1/m_i$: that is, $M_{ii}=1/m_i$ and $M_{ij}=0$ for $i\ne j$. From the right hand side of the preceding formula it follows immediately that

$$M S M + 1_n = \exp(\sigma_{ij})$$

and we easily recover $\Sigma$ by taking the logarithms term-by-term. With these values in hand,

$$\mu_i = \log(m_i) - \sigma_{ii}/2.$$

### Edit

The code in the question uses "linear returns" rather than means. There's no problem with that: starting with the "returns" $m_i$ computed as $\exp(\mu_i + \sigma_{ii}/2)-1$, first add back the $1$ and proceed as above.

-
thanks! sorry for the delay –  Quant Guy Nov 28 '11 at 15:18