Mack - 10 months ago 150

Python Question

Using Python, how can I sample data from a multivariate log-normal distribution? For instance, for a multivariate normal, there are two options. Let's assume we have a 3 x 3 covariance matrix and a 3-dimensional mean vector mu.

`# Method 1`

sample = np.random.multivariate_normal(mu, covariance)

# Method 2

L = np.linalg.cholesky(covariance)

sample = L.dot(np.random.randn(3)) + mu

I found numpy's numpy.random.lognormal, but that only seems to work for univariate samples. I also noticed scipy's scipy.stats.lognorm. This does seem to have the potential for multivariate samples. However, I can't figure out how to do this.

Answer

A multivariate lognormal distributed random variable `Rv`

should have this property: `log(Rv)`

should follow a normal distribution. Therefore, the problem is really just to generation a random variable of multivariate normal distribution and `np.exp`

it.

```
In [1]:
import numpy.random as nr
In [2]:
cov = [[1.0, 0.2, 0.3,],
[0.2, 1.0, 0.3,],
[0.3, 0.3, 1.0]]
cov = np.array(cov)
In [3]:
mu = np.log([0.3, 0.4, 0.5])
In [4]:
rv = nr.multivariate_normal(mu, cov, size=5)
rv
Out[4]:
array([[-1.36808854, -1.32562291, -1.9706876 ],
[-2.13119289, 1.28146425, 0.66000019],
[-2.82590272, -1.22500654, -0.32635701],
[-0.4967589 , -0.34469589, -2.04084115],
[-0.85590235, -1.27133544, -0.70959595]])
#5 points in the resultant np.array
```

`np.exp(rv)`

would be multivariate lognormal distributed.