CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
cbl::data::CovarianceMatrix Class Reference

The class CovarianceMatrix. More...

#include <CovarianceMatrix.h>

Inheritance diagram for cbl::data::CovarianceMatrix:

Public Member Functions

Constructors/destructors
 CovarianceMatrix ()
 default constructor
 
 CovarianceMatrix (std::vector< std::vector< double >> covariance_matrix, const double nmeasures=-1, const double prec=1.e-10)
 constructor which sets the covariance matrix More...
 
 CovarianceMatrix (std::vector< double > standard_deviation, const double nmeasures=-1)
 constructor which gets the data from an input vector More...
 
 CovarianceMatrix (const std::string filename, const int cov_col=2, const int skipped_lines=0, const double nmeasures=-1, const double prec=1.e-10)
 constructs with sets the covariance matrix reading from an input file More...
 
virtual ~CovarianceMatrix ()=default
 default destructor
 
Member functions to get the private/protected members
double operator() (const int i, const int j) const
 get the value of the covariance matrix at index i,j More...
 
std::vector< std::vector< double > > operator() () const
 get the covariance matrix More...
 
double correlation (const int i, const int j) const
 get the value of the correlation matrix at index i,j More...
 
std::vector< std::vector< double > > correlation () const
 get the value of the correlation matrix at index i,j More...
 
double precision (const int i, const int j) const
 get the value of the precision matrix at index i,j More...
 
std::vector< std::vector< double > > precision () const
 get the precision matrix More...
 
double determinant () const
 get the determinant More...
 
double precision_hartlap (const int i, const int j) const
 get the value of the precision matrix at index i,j times the Hartlap factor More...
 
std::vector< std::vector< double > > precision_hartlap () const
 get the value of the precision matrix at index i,j times the Hartlap factor More...
 
double standard_deviation (const int i) const
 get value of the standard deviation at index i More...
 
std::vector< double > standard_deviation () const
 get the standard deviation More...
 
double variance (const int i) const
 get value of the variance at index i More...
 
std::vector< double > variance () const
 get the variance More...
 
size_t order () const
 return the covariance matrix order More...
 
Member functions to set the private/protected members
void set_from_matrix (const std::vector< std::vector< double >> covariance, const double nmeasures=-1, const double prec=1.e-10)
 set the covariance matrix by passing a std::vector<std::vector<double>> object; More...
 
void set_from_standard_deviation (const std::vector< double > standard_deviation, const double nmeasures=-1)
 set the covariance by passing the diagonal square root More...
 
void measure (const std::vector< std::shared_ptr< Data >> dataset, const double normalization=1, const double prec=1.e-10)
 measure the covariance from a collection of dataset More...
 
void measure (const std::vector< std::vector< std::shared_ptr< Data >>> dataset, const double normalization=1, const double prec=1.e-10)
 measure the covariance from a collection of dataset More...
 
Member functions used for Input/Output
void read (const std::string filename, const int cov_col=2, const int skipped_lines=0, const double nmeasures=-1, const double prec=1.e-10)
 set the covariance matrix, reading from an input file More...
 
void write (const std::string dir, const std::string file, const int precision=4, const int rank=0) const
 write the covariance matrix More...
 
Member functions for covariance matrix cut
CovarianceMatrix cut (const std::vector< bool > mask) const
 cut the data, for Data1D More...
 
Member functions to add two (or more) covariance matrices
CovarianceMatrix operator+= (const CovarianceMatrix covariance) const
 overloading of the += operator, to sum two catalogues More...
 
CovarianceMatrix operator+= (const std::shared_ptr< CovarianceMatrix > covariance) const
 overloading of the += operator, to sum two covariance matrices More...
 
CovarianceMatrix operator+= (const std::vector< CovarianceMatrix > covariance) const
 overloading of the += operator, to sum several covariance matrices, passed in a vector More...
 
CovarianceMatrix operator+= (const std::vector< std::shared_ptr< CovarianceMatrix >> covariance) const
 overloading of the += operator, to sum several covariance matrices, passed in a vector More...
 

Protected Member Functions

void m_set_default ()
 set internal attributes to default values
 
virtual void m_set (const std::vector< double > matrix, const double nmeasures=-1, const double prec=1.e-10)
 set internal attributes More...
 
double hartlap_factor (const double order, const double nmeasures=-1)
 compute the hartlap factor. This is used to de-bias precision matrix measured from covariance measured from limited number of datasets More...
 

Protected Attributes

size_t m_order
 number of data
 
Eigen::MatrixXd m_matrix
 covariance matrix
 
Eigen::MatrixXd m_precision
 precision matrix
 
Eigen::MatrixXd m_correlation
 correlation matrix
 
Eigen::VectorXd m_variance
 diagonal of the covariance matrix
 
Eigen::VectorXd m_std
 standard deviation
 
double m_determinant
 determinant
 
double m_hartlap_factor
 The hartlap factor, only set when.
 

Detailed Description

The class CovarianceMatrix.

"Headers/CovarianceMatrix.h"

This is the base class used to manage covariance matrices

Definition at line 59 of file CovarianceMatrix.h.

Constructor & Destructor Documentation

◆ CovarianceMatrix() [1/3]

cbl::data::CovarianceMatrix::CovarianceMatrix ( std::vector< std::vector< double >>  covariance_matrix,
const double  nmeasures = -1,
const double  prec = 1.e-10 
)
inline

constructor which sets the covariance matrix

Parameters
covariance_matrixarray containing the covariance matrix
nmeasuresnumber of measures used to compute the covariance
precthe precision required in the inversion of the covariance matrix

Definition at line 151 of file CovarianceMatrix.h.

◆ CovarianceMatrix() [2/3]

cbl::data::CovarianceMatrix::CovarianceMatrix ( std::vector< double >  standard_deviation,
const double  nmeasures = -1 
)
inline

constructor which gets the data from an input vector

Parameters
standard_deviationthe standard deviation
nmeasuresnumber of measures used to compute the covariance

Definition at line 163 of file CovarianceMatrix.h.

◆ CovarianceMatrix() [3/3]

cbl::data::CovarianceMatrix::CovarianceMatrix ( const std::string  filename,
const int  cov_col = 2,
const int  skipped_lines = 0,
const double  nmeasures = -1,
const double  prec = 1.e-10 
)
inline

constructs with sets the covariance matrix reading from an input file

Parameters
filenamefile containing the covariance matrix in the format: column 0 \( \rightarrow \) xi, column 1 \( \rightarrow \) xj, column cov_col → cov(xi,xj)
cov_colcovariance matrix column, starting from 0
skipped_linescomment lines to be skipped
nmeasuresnumber of measures used to compute the covariance
precthe precision required in the inversion of the covariance matrix

Definition at line 186 of file CovarianceMatrix.h.

Member Function Documentation

◆ correlation() [1/2]

vector< vector< double > > cbl::data::CovarianceMatrix::correlation ( ) const

get the value of the correlation matrix at index i,j

Returns
the value of the correlation \( Corr_{i,j} = \frac{Cov_{i,j}}{\sqrt{Cov_{i,i} \cdot Cov{j,j}}} \)

Definition at line 95 of file CovarianceMatrix.cpp.

◆ correlation() [2/2]

double cbl::data::CovarianceMatrix::correlation ( const int  i,
const int  j 
) const
inline

get the value of the correlation matrix at index i,j

Parameters
iindex
jindex
Returns
the value of the correlation \( Corr_{i,j} = \frac{Cov_{i,j}}{\sqrt{Cov_{i,i} \cdot Cov{j,j}}} \)

Definition at line 229 of file CovarianceMatrix.h.

◆ cut()

CovarianceMatrix cbl::data::CovarianceMatrix::cut ( const std::vector< bool >  mask) const

cut the data, for Data1D

Parameters
[in]maskstd::vector containing values to be masked
Returns
pointer to an object of type Data1D

Definition at line 275 of file CovarianceMatrix.cpp.

◆ determinant()

double cbl::data::CovarianceMatrix::determinant ( ) const
inline

get the determinant

Returns
the determinant

Definition at line 262 of file CovarianceMatrix.h.

◆ hartlap_factor()

double cbl::data::CovarianceMatrix::hartlap_factor ( const double  order,
const double  nmeasures = -1 
)
inlineprotected

compute the hartlap factor. This is used to de-bias precision matrix measured from covariance measured from limited number of datasets

\[ \hat{\Psi}=\left(1-\frac{N_{\mathrm{b}}+1}{N_{\mathrm{s}}-1}\right) \hat{\mathrm{c}}^{-1} \]

Parameters
orderthe matrix order (or the number of data points)
nmeasuresnumber of measures used to compute the covariance
Returns
the hartlap factor

Definition at line 122 of file CovarianceMatrix.h.

◆ m_set()

void cbl::data::CovarianceMatrix::m_set ( const std::vector< double >  matrix,
const double  nmeasures = -1,
const double  prec = 1.e-10 
)
protectedvirtual

set internal attributes

Parameters
matrixthe covariance matrix
nmeasuresnumber of measures
precthe precision required in the inversion of the covariance matrix

Reimplemented in cbl::data::TaperedCovarianceMatrix.

Definition at line 64 of file CovarianceMatrix.cpp.

◆ measure() [1/2]

void cbl::data::CovarianceMatrix::measure ( const std::vector< std::shared_ptr< Data >>  dataset,
const double  normalization = 1,
const double  prec = 1.e-10 
)

