Search code examples
luanormal-distributiontorch

How to draw samples from a multivariate Gaussian in Lua/Torch


I turns out Torch does not have any built-in function for drawing samples for a multivariate Gaussian distribution given a desired covariance matrix. Can anyone tell me how I can draw samples from a multivariate Gaussian distribution using a desired covariance matrix?

Here's my try:

I installed the randomkit package from here using luarocks install randomkit in terminal. However, when I do require 'randomkit' in Lua I cannot find the multivariate_normal function as one of the elements of randomkit. Am I doing something wrong?


Solution

  • You can use the torch-distributions package. To install it, run the following command in terminal:

    luarocks install https://raw.github.com/jucor/torch-distributions/master/distributions-0-0.rockspec
    

    Below is a working example in lua to draw samples from a multivariate normal distribution:

    require 'distributions'
    mu = torch.Tensor({10, 0})
    sigma = torch.eye(2)
    sample = distributions.mvn.rnd(mu, sigma) -- a sample from the distribution