Probability distributions

qif.probab

A distribution \(\pi\) on a finite set \(\mathcal X = \{0,\ldots,n{-}1\}\) is a 1-dimensional numpy array of length \(n\), with pi[x] \(= \pi_x\). Elements are double or rat; the functions that construct distributions take a type argument, defaulting to the one given to qif.set_default_type().

For double elements, comparisons are made up to a maximum relative difference mrd (default def_mrd \(= 100\epsilon\), with \(\epsilon\) the machine epsilon); mrd = 0 means exact comparison. For rat elements the default is 0.

Functions

uniform(n_elem: int, type: TypeLike = def_type) → ndarray

The uniform distribution \(\vartheta\) on n_elem elements, that is \(\vartheta_x = 1/n\).

point(n_elem: int, x: int = 0, type: TypeLike = def_type) → ndarray

The point distribution \([x]\), giving probability \(1\) to x.

randu(n_elem: int, type: TypeLike = def_type) → ndarray

A distribution chosen uniformly at random from \(\mathbb D\mathcal X\) (the probability simplex).

normalize(pi: ndarray) → ndarray

Returns pi scaled to sum to \(1\), that is \(\pi_x / \sum_{x'} \pi_{x'}\).

sample(pi: ndarray) → int
sample(pi: ndarray, n_samples: int) → ndarray

Draws an element \(x\) from pi. Returns a single element, or an array of n_samples independent samples.

is_uniform(pi: ndarray, mrd: FloatOrRat = def_mrd) → bool

Whether pi is the uniform distribution \(\vartheta\), up to mrd.

is_proper(pi: ndarray, mrd: FloatOrRat = def_mrd) → bool

Whether pi is a proper distribution: its elements are non-negative and sum to \(1\) (up to mrd).

assert_proper(pi: ndarray) → None

Raises RuntimeError if pi is not a proper distribution (see is_proper()).

to_grid(pi: ndarray, width: int) → ndarray

For a distribution pi on the cells of a grid with width columns, where cell \((x,y)\) has index y*width + x (as in qif.point.from_cell()), returns the grid as a matrix M with M[y, x] being the probability of cell \((x,y)\).

from_grid(grid: ndarray) → ndarray

The inverse of to_grid(): returns the distribution pi with pi[y*width + x] = grid[y, x], where width is the number of columns of grid.