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.
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
Pandas DataFrame accessor for covariance calculations
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.
pandas_obj (DataFrame
) – Dataframe representing the covariance matrix
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.
cov_array (ndarray
) – Array of the covariance
param_names (Optional
[Collection
[str
]]) – Collection of strings
normalized (bool
) – Whether to return the normalized covariance matrix
DataFrame
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.
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)
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
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.
param_list (List
[str
]) – List of parameters for which the subCovariance matrix is desired
DataFrame
The covariance of the given parameters