Is there a torch function that takes in as input the mean tensor and the covariance tensor of a multivariate normal distribution and returns samples from the distribution?

I noticed that there is torch.normal but it takes per-element standard deviation and doesn’t take in correlation, I assume.

Hi @vvanirudh, I’m not sure whether you’re familiar with the phenomena, but when you train nets or statistical model which use multivariate normalized distributions, usually something called “factor rotation” occurs.

Basically, all the correlations will go to zero, so usually you don’t have to worry about modelling the full co-variance matrix - it simplifies things considerably - maybe this would be a good starting point?

Hi @AjayTalati, I wasn’t planning to model the full covariance matrix in the way you mention. I am currently predicting the parameters of a 2D XY gaussian distribution (mean_x, mean_y, std_x, std_y and corr), from which I subsequently sample to get the input at the next time-step. For this I need to have access to a function that can sample from the full 2D gaussian distribution (like the np.random.multivariate_normal function, but a torch analog if one exists)

import torch
import numpy
# take target covariance
cov = numpy.array(((1,0.5),(0.5,1)), dtype=numpy.float32)
# compute cholesky factor in numpy
l = torch.from_numpy(numpy.linalg.cholesky(cov))
# sample standard normal random and multiply
rnd = torch.mm(l,torch.randn(2,10000))
# check covariance in numpy
print (numpy.cov(rnd.numpy()))