# Probability Distributions¶

## Distribution - Abstract Base Class for Probability Distributions¶

class qinfer.Distribution[source]

Bases: object

Abstract base class for probability distributions on one or more random variables.

n_rvs

The number of random variables that this distribution is over.

Type: int
sample(n=1)[source]

Returns one or more samples from this probability distribution.

Parameters: n (int) – Number of samples to return. numpy.ndarray An array containing samples from the distribution of shape (n, d), where d is the number of random variables.

## Specific Distributions¶

class qinfer.UniformDistribution(ranges=array([[0, 1]]))[source]

Bases: qinfer.distributions.Distribution

Uniform distribution on a given rectangular region.

Parameters: ranges (numpy.ndarray) – Array of shape (n_rvs, 2), where n_rvs is the number of random variables, specifying the upper and lower limits for each variable.
n_rvs
sample(n=1)[source]
grad_log_pdf(var)[source]
class qinfer.DiscreteUniformDistribution(num_bits)[source]

Bases: qinfer.distributions.Distribution

Discrete uniform distribution over the integers between 0 and 2**num_bits-1 inclusive.

Parameters: num_bits (int) – non-negative integer specifying how big to make the interval.
n_rvs
sample(n=1)[source]
class qinfer.MVUniformDistribution(dim=6)[source]

Bases: qinfer.distributions.Distribution

Uniform distribution over the rectangle $$[0,1]^{\text{dim}}$$ with the restriction that vector must sum to 1. Equivalently, a uniform distribution over the dim-1 simplex whose vertices are the canonical unit vectors of $$\mathbb{R}^\text{dim}$$.

Parameters: dim (int) – Number of dimensions; n_rvs.
n_rvs
sample(n=1)[source]
class qinfer.NormalDistribution(mean, var, trunc=None)[source]

Bases: qinfer.distributions.Distribution

Normal or truncated normal distribution over a single random variable.

Parameters: mean (float) – Mean of the represented random variable. var (float) – Variance of the represented random variable. trunc (tuple) – Limits at which the PDF of this distribution should be truncated, or None if the distribution is to have infinite support.
n_rvs
sample(n=1)[source]
grad_log_pdf(x)[source]
class qinfer.MultivariateNormalDistribution(mean, cov)[source]

Bases: qinfer.distributions.Distribution

Multivariate (vector-valued) normal distribution.

Parameters: mean (np.ndarray) – Array of shape (n_rvs, ) representing the mean of the distribution. cov (np.ndarray) – Array of shape (n_rvs, n_rvs) representing the covariance matrix of the distribution.
n_rvs
sample(n=1)[source]
grad_log_pdf(x)[source]
class qinfer.SlantedNormalDistribution(ranges=array([[0, 1]]), weight=0.01)[source]

Bases: qinfer.distributions.Distribution

Uniform distribution on a given rectangular region with additive noise. Random variates from this distribution follow $$X+Y$$ where $$X$$ is drawn uniformly with respect to the rectangular region defined by ranges, and $$Y$$ is normally distributed about 0 with variance weight**2.

Parameters: ranges (numpy.ndarray) – Array of shape (n_rvs, 2), where n_rvs is the number of random variables, specifying the upper and lower limits for each variable. weight (float) – Number specifying the inverse variance of the additive noise term.
n_rvs
sample(n=1)[source]
class qinfer.LogNormalDistribution(mu=0, sigma=1)[source]

Bases: qinfer.distributions.Distribution

Log-normal distribution.

Parameters: mu – Location parameter (numeric), set to 0 by default. sigma – Scale parameter (numeric), set to 1 by default. Must be strictly greater than zero.
n_rvs
sample(n=1)[source]
class qinfer.ConstantDistribution(values)[source]

Bases: qinfer.distributions.Distribution

Represents a determinstic variable; useful for combining with other distributions, marginalizing, etc.

Parameters: values – Shape (n,) array or list of values $$X_0$$ such that $$\Pr(X) = \delta(X - X_0)$$.
n_rvs
sample(n=1)[source]
class qinfer.BetaDistribution(alpha=None, beta=None, mean=None, var=None)[source]

Bases: qinfer.distributions.Distribution

The beta distribution, whose pdf at $$x$$ is proportional to $$x^{\alpha-1}(1-x)^{\beta-1}$$. Note that either alpha and beta, or mean and var, must be specified as inputs; either case uniquely determines the distribution.

Parameters: alpha (float) – The alpha shape parameter of the beta distribution. beta (float) – The beta shape parameter of the beta distribution. mean (float) – The desired mean value of the beta distribution. var (float) – The desired variance of the beta distribution.
n_rvs
sample(n=1)[source]
class qinfer.BetaBinomialDistribution(n, alpha=None, beta=None, mean=None, var=None)[source]

Bases: qinfer.distributions.Distribution

The beta-binomial distribution, whose pmf at the non-negative integer $$k$$ is equal to $$\binom{n}{k}\frac{B(k+\alpha,n-k+\beta)}{B(\alpha,\beta)}$$ with $$B(\cdot,\cdot)$$ the beta function. This is the compound distribution whose variates are binomial distributed with a bias chosen from a beta distribution. Note that either alpha and beta, or mean and var, must be specified as inputs; either case uniquely determines the distribution.

