Search code examples
pytorch

How to sample from normal distribution?


If I have n sized mean vector, n sized variance vector, then how do I do this?

z ∼ N (μ, σ)
import torch
x = torch.randn(3, 3)
mu = x.mean()
sigma = x.var()

What do I do to get z?


Solution

  • If you want to sample from a normal distribution with mean mu and std sigma then you can simply

    z = torch.randn_like(mu) * sigma + mu
    

    If you sample many such z their mean and std will converge to sigma and mu:

    mu = torch.arange(10.)
    
    Out[]: tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
    
    sigma = 5. - 0.5 * torch.arange(10.)
    
    Out[]: tensor([5.0000, 4.5000, 4.0000, 3.5000, 3.0000, 2.5000, 2.0000, 1.5000, 1.0000, 0.5000])
    
    z = torch.randn(10, 1000000) * sigma[:, None] + mu[:, None]
    
    z.mean(dim=1)
    Out[]:
    tensor([-5.4823e-03,  1.0011e+00,  1.9982e+00,  2.9985e+00,  4.0017e+00,
             4.9972e+00,  6.0010e+00,  7.0004e+00,  7.9996e+00,  9.0006e+00])
    
    z.std(dim=1)
    Out[]:
    tensor([4.9930, 4.4945, 4.0021, 3.5013, 3.0005, 2.4986, 1.9997, 1.4998, 0.9990,
            0.5001])
    

    As you can see when you sample 1,000,000 elements from the distribution the sample mean and std are close to the original mu and sigma you started with.