# Utility Functions¶

## Function Reference¶

qinfer.utils.binomial_pdf(N, n, p)[source]

Returns the PDF of the binomial distribution $$\operatorname{Bin}(N, p)$$ evaluated at $$n$$.

qinfer.utils.outer_product(vec)[source]

Returns the outer product of a vector $$v$$ with itself, $$v v^\T$$.

qinfer.utils.particle_meanfn(weights, locations, fn=None)[source]

Returns the mean of a function $$f$$ over model parameters.

Parameters: weights (numpy.ndarray) – Weights of each particle. locations (numpy.ndarray) – Locations of each particle. fn (callable) – Function of model parameters to take the mean of. If None, the identity function is assumed.
qinfer.utils.particle_covariance_mtx(weights, locations)[source]

Returns an estimate of the covariance of a distribution represented by a given set of SMC particle.

Parameters: weights – An array containing the weights of each particle. location – An array containing the locations of each particle. numpy.ndarray, shape (n_modelparams, n_modelparams). An array containing the estimated covariance matrix.
qinfer.utils.in_ellipsoid(x, A, c)[source]

Determines which of the points x are in the closed ellipsoid with shape matrix A centered at c. For a single point x, this is computed as

$(c-x)^T\cdot A^{-1}\cdot (c-x) \leq 1$
Parameters: x (np.ndarray) – Shape (n_points, dim) or n_points. A (np.ndarray) – Shape (dim, dim), positive definite c (np.ndarray) – Shape (dim) bool or array of bools of length n_points
qinfer.utils.ellipsoid_volume(A=None, invA=None)[source]

Returns the volume of an ellipsoid given either its matrix or the inverse of its matrix.

qinfer.utils.mvee(points, tol=0.001)[source]

Returns the minimum-volume enclosing ellipse (MVEE) of a set of points, using the Khachiyan algorithm.

qinfer.utils.uniquify(seq)[source]

Returns the unique elements of a sequence seq.

qinfer.utils.format_uncertainty(value, uncertianty, scinotn_break=4)[source]

Given a value and its uncertianty, format as a LaTeX string for pretty-printing.

Parameters: scinotn_break (int) – How many decimal points to print before breaking into scientific notation.
qinfer.utils.to_simplex(y)[source]

Interprets the last index of y as stick breaking fractions in logit space and returns a non-negative array of the same shape where the last dimension always sums to unity.

A unit simplex is a list of non-negative numbers $$(x_1,...,x_K)$$ that sum to one, $$\sum_{k=1}^K x_k=1$$, for example, the probabilities of an K-sided die. It is sometimes desireable to parameterize this object with variables that are unconstrained and “decorrelated”. To this end, we imagine $$\vec{x}$$ as a partition of the unit stick $$[0,1]$$ with $$K-1$$ break points between $$K$$ successive intervals of respective lengths $$(x_1,...,x_K)$$. Instead of storing the interval lengths, we start from the left-most break point and iteratively store the breaking fractions, $$z_k$$, of the remaining stick. This gives the formula $$z_k=x_k / (1-\sum_{k'=1}^{k-1}x_k)$$ with the convention $$x_0:=0$$, which has an inverse formula $$x_k = z_k(1-z_{k-1})\cdots(1-z_1)$$. Note that $$z_K=1$$ since the last stick is not broken; this is the result of the redundant information imposed by $$\sum_{k=1}^K x_k=1$$. To unbound the parameters $$z_k$$ into the real line, we pass through the logit function, $$\operatorname{logit}(p)=\log\frac{p}{1-p}$$, to end up with the parameterization $$y_k=\operatorname{logit}(z_k)+\log(K-k)$$, with the convention $$y_K=0$$. The shift by $$\log(K-k)$$ is largely asthetic and causes the uniform simplex $$\vec{x}=(1/K,1/K,...,1/K)$$ to be mapped to $$\vec{x}=(0,0,...,0)$$.

Inverse to from_simplex().

Parameters: np.ndarray – Array of logit space stick breaking fractions along the last index. np.ndarray
qinfer.utils.from_simplex(x)[source]

Inteprets the last index of x as unit simplices and returns a real array of the sampe shape in logit space.

Inverse to to_simplex() ; see that function for more details.

Parameters: np.ndarray – Array of unit simplices along the last index. np.ndarray