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_elemelements, 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
piscaled 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 ofn_samplesindependent samples.
- is_uniform(pi: ndarray, mrd: FloatOrRat = def_mrd) bool¶
Whether
piis the uniform distribution \(\vartheta\), up tomrd.
- is_proper(pi: ndarray, mrd: FloatOrRat = def_mrd) bool¶
Whether
piis a proper distribution: its elements are non-negative and sum to \(1\) (up tomrd).
- assert_proper(pi: ndarray) None¶
Raises
RuntimeErrorifpiis not a proper distribution (seeis_proper()).
- to_grid(pi: ndarray, width: int) ndarray¶
For a distribution
pion the cells of a grid withwidthcolumns, where cell \((x,y)\) has indexy*width + x(as inqif.point.from_cell()), returns the grid as a matrixMwithM[y, x]being the probability of cell \((x,y)\).