How to calculate probability in normal distribution given mean, std in Python? I can always explicitly code my own function according to the definition like the OP in this question did: Calculating Probability of a Random Variable in a Distribution in Python
Just wondering if there is a library function call will allow you to do this. In my imagine it would like this:
nd = NormalDistribution(mu=100, std=12) p = nd.prob(98)
There is a similar question in Perl: How can I compute the probability at a point given a normal distribution in Perl?. But I didn't see one in Python.
Numpy
has a random.normal
function, but it's like sampling, not exactly what I want.
The probability of P(a < Z < b) is calculated as follows. Then express these as their respective probabilities under the standard normal distribution curve: P(Z < b) – P(Z < a) = Φ(b) – Φ(a). Therefore, P(a < Z < b) = Φ(b) – Φ(a), where a and b are positive.
To find the probability of LARGER z-score, which is the probability of observing a value greater than x (the area under the curve to the RIGHT of x), type: =1 - NORMSDIST (and input the z-score you calculated).
There's one in scipy.stats:
>>> import scipy.stats >>> scipy.stats.norm(0, 1) <scipy.stats.distributions.rv_frozen object at 0x928352c> >>> scipy.stats.norm(0, 1).pdf(0) 0.3989422804014327 >>> scipy.stats.norm(0, 1).cdf(0) 0.5 >>> scipy.stats.norm(100, 12) <scipy.stats.distributions.rv_frozen object at 0x928352c> >>> scipy.stats.norm(100, 12).pdf(98) 0.032786643008494994 >>> scipy.stats.norm(100, 12).cdf(98) 0.43381616738909634 >>> scipy.stats.norm(100, 12).cdf(100) 0.5
[One thing to beware of -- just a tip -- is that the parameter passing is a little broad. Because of the way the code is set up, if you accidentally write scipy.stats.norm(mean=100, std=12)
instead of scipy.stats.norm(100, 12)
or scipy.stats.norm(loc=100, scale=12)
, then it'll accept it, but silently discard those extra keyword arguments and give you the default (0,1).]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With