0

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.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Welcome, please elaborate a little. Give us a small sample of your expected output and your input. Do you want to generate a random matrix where each element is randomly chosen from five normal distributions and "clip" them between 0 and 1? – Tarifazo Jan 11 '19 at 12:31
  • Have you considered creating the 5 different distributions as individual arrays, then using numpy `vstack` or `concatenate` to combine them into a single array (matrix)? – kcw78 Jan 11 '19 at 13:54
  • @kcw78 I know that i can generate arrays with `numpy.random.randn` and arbitrary _sigma_ and _mu_ but I dont know how I can solve that ristriction with having numbers between 0 and 1. – morteza rahimi Jan 11 '19 at 14:21
  • @Mstaino Input: N (number of rows) , M (number of columns), x= number of different normal distribution with differen _sigma_ and _mu_ ; Output: a N*M Matrix, that the row _r1_ is generated from normal distribution with _sigma1_ and _mu1_, row _r2_ is generated from normal distribution with _sigma2_ and _mu2_ and etc. ristriction: the elements of matrix are numbers _n_ with 0<_n_<1 like [0.12 , 0.19, 0.39]. – morteza rahimi Jan 11 '19 at 14:40
  • Normalize the array: find the max in the returned array with `numpy.amax` then divide. Or try `numpy.random.standard_normal`. It returns values with mean=0, and std_dev=1. – kcw78 Jan 11 '19 at 15:34

1 Answers1

0

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.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62