snat_sim.utils.cov_utils

The cov_utils module extends pandas.DataFrame objects to include covariance calculations relevant to processing supernova fit results.

Note

This module is heavily based on code ported from the AnalyzeSN project.

Usage Example

The cov_utils accessor provides a constructor method for converting covariance data from numpy type objects into a pandas.DataFrame instance:

>>> import numpy as np
>>> import pandas as pd
>>> import snat_sim

>>> parameter_names = ['z', 't0', 'x0', 'x1', 'c']
>>> example_covariance = np.array([
...     [ 2.19e-04,  9.70e-04,  1.46e-09, -9.15e-04, -3.38e-04],
...     [ 9.70e-04,  1.75e-01, -2.75e-08,  4.74e-02, -6.88e-04],
...     [ 1.46e-09, -2.75e-08,  1.53e-13, -7.58e-08, -9.63e-09],
...     [-9.15e-04,  4.74e-02, -7.58e-08,  1.04e-01,  2.85e-03],
...     [-3.38e-04, -6.88e-04, -9.63e-09,  2.85e-03,  1.33e-03]
... ])
>>> covariance_df = pd.DataFrame.cov_utils.from_array(example_covariance, parameter_names)
>>> covariance_df
               z            t0            x0            x1             c
z   2.190000e-04  9.700000e-04  1.460000e-09 -9.150000e-04 -3.380000e-04
t0  9.700000e-04  1.750000e-01 -2.750000e-08  4.740000e-02 -6.880000e-04
x0  1.460000e-09 -2.750000e-08  1.530000e-13 -7.580000e-08 -9.630000e-09
x1 -9.150000e-04  4.740000e-02 -7.580000e-08  1.040000e-01  2.850000e-03
c  -3.380000e-04 -6.880000e-04 -9.630000e-09  2.850000e-03  1.330000e-03

From a given dataframe, you can easily extract a subset of the covariance data using the subcovariance method:

>>> covariance_df.cov_utils.subcovariance(['x1', 'c'])
         x1        c
x1  0.10400  0.00285
c   0.00285  0.00133

Module Docs

class snat_sim.utils.cov_utils.CovarianceAccessor(pandas_obj)[source]

Pandas DataFrame accessor for covariance calculations

__init__(pandas_obj)[source]

Extends pandas support for time series data

DO NOT USE THIS CLASS DIRECTLY! This class is registered as a pandas accessor. See the module level usage example for more information.

Parameters:

pandas_obj (DataFrame) – Dataframe representing the covariance matrix

classmethod from_array(cov_array, param_names=None, normalized=False)[source]

Instantiates a Dataframe covariance matrix using data from a numpy.ndarray object.

If paramNames is not None, then the dataframe is indexed by the parameter names, and has columns corresponding to the parameter names enabling easy access by index or names.

Parameters:
  • cov_array (ndarray) – Array of the covariance

  • param_names (Optional[Collection[str]]) – Collection of strings

  • normalized (bool) – Whether to return the normalized covariance matrix

Return type:

DataFrame

Returns:

A pandas.DataFrame with column names and indexes given by the parameter names. If paramNames is None, the return is a DataFrame with indexes and column names chosen by pandas.

log_covariance(param_name: int, param_value: float, factor: float = 1.0) ndarray[source]
log_covariance(param_name: str, param_value: float, factor: float = 1.0) DataFrame

Covariance of the parameters with parameter paramName replaced by factor * np.log(param) everywhere, and its true value is paramValue, assuming linear propagation

The factor parameter is used to scale the logarithm. For example, if the relevant transformation is going from f to -2.5 * log10(f), the factor should be -2.5 / np.log(10)

Parameters:
  • param_name – Parameter name or integer index specifying the position of the desired variable

  • param_value – True or estimated value of the variable itself

  • factor – Factor multiplying the natural logarithm

subcovariance(param_list)[source]

Returns the covariance of a subset of parameters in a covariance dataFrame.

The set of parameters in paramList must be a subset of the columns and indices of covariance.

Parameters:

param_list (List[str]) – List of parameters for which the subCovariance matrix is desired

Return type:

DataFrame

Returns:

The covariance of the given parameters

expAVsquare(A)[source]

The expectation of (A^T * V) ** 2 where A is a constant vector and V is a random vector V ~ N(0., covV) by computing A^T * covV * A

Parameters:

A (array) – Vector of constants.

Return type:

float

Returns:

The variance as a scalar value