statistics.NormalDist Computes Normal Probabilities

Build a statistics.NormalDist from mean and standard deviation, then read PDF, CDF, and inverse CDF values for a normal distribution.

PythonData Integrity

Code

pythonsnippet
25 lines
from statistics import NormalDist


def main() -> None:
    distribution = NormalDist(mu=100, sigma=15)
    print("mean:", distribution.mean)
    print("stdev:", distribution.stdev)

    for score in (85, 100, 115):
        density = distribution.pdf(score)
        cumulative = distribution.cdf(score)
        print(score, "pdf:", round(density, 6), "cdf:", round(cumulative, 4))

    # inv_cdf maps a probability back to the value at that quantile.
    print("90th percentile:", round(distribution.inv_cdf(0.90), 2))

    # Adding independent normal distributions combines mean and variance.
    second = NormalDist(mu=50, sigma=5)
    combined = distribution + second
    print("combined mean:", combined.mean)
    print("combined stdev:", round(combined.stdev, 2))


if __name__ == "__main__":
    main()

Output

Captured from a sandboxed run of this snippet in the build pipeline.

mean: 100.0
stdev: 15.0
85 pdf: 0.016131 cdf: 0.1587
100 pdf: 0.026596 cdf: 0.5
115 pdf: 0.016131 cdf: 0.8413
90th percentile: 119.22
combined mean: 150.0
combined stdev: 15.81

How it works

statistics.NormalDist Computes Normal Probabilities keeps everything in the function main.

Iteration happens inside for score in (85, 100, 115) — everything outside it runs once. NormalDist are the only symbols borrowed from statistics — the coupling is explicit and narrow.

Points of care

  • Dependency check: statistics — not built-in, must be present in the project.

Putting it to use

The main signature reads def main() -> None

The run emits 3 labeled sections — mean:, stdev:, 90th percentile: — one per step.

Interactive Tools for This Topic

FAQ

What does statistics.NormalDist Computes Normal Probabilities print when run?
Execute the whole file; each printed line is prefixed with a label like `mean:`, `stdev:`, `90th percentile:`.