Parameters: n (int) – The $$n$$ parameter of the beta-binomial distribution. alpha (float) – The alpha shape parameter of the beta-binomial distribution. beta (float) – The beta shape parameter of the beta-binomial distribution. mean (float) – The desired mean value of the beta-binomial distribution. var (float) – The desired variance of the beta-binomial distribution.
n_rvs
sample(n=1)[source]
class qinfer.GammaDistribution(alpha=None, beta=None, mean=None, var=None)[source]

Bases: qinfer.distributions.Distribution

The gamma distribution, whose pdf at $$x$$ is proportional to $$x^{-\alpha-1}e^{-x\beta}$$. Note that either alpha and beta, or mean and var, must be specified as inputs; either case uniquely determines the distribution.

Parameters: alpha (float) – The alpha shape parameter of the gamma distribution. beta (float) – The beta shape parameter of the gamma distribution. mean (float) – The desired mean value of the gamma distribution. var (float) – The desired variance of the gamma distribution.
n_rvs
sample(n=1)[source]
class qinfer.InterpolatedUnivariateDistribution(pdf, compactification_scale=1, n_interp_points=1500)[source]

Bases: qinfer.distributions.Distribution

Samples from a single-variable distribution specified by its PDF. The samples are drawn by first drawing uniform samples over the interval [0, 1], and then using an interpolation of the inverse-CDF corresponding to the given PDF to transform these samples into the desired distribution.

Parameters: pdf (callable) – Vectorized single-argument function that evaluates the PDF of the desired distribution. compactification_scale (float) – Scale of the compactified coordinates used to interpolate the given PDF. n_interp_points (int) – The number of points at which to sample the given PDF.
n_rvs
sample(n=1)[source]
class qinfer.HilbertSchmidtUniform(dim=2)[source]

Bases: qinfer.distributions.SingleSampleMixin, qinfer.distributions.Distribution

Creates a new Hilber-Schmidt uniform prior on state space of dimension dim. See e.g. [Mez06] and [Mis12].

Parameters: dim (int) – Dimension of the state space.
n_rvs
sample()[source]
make_Paulis(paulis, d)[source]
class qinfer.HaarUniform(dim=2)[source]

Bases: qinfer.distributions.SingleSampleMixin, qinfer.distributions.Distribution

Haar uniform distribution of pure states of dimension dim, parameterized as coefficients of the Pauli basis.

Parameters: dim (int) – Dimension of the state space.

Note

This distribution presently only works for dim==2 and the Pauli basis.

n_rvs
class qinfer.GinibreUniform(dim=2, k=2)[source]

Bases: qinfer.distributions.SingleSampleMixin, qinfer.distributions.Distribution

Creates a prior on state space of dimension dim according to the Ginibre ensemble with parameter k. See e.g. [Mis12].

Parameters: dim (int) – Dimension of the state space.
n_rvs

## Combining Distributions¶

QInfer also offers classes for combining distributions together to produce new ones.

class qinfer.ProductDistribution(*factors)[source]

Bases: qinfer.distributions.Distribution

Takes a non-zero number of QInfer distributions $$D_k$$ as input and returns their Cartesian product.

In other words, the returned distribution is $$\Pr(D_1, \dots, D_N) = \prod_k \Pr(D_k)$$.

Parameters: factors (Distribution) – Distribution objects representing $$D_k$$. Alternatively, one iterable argument can be given, in which case the factors are the values drawn from that iterator.
n_rvs
sample(n=1)[source]
class qinfer.PostselectedDistribution(distribution, model, maxiters=100)[source]

Bases: qinfer.distributions.Distribution

Postselects a distribution based on validity within a given model.

n_rvs
sample(n=1)[source]

Returns one or more samples from this probability distribution.

Parameters: Return numpy.ndarray: n (int) – Number of samples to return. An array containing samples from the distribution of shape (n, d), where d is the number of random variables.
grad_log_pdf(x)[source]
class qinfer.MixtureDistribution(weights, dist, dist_args=None, dist_kw_args=None, shuffle=True)[source]

Bases: qinfer.distributions.Distribution

Samples from a weighted list of distributions.

Parameters: weights – Length n_dist list or np.ndarray of probabilites summing to 1. dist – Either a length n_dist list of Distribution instances, or a Distribution class, for example, NormalDistribution. It is assumed that a list of Distributions all have the same n_rvs. dist_args – If dist is a class, an array of shape (n_dist, n_rvs) where dist_args[k,:] defines the arguments of the k’th distribution. Use None if the distribution has no arguments. dist_kw_args – If dist is a class, a dictionary where each key’s value is an array of shape (n_dist, n_rvs) where dist_kw_args[key][k,:] defines the keyword argument corresponding to key of the k’th distribution. Use None if the distribution needs no keyword arguments. shuffle (bool) – Whether or not to shuffle result after sampling. Not shuffling will result in variates being in the same order as the distributions. Default is True.
n_rvs
n_dist

The number of distributions in the mixture distribution.

sample(n=1)[source]
class qinfer.ConstrainedSumDistribution(underlying_distribution, desired_total=1)[source]

Bases: qinfer.distributions.Distribution

Samples from an underlying distribution and then enforces that all samples must sum to some given value by normalizing each sample.

Parameters: underlying_distribution (Distribution) – Underlying probability distribution. desired_total (float) – Desired sum of each sample.
underlying_distribution
n_rvs
sample(n=1)[source]

## Mixins for Distribution Development¶

class qinfer.SingleSampleMixin[source]

Bases: object

Mixin class that extends a class so as to generate multiple samples correctly, given a method _sample that generates one sample at a time.

sample(n=1)[source]