uncertainty.covariance¶
Covariances of uncertain values: the dependence between them.
A Covariance is the dependence of a set of uncertain values (the inputs of a
simulation or the parameters of a model), given as a covariance matrix or as a
correlation: a full matrix or the coefficients of pairs. The same object is
used by every step of an uncertainty analysis, see sbmlsim.uncertainty.
A covariance is in the units of its targets (units, the entry ij in the
unit of i times the unit of j), on the linear scale or on the log scale
(scale="log", e.g. the omega matrix of a population analysis of log-normal
parameters). A correlation is dimensionless; the standard deviations come from
the marginals of its targets.
Every matrix is validated: square, symmetric, finite, a positive diagonal
(unit for a correlation, |rho| <= 1) and positive semi-definite. A
correlation which is not positive semi-definite, e.g. assembled from the
coefficients of several sources, can be replaced by the nearest correlation
matrix (nearest=True, Higham 2002).
Higham NJ (2002). Computing the nearest correlation matrix: a problem from finance. IMA J Numer Anal 22:329-343.
Covariance
dataclass
¶
The dependence of uncertain values, see the module.
Attributes:
| Name | Type | Description |
|---|---|---|
targets |
tuple[str, ...]
|
the targets, the names of the uncertain values. |
matrix |
ndarray
|
the read-only matrix, a covariance or a correlation. |
kind |
Literal['covariance', 'correlation']
|
|
scale |
Literal['linear', 'log']
|
the scale of a covariance, |
units |
tuple[str | None, ...] | None
|
the unit of every target of a covariance, |
Create a covariance (or a correlation, see correlation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Sequence[str]
|
the targets in the order of the rows. |
required |
matrix
|
ArrayLike
|
the matrix. |
required |
units
|
Sequence[str | None] | None
|
the unit of every target, for a covariance. |
None
|
scale
|
Literal['linear', 'log']
|
|
'linear'
|
kind
|
Literal['covariance', 'correlation']
|
|
'covariance'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if the matrix is not valid, see the module. |
correlation
classmethod
¶
Create a correlation, from the coefficients of pairs or a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pairs
|
Mapping[tuple[str, str], float] | None
|
|
None
|
matrix
|
ArrayLike | None
|
a full correlation matrix, with |
None
|
targets
|
Sequence[str] | None
|
the targets of the matrix. |
None
|
nearest
|
bool
|
replace a matrix which is not positive semi-definite by the nearest correlation matrix, with a warning. |
False
|
Returns:
| Type | Description |
|---|---|
Covariance
|
The correlation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
for pairs and a matrix together or neither, a matrix without targets, or a matrix which is not valid. |
nearest_correlation
¶
Get the nearest correlation matrix in the Frobenius norm.
The alternating projections method with the correction of Dykstra of
Higham (2002): the projection onto the positive semi-definite matrices
and onto the matrices with a unit diagonal, alternately, until the
iterates change less than tol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ArrayLike
|
a symmetric matrix with a unit diagonal. |
required |
max_iterations
|
int
|
the most iterations. |
100
|
tol
|
float
|
the tolerance of the change of an iteration in the Frobenius norm. |
1e-10
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The nearest correlation matrix. |