Work with normal distributions through statistics.NormalDist.
What It Does and Why It Matters
NormalDist(mu=0.0, sigma=1.0) provides normal probability density (pdf), cumulative distribution (cdf), inverse cdf (inv_cdf), sample generation, and ways to combine independent normal variables. The standard deviation must be nonnegative.
NormalDist describes an assumed model; it does not prove that observed data are normally distributed.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
from statistics import NormalDist
standard_normal = NormalDist()
print(round(standard_normal.cdf(0), 3))
print(round(standard_normal.inv_cdf(0.975), 3))
How the Code Works
- The imports make these standard-library tools available: statistics.
- The name standard_normal receives the value of NormalDist(). Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- The calculation uses the exact sample values shown in the code. Interpret the result using the required dataset assumptions (for example, paired values, positive numbers or an adequate sample size) rather than treating every numerical result as interchangeable.
Expected output:
0.5
1.96
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
Second Example: Compare Two Cases
Use a different input or a related operation to observe an important difference. The two examples help separate the general rule from one particular result.
import statistics
print(round(statistics.NormalDist().cdf(0), 3))
Expected output:
0.5
Where You Would Use This
Use this in calculations, data summaries, simulations or validation when the numerical assumptions match the task. Check input domains, units and precision before interpreting a result. A single example output does not prove a statistical conclusion.
Practice Exercise
Estimate the probability that a normal measurement falls below a threshold.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Confusing the probability density pdf() with a probability over an interval.
- Using a numeric result without checking empty data, invalid ranges, precision and the meaning of its units.
Key Takeaways
- Work with normal distributions through statistics.NormalDist.
- NormalDist(mu=0.0, sigma=1.0) provides normal probability density (pdf), cumulative distribution (cdf), inverse cdf (inv_cdf), sample generation, and ways to combine independent normal variables.
- Estimate the probability that a normal measurement falls below a threshold.