Samplers
Samplers represent probability distributions with known mutual information.
Simple distributions
bmi.samplers.SplitMultinormal
Bases: BaseSampler
Represents two variables with jointly multivariate normal distribution
Covariance matrix should have the block form:
where:
- \(\Sigma_{XX}\) is the covariance matrix of \(X\) variable (shape
(dim_x, dim_x)
), - \(\Sigma_{YY}\) is the covariance of the \(Y\) variable (shape
(dim_y, dim_y)
) - \(\Sigma_{XY}\) and \(\Sigma_{YX}\)
(being transposes of each other, as the matrix is symmetric,
of shapes
(dim_x, dim_y)
or transposed one) describe the covariance between \(X\) and \(Y\).
__init__(*, dim_x, dim_y, covariance, mean=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim_x |
int
|
dimension of the X space |
required |
dim_y |
int
|
dimension of the Y space |
required |
mean |
Optional[ArrayLike]
|
mean vector, shape |
None
|
covariance |
ArrayLike
|
covariance matrix, shape (n, n) |
required |
mutual_information()
Calculates the mutual information I(X; Y) using an exact formula. Returns: mutual information, in nats Mutual information is given by
which follows from the formula \(I(X; Y) = H(X) + H(Y) - H(X, Y)\) and the formula for the differential entropy of the multivariate normal distribution.
bmi.samplers.SplitStudentT
Bases: BaseSampler
Multivariate Student-t distribution.
Sampling is based on Wikipedia
Mutual information is based on:
R.B. Arellano-Valle, J.E. Contreras-Reyes, M.G. Genton, Shannon Entropy and Mutual Information for Multivariate Skew-Elliptical Distributions, Scandinavian Journal of Statistics, vol. 40, pp. 46-47, 2013
Note that the final formula for the mutual information is slightly wrong, but can be calculated using the expressions involving differential entropies above.
df: int
property
Degrees of freedom.
__init__(*, dim_x, dim_y, df, dispersion, mean=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim_x |
int
|
dimension of the X variable |
required |
dim_y |
int
|
dimension of the Y variable |
required |
df |
int
|
degrees of freedom, strictly positive. Use |
required |
dispersion |
ArrayLike
|
dispersion matrix, shape |
required |
mean |
Optional[ArrayLike]
|
mean of the distribution, shape |
None
|
Note
Dispersion is not the covariance matrix.
To calculate the covariance matrix, use the covariance
method.
covariance()
Calculates the covariance matrix.
Returns:
Type | Description |
---|---|
ndarray
|
array, shape |
Raises:
Type | Description |
---|---|
ValueError
|
if covariance is not defined (for |
mi_correction()
Correction term in MI calculation.
This term depends only on the dimensionality of each variable and the degrees of freedom. (It does not depend on the dispersion matrix or mean vector used).
mi_correction_function(df, dim_x, dim_y)
staticmethod
Mutual information between variables with joint Multivariate Student-t decomposes as the sum of mutual information of Gaussian variables and the correction term.
mi_normal()
Part of the mutual information corresponding to a multivariate normal with covariance given by the dispersion matrix.
mutual_information()
Expression for MI taken from Arellano-Valle et al., p. 47.
This expression consists of two terms
mi_normal
, which is the MI of a multivariate normal distribution
with covariance given by the dispersion
mi_correction
, which is a correction term which does not depend
on the means or the dispersion
sample(n_points, rng)
Sampling from multivariate Student distribution.
Note
This function is based on SciPy's sampling.
bmi.samplers.AdditiveUniformSampler
Bases: BaseSampler
__init__(epsilon)
Represents the distribution \(P_{XY}\) under the following model:
The MI in this case is:
and can be derived analytically.
bmi.samplers.BivariateNormalSampler
Bases: BaseSampler
A special case of a general multivariate normal sampler, where both X and Y are one-dimensional Gaussian variables with a given correlation.
Combining and transforming samplers
bmi.samplers.IndependentConcatenationSampler
Bases: BaseSampler
Consider a sequence of samplers \(S_k\), where \(k \in \{1, \dotsc, m \}\) and variables
If the variables are sampled independently, we can concatenate them to \(X = (X_1, \dotsc, X_m)\) and \(Y = (Y_1, \dotsc, Y_m)\)
and have
__init__(samplers)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samplers |
Sequence[ISampler]
|
sequence of samplers to concatenate |
required |
bmi.samplers.TransformedSampler
Bases: BaseSampler
Pushforward of a distribution \(P_{XY}\) via a product mapping \(f \times g\).
In other words, we have mutual information between \(f(X)\) and \(g(Y)\) for some mappings \(f\) and \(g\).
Note
By default we assume that f and g are diffeomorphisms, so that
I(f(X); g(Y)) = I(X; Y).
If you don't use diffeomorphisms (in particular,
non-default add_dim_x
or add_dim_y
), overwrite the
mutual_information()
method
__init__(base_sampler, transform_x=None, transform_y=None, add_dim_x=0, add_dim_y=0, vectorise=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_sampler |
ISampler
|
allows sampling from \(P(X, Y)\) |
required |
transform_x |
Optional[Callable]
|
diffeomorphism \(f\), so that we have variable \(f(X)\). By default the identity mapping. |
None
|
transform_y |
Optional[Callable]
|
diffeomorphism \(g\), so that we have variable \(g(Y)\). By default the identity mapping. |
None
|
add_dim_x |
int
|
the difference in dimensions of the output of \(f\) and its input. Note that for any diffeomorphism it must be zero |
0
|
add_dim_y |
int
|
similarly as |
0
|
vectorise |
bool
|
whether to use |
True
|
Note
If you don't use diffeomorphisms (in particular,
non-default add_dim_x
or add_dim_y
), overwrite the
mutual_information()
method
sample(n_points, rng)
Samples from the distribution \(P(f(X), g(Y))\).
Returns:
Type | Description |
---|---|
tuple[ndarray, ndarray]
|
paired samples
from \(f(X)\), shape |
transform(x, y)
Transforms given samples by f x g
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
SomeArray
|
samples, (n_points, dim(X)) |
required |
y |
SomeArray
|
samples, (n_points, dim(Y)) |
required |
Returns:
Type | Description |
---|---|
ndarray
|
f(x), shape (n_points, dim(X) + add_dim_x) |
ndarray
|
g(y), shape (n_points, dim(Y) + add_dim_y) |
Discrete random variables
bmi.samplers.DiscreteUniformMixtureSampler
Bases: BaseSampler
Sampler from Gao et al. (2017) for the discrete-continuous mixture model:
is sampled from the set \(\{0, ..., m-1\}\).
Then,
It holds that
__init__(*, n_discrete=5, use_discrete_x=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_discrete |
int
|
number of discrete values to sample X from |
5
|
use_discrete_x |
bool
|
if True, X will be an integer. If False, X will be casted to a float. |
True
|
cite()
staticmethod
Returns the BibTeX citation.
bmi.samplers.MultivariateDiscreteUniformMixtureSampler
Bases: IndependentConcatenationSampler
Multivariate alternative for DiscreteUniformMixtureSampler
,
which is a concatenation of several independent samplers.
Namely, for a sequence of integers \((m_k)\), the variables \(X = (X_1, ..., X_k)\) and \(Y = (Y_1, ..., Y_k)\) are sampled coordinate-wise.
Each coordinate
is from the set \(\{0, ..., m_k-1\}\).
Then,
Mutual information can be calculated as
where
cite()
staticmethod
Returns the BibTeX citation.
bmi.samplers.ZeroInflatedPoissonizationSampler
Bases: BaseSampler
Sampler from Gao et al. (2017) modelling zero-inflated Poissonization of the exponential distribution.
Let \(X \sim \mathrm{Exponential}(1)\). Then, \(Y\) is sampled from the mixture distribution
__init__(p=0.15, use_discrete_y=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
float
|
zero-inflation parameter. Must be in [0, 1). |
0.15
|
cite()
staticmethod
Returns the BibTeX citation.
mutual_information(truncation=None)
Ground-truth mutual information is equal to
where
so that the approximation
holds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
truncation |
Optional[int]
|
if set to None, the above approximation will be used. Otherwise, the sum will be truncated at the given value. |
None
|
Bend and Mix Models
See the Bend and Mix Models subpackage API for more information.