0

I am newbie to numpy and recently I got very confused on the random.normal method I would like to generate a 2 by 2 matrix where the mean is zero so I wrote the following, however, as you can see the abs(0 - np.mean(b)) < 0.01 line outputs False, why? I expect it to output True.

>>> import numpy as np
>>> b = np.random.normal(0.0, 1.0, (2,2))
>>> b
array([[-1.44446094, -0.3655891 ],
   [-1.15680584, -0.56890335]])
>>> abs(0 - np.mean(b)) < 0.01
False
6
  • It's random. Why would the mean be a specific value? Especially with only 4 samples? Commented Jun 23, 2017 at 6:18
  • @spectras so it means this API cannot guarantee the results to be of the mean value I specified if the size is small? Commented Jun 23, 2017 at 6:19
  • look up variance. it will tell you how far from the distribution mean the mean of your sample can stray. the sample size is a parameter there! Commented Jun 23, 2017 at 6:22
  • That's not related to the API. Random sampling works this way. If you increase the sample size, the probability that you'll have a sample mean close to the distribution mean will be very large but even then it is not going to be certain. resources.infosecinstitute.com/wp-content/uploads/… Commented Jun 23, 2017 at 6:22
  • @peitipeyerli> are you familiar with randomness? Are you aware getting ((1, 1)(1, 1)) as a result is a perfectly valid outcome? The average of samples only approaches the center of the distribution if you pick a large amount of samples. Like tossing a coin: on average you'll get same amount of heads and tails. Yet it's possible you get 10 tails in a row. Or 20. Or 1000, it's less probable but still possible. It's a statistical average, not a guarantee. Commented Jun 23, 2017 at 6:28

2 Answers 2

1

If you want a generator, you'll need to manually fix the mean and std to your expected values:

def normal_gen(m, s, shape=(2,2)):
    b = np.random.normal(0, s, shape)
    b = (b - np.mean(b)) * (s / np.std(b)) + m
    return b
Sign up to request clarification or add additional context in comments.

Comments

0

Sampling from a normal distribution does not guarantee that the mean of your sample is the same as the mean of the normal distribution. If you take an infinite number of samples, it should have the same mean (via the Central Limit Theorem) but obviously, you can't really take an infinite number of samples.

1 Comment

I totally misunderstood the API. It is sampling from a pool, not generate a fixed size pool for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.