Keyreall
Keyreall

Reputation: 97

Generate a distribution in python with given mean and std

I'm trying to create a distribution with given mean and std. But I can't generate a distribution with exact mean and std. For example:

import numpy as np
np.random.seed(0)
group = np.random.normal(loc=10,scale=5,size=50)
print(group.std(),group.mean()) # 5.62761460277423 10.702796361565493

I need exact mean and standard deviation that I put into brackets. Which function do I need to use to create a distribution with fixed mean and std instead of .normal

Upvotes: 2

Views: 3964

Answers (2)

Tom271
Tom271

Reputation: 311

To follow from forgetso's answer (which follows from the Law of Large Numbers), to shift your random sample so that it has the exact mean and standard deviation, you can standardise the values to mean 0 standard deviation 1 and then shift it to your desired values

>>> import numpy as np
>>> np.random.seed(0)
>>> group = np.random.normal(loc=10,scale=5,size=50)
>>> print(group.std(),group.mean())
5.62761460277423 10.702796361565493
>>> group_standardised = (group - group.mean()) / group.std()
>>> print(group_standardised.std(),group_standardised.mean())
1.0 -6.483702463810914e-16
>>> desired_std = 5
>>> desired_mean = 10
>>> group_scaled = group_standardised * desired_std + desired_mean
>>> print(group_scaled.std(),group_scaled.mean())
5.0 9.999999999999996

Upvotes: 3

forgetso
forgetso

Reputation: 2484

The mean of the group is 10 and the std 5 if you take size to infinity. At the moment, you are drawing size=50 samples. You will get closer to the desired values as you increase the value of size.

>>> group = np.random.normal(loc=10,scale=5,size=50000)
>>> print(group.std(),group.mean())
5.000926728104604 9.999396725329085

Upvotes: 3

Related Questions