'Which method should I use to sample from a normal distribution?

I'm trying to sample batch_size points from an N-dim standard Gaussian distribution. But I noticed there are two similar functions I can use, and I want to know which one is correct or both.

Assume I want to sample 8 points from a 2-dim standard Gaussian.

  1. torch.distributions.MultivariateNormal(torch.zeros(2), torch.eye(2)).sample([8]), it will return a tensor of size [8,2]
  2. torch.randn(8,2)

They return similar outputs, but I'm wondering whether they're the same.



Solution 1:[1]

torch.randn gives you samples from a univariate standard normal distribution and reshapes them to the desired shape. So the mean of all the samples is 0 with unit variance.

x = torch.randn(1000000,2).numpy()
assert np.isclose(np.mean(x.flatten()), 0, atol=0.01)
plt.hist(x.flatten())

enter image description here

MultivariateNormal generates samples from a multivariate normal distribution. It can be parameterized by a mean vector and a covariance matrix.

x = torch.distributions.MultivariateNormal(
    torch.zeros(2), torch.eye(2)).sample([10000]).numpy()
assert np.isclose(np.mean(x.flatten()), 0, atol=0.01)
plt.hist(x.flatten())

The above usage is sort of a hack; we are creating two standard normals in two different dimensions and since the distributions (mean and variance) are the same we can consider them to be as well from a single distribution and hence the flattened array can be considered to be from a univariate standard normal. enter image description here

The real purpose of MultivariateNormal:

x = torch.distributions.MultivariateNormal(
    torch.tensor([-2.0, 2.0]), torch.eye(2)).sample([10000]).numpy()
plt.hist(x[:, 0])
plt.hist(x[:, 1])

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujjiga