size (int, optional) – Defining number of random variates (Default is 1)
random_state (int, numpy.random.Generator, numpy.random.RandomState, optional) – Random state for reproducibility. Can be:
- An integer seed
- A numpy.random.Generator instance
- A numpy.random.RandomState instance
- None (default) for non-deterministic results
Returns:
Random variates
Return type:
ndarray
Notes
If size is not specified, a single (possibly-vector) random variate is
returned. If size is specified, an ndarray of random variates is
returned, even for size=1.
L (int, optional) – number of components to spit Gaussian into. Default is 3
lam (float, optional) – Gauss split optimization regularization parameter. Lower values
result in less component spread and larger component covariances.
This value should be decreased as L increases. Default is 1e-3
recurse_depth (int, optional) – maximum recursion depth. Default is inf
min_weight (float, optional) – smallest component weight that is considered for splitting. Default
is 1e-3
state_idxs (tuple) – optional (2,) indices of state vector corresponding to the FoV space. Default is (0,1)
spectral_direction_only (bool, optional) – if True, only use spectral direction for splitting. Default is False
variance_preserving (bool, optional) – if True, preserve variance in splitting. Default is True
Calculations involving covariance matrices (e.g. data whitening,
multivariate normal function evaluation) are often performed more
efficiently using a decomposition of the covariance matrix instead of the
covariance matrix itself. This class allows the user to construct an
object representing a covariance matrix using any of several
decompositions and perform calculations using a common interface.
Note
The Covariance class cannot be instantiated directly. Instead, use
one of the factory methods (e.g. Covariance.from_diagonal).
Examples
The Covariance class is used by calling one of its
factory methods to create a Covariance object, then pass that
representation of the Covariance matrix as a shape parameter of a
multivariate distribution.
For instance, the multivariate normal distribution can accept an array
representing a covariance matrix:
>>> fromscipyimportstats>>> importnumpyasnp>>> d=[1,2,3]>>> A=np.diag(d)# a diagonal covariance matrix>>> x=[4,-2,5]# a point of interest>>> dist=stats.multivariate_normal(mean=[0,0,0],cov=A)>>> dist.pdf(x)4.9595685102808205e-08
but the calculations are performed in a very generic way that does not
take advantage of any special properties of the covariance matrix. Because
our covariance matrix is diagonal, we can use Covariance.from_diagonal
to create an object representing the covariance matrix, and
multivariate_normal can use this to compute the probability density
function more efficiently.
Return a representation of a covariance matrix from its diagonal.
Parameters:
diagonal (array_like) – The diagonal elements of a diagonal matrix.
Notes
Let the diagonal elements of a diagonal covariance matrix \(D\) be
stored in the vector \(d\).
When all elements of \(d\) are strictly positive, whitening of a
data point \(x\) is performed by computing
\(x \cdot d^{-1/2}\), where the inverse square root can be taken
element-wise.
\(\log\det{D}\) is calculated as \(-2 \sum(\log{d})\),
where the \(\log\) operation is performed element-wise.
This Covariance class supports singular covariance matrices. When
computing _log_pdet, non-positive elements of \(d\) are
ignored. Whitening is not well defined when the point to be whitened
does not lie in the span of the columns of the covariance matrix. The
convention taken here is to treat the inverse square root of
non-positive elements of \(d\) as zeros.
Examples
Prepare a symmetric positive definite covariance matrix A and a
data point x.
Return a representation of a covariance from its precision matrix.
Parameters:
precision (array_like) – The precision matrix; that is, the inverse of a square, symmetric,
positive definite covariance matrix.
covariance (array_like, optional) – The square, symmetric, positive definite covariance matrix. If not
provided, this may need to be calculated (e.g. to evaluate the
cumulative distribution function of
scipy.stats.multivariate_normal) by inverting precision.
Notes
Let the covariance matrix be \(A\), its precision matrix be
\(P = A^{-1}\), and \(L\) be the lower Cholesky factor such
that \(L L^T = P\).
Whitening of a data point \(x\) is performed by computing
\(x^T L\). \(\log\det{A}\) is calculated as
\(-2tr(\log{L})\), where the \(\log\) operation is performed
element-wise.
This Covariance class does not support singular covariance matrices
because the precision matrix does not exist for a singular covariance
matrix.
Examples
Prepare a symmetric positive definite precision matrix P and a
data point x. (If the precision matrix is not already available,
consider the other factory methods of the Covariance class.)
>>> importnumpyasnp>>> fromscipyimportstats>>> rng=np.random.default_rng()>>> n=5>>> P=rng.random(size=(n,n))>>> P=P@P.T# a precision matrix must be positive definite>>> x=rng.random(size=n)
Create the Covariance object.
>>> cov=stats.Covariance.from_precision(P)
Compare the functionality of the Covariance object against
reference implementations.
Representation of a covariance provided via the (lower) Cholesky factor
Parameters:
cholesky (array_like) – The lower triangular Cholesky factor of the covariance matrix.
Notes
Let the covariance matrix be \(A\) and \(L\) be the lower
Cholesky factor such that \(L L^T = A\).
Whitening of a data point \(x\) is performed by computing
\(L^{-1} x\). \(\log\det{A}\) is calculated as
\(2tr(\log{L})\), where the \(\log\) operation is performed
element-wise.
This Covariance class does not support singular covariance matrices
because the Cholesky decomposition does not exist for a singular
covariance matrix.
Examples
Prepare a symmetric positive definite covariance matrix A and a
data point x.
>>> importnumpyasnp>>> fromscipyimportstats>>> rng=np.random.default_rng()>>> n=5>>> A=rng.random(size=(n,n))>>> A=A@A.T# make the covariance symmetric positive definite>>> x=rng.random(size=n)
Perform the Cholesky decomposition of A and create the
Covariance object.
Representation of a covariance provided via eigendecomposition
Parameters:
eigendecomposition (sequence) – A sequence (nominally a tuple) containing the eigenvalue and
eigenvector arrays as computed by scipy.linalg.eigh or
numpy.linalg.eigh.
Notes
Let the covariance matrix be \(A\), let \(V\) be matrix of
eigenvectors, and let \(W\) be the diagonal matrix of eigenvalues
such that V W V^T = A.
When all of the eigenvalues are strictly positive, whitening of a
data point \(x\) is performed by computing
\(x^T (V W^{-1/2})\), where the inverse square root can be taken
element-wise.
\(\log\det{A}\) is calculated as \(tr(\log{W})\),
where the \(\log\) operation is performed element-wise.
This Covariance class supports singular covariance matrices. When
computing _log_pdet, non-positive eigenvalues are ignored.
Whitening is not well defined when the point to be whitened
does not lie in the span of the columns of the covariance matrix. The
convention taken here is to treat the inverse square root of
non-positive eigenvalues as zeros.
Examples
Prepare a symmetric positive definite covariance matrix A and a
data point x.
>>> importnumpyasnp>>> fromscipyimportstats>>> rng=np.random.default_rng()>>> n=5>>> A=rng.random(size=(n,n))>>> A=A@A.T# make the covariance symmetric positive definite>>> x=rng.random(size=n)
Perform the eigendecomposition of A and create the Covariance
object.
“Whitening” (“white” as in “white noise”, in which each frequency has
equal magnitude) transforms a set of random variables into a new set of
random variables with unit-diagonal covariance. When a whitening
transform is applied to a sample of points distributed according to
a multivariate normal distribution with zero mean, the covariance of
the transformed sample is approximately the identity matrix.
Parameters:
x (array_like) – An array of points. The last dimension must correspond with the
dimensionality of the space, i.e., the number of columns in the
covariance matrix.
“Colorizing” (“color” as in “colored noise”, in which different
frequencies may have different magnitudes) transforms a set of
uncorrelated random variables into a new set of random variables with
the desired covariance. When a coloring transform is applied to a
sample of points distributed according to a multivariate normal
distribution with identity covariance and zero mean, the covariance of
the transformed sample is approximately the covariance matrix used
in the coloring transform.
Parameters:
x (array_like) – An array of points. The last dimension must correspond with the
dimensionality of the space, i.e., the number of columns in the
covariance matrix.
sig_mul (float, optional) – sigma multiplier factor. For example, sig_mul=2 will return points
corresponding to the 2-sigma contour. Defaults to sig_mul=1
num_pts (int, optional) – number of points to compute. Defaults to num_pts=100
lam (float) – optimization parameter that specifies the importance of standard
deviation size and overall L2 distance. lam=0 will place zero
importance on standard deviation, making objective equivalent to L2
distance
eps (float) – spacing between means
sig (float) – standard deviation of components
w (ndarray) – (ceil(L/2),) weights of candidate Gaussian mixture. To enforce
symmetry, only the left-half weights are used
optimize L-wise split of standard univariate Gaussian, preserving variance
Parameters:
L (int) – number of components to split into
lam (float) – optimization parameter that specifies the importance of standard
deviation size and overall L2 distance. lam=0 will place zero
importance on standard deviation, making objective equivalent to L2
distance
This optimization process uses the following constraints to restrict the
dimensionality of the search space:
1. Means are evenly spaced
2. The components are homoscedastic
3. The distribution variance is preserved
optimize L-wise split of standard univariate Gaussian
Parameters:
L (int) – number of components to split into
lam (float) – optimization parameter that specifies the importance of standard
deviation size and overall L2 distance. lam=0 will place zero
importance on standard deviation, making objective equivalent to L2
distance
This optimization process uses the following constraints to restrict the
dimensionality of the search space:
1. Means are evenly spaced
2. The components are homoscedastic
split 1D standard univariate Gaussian into L-component GM
Parameters:
L (int) – number of components to split into
lam (float) – optimization parameter that specifies the importance of standard
deviation size and overall L2 distance. lam=0 will place zero
importance on standard deviation, making objective equivalent to L2
distance
variance_preserving (bool) – if True, the L2 distance will be minimized while preserving the
variance of the original Gaussian. If False, the L2 distance will be
minimized without regard to the variance of the original Gaussian
cov_type (str, optional) – type of covariance provided. Default is ‘full’
direction (ndarray, optional) – (nx,) desired direction along which to split. The covariance
eigenvector that closest matches this direction is used. If
direction is None, the direction of largest variance will be used.
pyest.gm.split.split_spectral_direction(w, m, S, V, D, eig_idx, wt, mt, Pt)[source]
split Gaussian along spectral direction
Parameters:
w (float) – component weight
m (ndarray) – (nx,) component mean
S (ndarray) – (nx, nx) component covariance lower Cholesky factor
V (ndarray) – (nx, nx) spectral decomposition eigenvectors
D (ndarray) – (nx,,nx) spectral decomposition eigenvalues diagonal matrix
eig_idx (int) – index of eigenvector to split along
wt (ndarray) – (L,) weights of the standard split library
mt (ndarray) – (L, nx) means of the standard split library
Pt (ndarray)
Returns:
L-component GM approximation of the input weighted Gaussian
choose direction for FoV splitting to minimize number of downstream
components
Parameters:
intact_cols (ndarray) – (L,) logical, where intact_cols[i]=True indicates that all grid points
in the ith column are either entirely included in the FoV or entirely
excluded
intact_rows (ndarray) – (L,) logical, where intact_rows[i]=True indicates that all grid points
in the ith row are either entirely included in the FoV or entirely
excluded
eigvals (ndarray) – (2,) positional covariance eigenvalues. In the case that the
direction cannot be determined by point inclusion, the direction will
be chosen according to the largest positional covariance eigenvalue.
Returns:
index of direction to split along. 0 corresponds to a split along the
horizontal axis, and 1 corresponds to a split along the vertical axis
in_mask_tensor (ndarray) – (L,L,L) boolean mask indicating point-wise inclusion by the FoV, where
L is the number of equally spaced points per dimension
num_pts_in_slice (int) – (L,) number of valid collocation points in each slice
Returns:
intact_xy_plane (ndarray) – (L,) logical, where intact_xy[i]=True indicates that all grid points
in the ith horizontal slice [i,:,:] are either entirely included in the FoV or entirely
excluded
intact_xz_plane (ndarray) – (L,) logical, where intact_xz_plane[j]=True indicates that all grid points
in the jth lateral slice [:,j,:] are either entirely included in the FoV or entirely
excluded
intact_yz_plane (ndarray) – (L,) logical, where intact_xz_plane[k]=True indicates that all grid points
in the kth frontal slice [:,:,k] are either entirely included in the FoV or entirely
excluded
find rows and columns that are totally included/excluded
Parameters:
in_mask_mat (ndarray) – (L,L) boolean mask indicating point-wise inclusion by the FoV, where
L is the number of equally spaced points per dimension
num_pts_in_slice (int) – (L,1) number of collocation points in each slice
Returns:
intact_cols (ndarray) – (L,) logical, where intact_cols[i]=True indicates that all grid points
in the ith column are either entirely included in the FoV or entirely
excluded
intact_rows (ndarray) – (L,) logical, where intact_rows[i]=True indicates that all grid points
in the ith row are either entirely included in the FoV or entirely
excluded
identify components and split directions based on variance
Parameters:
p (GaussianMixture) – input Gaussian mixture to be considered for splitting
tol (float) – tolerance for splitting, given as a standard deviation.
If the weighted standard deviation of the considered component is greater
than tol in any spectral direction, the component is marked for splitting
Returns:
split_mask (np.ndarray) – (nC,) boolean array indicating which components are marked for splitting
split_dir (np.ndarray) – (nC, nX) array of split directions for each component
Notes
Let
\(\mathbf{P_x}\) be the initial covariance matrix.
identify components and split directions based on nonlinear stretching
Parameters:
p (GaussianMixture) – input Gaussian mixture to be considered for splitting
pdt_func (callable) – nonlinear function partial derivative tensor of the form
pdt_func(x1, x2, …, xn)
jacobian_func (callable) – nonlinear function Jacobian of the form jacobian_func(x1, x2, …, xn)
tol (float) – tolerance for splitting, given as a Mahalanobis distance. If
e^T Pf^-1 e > tol, the component is marked for splitting, where
Pf is the linearly-mapped covariance of the considered component
single_fn (bool) – interpret pdt_func argument as returning the tuple (x_f, jac, hess)
and disregard jacobian function completely
Returns:
split_mask (np.ndarray) – (nC,) boolean array indicating which components are marked for splitting
split_dir (np.ndarray) – (nC, nX) array of split directions for each component
Notes
Let
\(\mathbf{G}\) be the Jacobian of the nonlinear function.
The second order stretching (SOS) measure is given by
identify components and split directions based on output-whitened
uncertainty scaled second-order linearization change
Parameters:
p (GaussianMixture) – input Gaussian mixture to be considered for splitting
pdt_func (callable) – function that returns the nonlinear function’s second-order partial
derivative tensor of the nonlinear function of the form
pdt_func(x1, x2, …, xn)
jacobian_func (callable) – function that returns the Jacobian of the nonlinear function
tol (float) – tolerance for splitting. If the weighted norm exceeds tol, the component
is marked for splitting
single_fn (bool) – interpret pdt_func argument as returning the tuple (x_f, jac, pdt)
and disregard jacobian function completely
Returns:
split_mask (np.ndarray) – (nC,) boolean array indicating which components are marked for splitting
split_dir (np.ndarray) – (nC, nX) array of split directions for each component
Notes
Let
\(\mathbf{\|x\|_F}\) be the Frobenius norm of x.
\(\mathbf{\|x\|_{P_x^{-1}}}\) be the norm of x induced by the initial precision matrix.
\(\mathbf{G}\) be the Jacobian of the nonlinear function.
\(\mathbf{P_x}\) be the initial covariance matrix.
\(\mathbf{P_z}\) be the linear prediction of the covariance matrix.
The whitened uncertainty-scaled second-order linearization change (WUSSOLC) measure is given by
compute L2 distance (ISE) between two Gaussian mixtures
Notes
This function is intended for use with generic callable densities and
makes no assumptions about the form of the densities. If both p1 and p2
are Gaussian mixtures, use l2_dist instead, which is exact and more
efficient.
pyest.metrics.normalized_integral_squared_error_2d(p1, p2, a, b, c, d, epsabs=0.0149, epsrel=0.0149)[source]
compute normalized integral squared error between two 2D, densities
Parameters:
p1 (callable) – first density
p2 (callable) – second density
a (float) – The limits of integration in x: a<b
b (float) – The limits of integration in x: a<b
c (float) – The limits of integration in y: c<d
d (float) – The limits of integration in y: c<d
epsabs (float, optional) – absolute error tolerance for numerical integration
epsrel (float, optional) – relative error tolerance for numerical integration
Returns:
nise (float) – normalized integral squared error
ise – integral squared error
err – numerical integration estimated error in ISE computation
compute L2 distance (ISE) between two Gaussian mixtures
Notes
This function is intended for use with generic callable densities and
makes no assumptions about the form of the densities. If both p1 and p2
are Gaussian mixtures, use metrics.l2_dist and gm.integral_squared_gm
instead for the numerator and denominator terms separately, which is
exact and more efficient.