typestar

Discrete distributions in Python

Binomial, Poisson and the questions they answer about counts.

from scipy import stats

runs = stats.binom(n=20, p=0.3)
print(round(runs.pmf(6), 4), round(runs.cdf(6), 4), round(runs.sf(10), 5))
print(runs.mean(), round(runs.std(), 3))

errors = stats.poisson(mu=1.4)
print(round(errors.pmf(0), 4), round(errors.sf(4), 5))

test = stats.binomtest(k=14, n=20, p=0.5)
print(round(test.pvalue, 5), test.proportion_estimate)

How it works

  1. pmf is the discrete twin of pdf.
  2. sf is the survival function: one minus the cdf, but accurate.
  3. binom_test moved: binomtest returns a result object.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
357
Tokens
144
Three-star pace
100 tpm

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

Type this snippet

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

← Previous Next →