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