Search code examples
pythonnumpymatrixscipynormal-distribution

Generating a normalized matrix of random numbers which rows are generated from different Gaussian (normal) distributions


I am trying to write a generator in python which generates a N*M matrix of random numbers using gaussian (normal) distribution.

given is N and M and the number of Gaussian random fields (normal distribution). The numbers in the matrix should also be between 0 and 1. For example, a 100 * 80 matrix of float numbers between 0 and 1, which are generated from 5 different Gaussian distributions. The mean and variance of these distributions can be chosen arbitrarily. It is also arbitrary which and how many rows are generated by which distribution. The important thing is that the numbers in a row are generated in the same normal distribution. (Or rather, each row is a point that exists in a specific multivariate gaussian distribution)

I have already tried scipy.stats.truncnorm. Here I do not know how to generate the rows by different distribution and num.random.multivariate_normal is too complex to understanding. I have been looking for a long time and can find neither a good possibility to pass the restriction with the numbers between 0 and 1 nor find a way to generate the numbers from different normal distributions.


Solution

  • Considering that you already have sigma and mu (indicating which row will be using which distribution):

    sigma = np.array((1, 2, 3))
    mu = np.array((-1, -2, -3))
    

    Just build the resulting distribution (3 rows/different disctributions, 10 columns here):

    samples = np.random.standard_normal((3, 10)) * sigma[:, None] + mu[:, None]
    

    Be aware that Gaussian distribution are unbounded, so you have to clip:

    samples = np.clip(samples, 0, 1)
    

    Of course, depending on the values of sigma and mu, you won't get Gaussian distributions of your values.