measure the covariance from a collection of dataset

This use the standard estimator of the covariance

\[ \hat{C}_{i j}=\frac{f}{N_{\mathrm{s}}-1} \sum_{k=1}^{N_{\mathrm{s}}} \left(D_{i}^{k}-\bar{D}_{i}\right)\left(D_{j}^{k}-\bar{D}_{j}\right) \]

where \(D_{i}\) is the i-th measure \( \bar{D}_{i}\) the mean. \(f\) is a normalization factor the user can provide in input.

Parameters
datasetvector of pointers of object of type Data
normalizationthe normalization factor
precthe precision required in the inversion of the covariance matrix

Definition at line 205 of file CovarianceMatrix.cpp.

◆ measure() [2/2]

void cbl::data::CovarianceMatrix::measure ( const std::vector< std::vector< std::shared_ptr< Data >>>  dataset,
const double  normalization = 1,
const double  prec = 1.e-10 
)

measure the covariance from a collection of dataset

This use the standard estimator of the covariance

\[ \hat{C}_{i j}=\frac{f}{N_{\mathrm{s}}-1} \sum_{k=1}^{N_{\mathrm{s}}} \left(D_{i}^{k}-\bar{D}_{i}\right)\left(D_{j}^{k}-\bar{D}_{j}\right) \]

where \(D_{i}\) is the i-th measure \( \bar{D}_{i}\) the mean. \(f\) is a normalization factor the user can provide in input.

This function takes a vector of measurement. Each measurement is a vector of dataset. This should be used when multiple measurements are made

Parameters
datasetvector of pointers of object of type Data
normalizationthe normalization factor
precthe precision required in the inversion of the covariance matrix

Definition at line 233 of file CovarianceMatrix.cpp.

◆ operator()() [1/2]

vector< vector< double > > cbl::data::CovarianceMatrix::operator() ( ) const

get the covariance matrix

Returns
the covariance matrix

Definition at line 86 of file CovarianceMatrix.cpp.

◆ operator()() [2/2]

double cbl::data::CovarianceMatrix::operator() ( const int  i,
const int  j 
) const
inline

get the value of the covariance matrix at index i,j

Parameters
iindex
jindex
Returns
the value of the covariance matrix

Definition at line 211 of file CovarianceMatrix.h.

◆ operator+=() [1/4]

cbl::data::CovarianceMatrix cbl::data::CovarianceMatrix::operator+= ( const CovarianceMatrix  covariance) const

overloading of the += operator, to sum two catalogues

This function add two covariance matrices of order N and M. It creates a block matrix of order N+M

Parameters
covarianceobject of class CovarianceMatrix
Returns
object of class CovarianceMatrix

Definition at line 296 of file CovarianceMatrix.cpp.

◆ operator+=() [2/4]

cbl::data::CovarianceMatrix cbl::data::CovarianceMatrix::operator+= ( const std::shared_ptr< CovarianceMatrix covariance) const

overloading of the += operator, to sum two covariance matrices

This function add two covariance matrices of order N and M. It creates a block matrix of order N+M

Parameters
covarianceobject of class CovarianceMatrix
Returns
object of class CovarianceMatrix

Definition at line 316 of file CovarianceMatrix.cpp.

◆ operator+=() [3/4]

cbl::data::CovarianceMatrix CovarianceMatrix::operator+= ( const std::vector< CovarianceMatrix covariance) const

overloading of the += operator, to sum several covariance matrices, passed in a vector

This function add the current covariance matrix of order N with several covariance matrices of order M_i. It creates a block matrix of order \( N+\sum_i^n M_i\).

Parameters
covarianceobject of class CovarianceMatrix
Returns
object of class CovarianceMatrix

Definition at line 336 of file CovarianceMatrix.cpp.

◆ operator+=() [4/4]

cbl::data::CovarianceMatrix cbl::data::CovarianceMatrix::operator+= ( const std::vector< std::shared_ptr< CovarianceMatrix >>  covariance) const

overloading of the += operator, to sum several covariance matrices, passed in a vector

This function add the current covariance matrix of order N with several covariance matrices of order M_i. It creates a block matrix of order \( N+\sum_i^n M_i\).

Parameters
covarianceobject of class CovarianceMatrix
Returns
object of class CovarianceMatrix

Definition at line 368 of file CovarianceMatrix.cpp.

◆ order()

size_t cbl::data::CovarianceMatrix::order ( ) const
inline

return the covariance matrix order

Returns
the covariance matrix order

Definition at line 341 of file CovarianceMatrix.h.

◆ precision() [1/2]

vector< vector< double > > cbl::data::CovarianceMatrix::precision ( ) const

get the precision matrix

Returns
the std::vector containing the precision matrix

Definition at line 104 of file CovarianceMatrix.cpp.

