Generate large Gaussian distributed vector

I need to generate a large random vector, whose entries are independent but not identically distributed. Each entry has its own mean and variance. We could loop over the entries and sample a scalar Gaussian distribution, but that would need many function calls, slowing down the speed.

Alternatively, I could use the ``torch.distributions.multivariate_normal()`’, but the co-variance matrix was large enough to cause an out-of-memory exception.

Thanks.

Hello Liangping!

Let’s assume your large random vector is to be of length N, and
that you have your desired per-element means and standard
deviations (standard deviation = sqrt (variance)) in the existing
length-N vectors means and stdevs.

Then:

rvec = torch.randn (N)
rvec.mul_ (stdevs)
rvec.add_ (means)

torch.randn() gives you a vector with normally (mean = 0,
stdev = 1), independently and identically distributed elements.
We then multiply rvec, element-wise, by stdevs to give each
of its elements its own desired standard deviation (and, hence,
variance). Lastly, we add, element-wise, to give each element
its desired mean.

Good luck.

K. Frank

Hi Frank,

That is an elegant idea! I tried it out and it sped up by a factor of more than 5000!

Thanks!

Liangping