fips.Covariance.diagonal#

classmethod Covariance.diagonal(values, space=None, name=None)[source]#

Build a diagonal covariance from its variances.

The covariance is kept as its diagonal, so it costs one number per label whatever the size.

Parameters:
  • values (float | ArrayLike | Vector) – The variances (an array is copied). A scalar is the same for every label; a Vector brings its own space.

  • space (Space | None) – The labels. Required for a scalar or an array; for a Vector it must be left out, or have exactly the vector’s labels (the variances are put in its order).

  • name (str | None) – The covariance’s name.

Returns:

The diagonal covariance.

Return type:

Self

Raises:

ValueError – If a scalar or an array comes without a space, or values is not a scalar or 1D, or has a length other than the space’s, or a variance is below zero by more than roundoff.

Examples

>>> import pandas as pd
>>> from fips import Covariance, Space
>>> space = Space({"co2": pd.Index(["a", "b", "c"], name="site")})
>>> S = Covariance.diagonal(0.25, space)
>>> S.dense()
array([[0.25, 0.  , 0.  ],
       [0.  , 0.25, 0.  ],
       [0.  , 0.  , 0.25]])
>>> Covariance.diagonal(S.variances).variances.values
array([0.25, 0.25, 0.25])