◆ precision() [2/2]

double cbl::data::CovarianceMatrix::precision ( const int  i,
const int  j 
) const
inline

get the value of the precision matrix at index i,j

Parameters
iindex
jindex
Returns
the value of the precision matrix at position i,j

Definition at line 248 of file CovarianceMatrix.h.

◆ precision_hartlap() [1/2]

vector< vector< double > > cbl::data::CovarianceMatrix::precision_hartlap ( ) const

get the value of the precision matrix at index i,j times the Hartlap factor

This function returns the precision matrix times the Hartlap factor at index i,j:

\[ \hat{\Psi}=\left(1-\frac{N_{\mathrm{b}}+1}{N_{\mathrm{s}}-1}\right) \hat{\mathrm{c}}^{-1} \]

where \(N_b\) are the number of bins (the order of the matrix) and \(N_s\) the number of data used to measure the covariance

Returns
the std::vector containing the precision matrix

Definition at line 113 of file CovarianceMatrix.cpp.

◆ precision_hartlap() [2/2]

double cbl::data::CovarianceMatrix::precision_hartlap ( const int  i,
const int  j 
) const
inline

get the value of the precision matrix at index i,j times the Hartlap factor

This function returns the precision matrix times the Hartlap factor at index i,j:

\[ \hat{\Psi}=\left(1-\frac{N_{\mathrm{b}}+1}{N_{\mathrm{s}}-1}\right) \hat{\mathrm{c}}^{-1} \]

where \(N_b\) are the number of bins (the order of the matrix) and \(N_s\) the number of data used to measure the covariance

Parameters
iindex
jindex
Returns
the value of the precision matrix at position i,j

Definition at line 284 of file CovarianceMatrix.h.

◆ read()

void cbl::data::CovarianceMatrix::read ( const std::string  filename,
const int  cov_col = 2,
const int  skipped_lines = 0,
const double  nmeasures = -1,
const double  prec = 1.e-10 
)

set the covariance matrix, reading from an input file

Parameters
filenamefile containing the covariance matrix in the format: column 0 \( \rightarrow \) xi, column 1 \( \rightarrow \) xj, column cov_col → cov(xi,xj)
cov_colcovariance matrix column, starting from 0
skipped_linescomment lines to be skipped
nmeasuresnumber of measures used to compute the covariance
precthe precision required in the inversion of the covariance matrix

Definition at line 155 of file CovarianceMatrix.cpp.

◆ set_from_matrix()

void cbl::data::CovarianceMatrix::set_from_matrix ( const std::vector< std::vector< double >>  covariance,
const double  nmeasures = -1,
const double  prec = 1.e-10 
)
inline

set the covariance matrix by passing a std::vector<std::vector<double>> object;

Parameters
covariancestd::vector<std::vector<double>> containing the covariance matrix
precthe precision required in the inversion of the covariance matrix
nmeasuresnumber of measures used to compute the covariance

Definition at line 362 of file CovarianceMatrix.h.

◆ set_from_standard_deviation()

void cbl::data::CovarianceMatrix::set_from_standard_deviation ( const std::vector< double >  standard_deviation,
const double  nmeasures = -1 
)

set the covariance by passing the diagonal square root

Parameters
standard_deviationstd::vector containing the standard deviation
nmeasuresnumber of measures used to compute the covariance

Definition at line 140 of file CovarianceMatrix.cpp.

◆ standard_deviation() [1/2]

vector< double > cbl::data::CovarianceMatrix::standard_deviation ( ) const

get the standard deviation

Returns
the standard deviation

Definition at line 122 of file CovarianceMatrix.cpp.

◆ standard_deviation() [2/2]

double cbl::data::CovarianceMatrix::standard_deviation ( const int  i) const
inline

get value of the standard deviation at index i

Parameters
iindex
Returns
the value of the standard deviation vector at position i

Definition at line 311 of file CovarianceMatrix.h.

◆ variance() [1/2]

vector< double > cbl::data::CovarianceMatrix::variance ( ) const

get the variance

Returns
the variance

Definition at line 131 of file CovarianceMatrix.cpp.

◆ variance() [2/2]

double cbl::data::CovarianceMatrix::variance ( const int  i) const
inline

get value of the variance at index i

Parameters
iindex
Returns
the value of the variance vector at position i

Definition at line 327 of file CovarianceMatrix.h.

◆ write()

void cbl::data::CovarianceMatrix::write ( const std::string  dir,
const std::string  file,
const int  precision = 4,
const int  rank = 0 
) const

write the covariance matrix

Parameters
diroutput directory
fileoutput file

the first line of the output file

Parameters
precisionthe float precision
rankcpu index (for MPI usage)

Definition at line 183 of file CovarianceMatrix.cpp.


The documentation for this class was generated from the following files: