typestar

Distributions in Python

A frozen distribution answers pdf, cdf and ppf without repeating parameters.

import numpy as np
from scipy import stats

tpm = stats.norm(loc=95, scale=12)

print(round(tpm.pdf(95), 5), round(tpm.cdf(107), 4))
print(round(tpm.ppf(0.95), 2), tpm.interval(0.95)[0].round(2))
print(tpm.mean(), tpm.std(), round(tpm.var(), 1))

rng = np.random.default_rng(0)
print(tpm.rvs(size=4, random_state=rng).round(1))

How it works

  1. norm(loc, scale) freezes the parameters into one object.
  2. cdf goes value to probability, ppf goes back.
  3. rvs samples, seeded by a Generator for reproducibility.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
327
Tokens
132
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 79 seconds.

Type this snippet

Step 1 of 3 in Distributions, step 1 of 23 in Scientific computing with SciPy.

Next →

Distributions in other languages