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
Best Answer
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.