user3639557
user3639557

Reputation: 5271

creating a normally distributed matrix in numpy with specified mean and variance per element

Assume 3 matrices Mean, Variance, Sample all with the same dimensionality. Is there a 1 line solution to generate the Sample matrix in numpy such that:

 Sample[i,j] is drawn from NormalDistribution(Mean[i,j], Variance[i,j])

Upvotes: 0

Views: 38

Answers (1)

P. Camilleri
P. Camilleri

Reputation: 13218

Using linearity of mean and Var(aX +b) = a**2 Var(X):

Generate a centered and reduced 2D array with np.random.randn(). Multiply pointwise by std (np.sqrt(variance)) and add (still pointwise) mean.

Upvotes: 2

Related Questions