ehs-risk-sem
Back to ehs-risk-sem

linalg

condition_number

condition_number(a: 'np.ndarray') -> 'float'

Ratio of largest to smallest eigenvalue of a symmetric matrix.

Returns ``inf`` for a singular matrix. Used as an empirical-identification diagnostic: a very large condition number on the observed covariance matrix means the data carry little independent information about some directions in parameter space, whatever the algebraic identification rules say.

corr_matrix

corr_matrix(x: 'np.ndarray') -> 'np.ndarray'

Sample correlation matrix of the columns of ``x``.

cov_matrix

cov_matrix(x: 'np.ndarray', ddof: 'int' = 1) -> 'np.ndarray'

Sample covariance matrix of the columns of ``x``.

cov_to_corr

cov_to_corr(sigma: 'np.ndarray') -> 'np.ndarray'

Convert a covariance matrix to the corresponding correlation matrix.

is_positive_definite

is_positive_definite(a: 'np.ndarray', tol: 'float' = 1e-10) -> 'bool'

True if all eigenvalues of the symmetric matrix ``a`` exceed ``tol``.

is_symmetric

is_symmetric(a: 'np.ndarray', tol: 'float' = 1e-08) -> 'bool'

True if ``a`` is square and symmetric to within ``tol``.

logdet

logdet(a: 'np.ndarray') -> 'float'

Log determinant of a symmetric positive-definite matrix.

Raises ``ValueError`` when the matrix is not positive definite, because the maximum-likelihood discrepancy function is undefined in that case and silently returning ``-inf`` would hide a model that has failed.

nearest_positive_definite

nearest_positive_definite(a: 'np.ndarray', floor: 'float' = 1e-08) -> 'np.ndarray'

Project a symmetric matrix onto the positive-definite cone.

Eigenvalues below ``floor`` are raised to ``floor``. This is a repair, not an estimate; anything using it should say so. In this package it is used only when a disattenuated latent correlation matrix comes back indefinite, which is itself a finding worth reporting (see :mod:`ehs_risk_sem.diagnostics`).

safe_inverse

safe_inverse(a: 'np.ndarray', ridge: 'float' = 0.0, warn_condition: 'float' = 10000000000.0) -> 'Tuple[np.ndarray, Optional[str]]'

Invert a symmetric matrix, optionally with a ridge, reporting trouble.

Returns the inverse and a warning string (or ``None``). The warning is returned rather than raised so that a caller running thousands of simulation replications can count problem cases instead of aborting.

smallest_eigenvalue

smallest_eigenvalue(a: 'np.ndarray') -> 'float'

Smallest eigenvalue of a symmetric matrix.

standardize

standardize(x: 'np.ndarray', ddof: 'int' = 1) -> 'np.ndarray'

Return ``x`` with each column centred and scaled to unit variance.

Columns with zero variance are returned centred but unscaled, and a ``ValueError`` is raised only if every column is constant, because a constant indicator is a data problem the caller should see rather than a numerical edge case to be smoothed over.

vech

vech(a: 'np.ndarray') -> 'np.ndarray'

Half-vectorization: the lower triangle of a symmetric matrix, including the diagonal, as a 1-d array. Its length ``p(p+1)/2`` is the number of non-redundant observed moments available to identify a model.