![]() |
CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
|
The class CovarianceMatrix. More...
#include <CovarianceMatrix.h>
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. | |
The class CovarianceMatrix.
"Headers/CovarianceMatrix.h"
This is the base class used to manage covariance matrices
Definition at line 59 of file CovarianceMatrix.h.
|
inline |
constructor which sets the covariance matrix
covariance_matrix | array containing the covariance matrix |
nmeasures | number of measures used to compute the covariance |
prec | the precision required in the inversion of the covariance matrix |
Definition at line 151 of file CovarianceMatrix.h.
|
inline |
constructor which gets the data from an input vector
standard_deviation | the standard deviation |
nmeasures | number of measures used to compute the covariance |
Definition at line 163 of file CovarianceMatrix.h.
|
inline |
constructs with sets the covariance matrix reading from an input file
filename | file containing the covariance matrix in the format: column 0 \( \rightarrow \) xi, column 1 \( \rightarrow \) xj, column cov_col → cov(xi,xj) |
cov_col | covariance matrix column, starting from 0 |
skipped_lines | comment lines to be skipped |
nmeasures | number of measures used to compute the covariance |
prec | the precision required in the inversion of the covariance matrix |
Definition at line 186 of file CovarianceMatrix.h.
vector< vector< double > > cbl::data::CovarianceMatrix::correlation | ( | ) | const |
get the value of the correlation matrix at index i,j
Definition at line 95 of file CovarianceMatrix.cpp.
|
inline |
get the value of the correlation matrix at index i,j
i | index |
j | index |
Definition at line 229 of file CovarianceMatrix.h.
CovarianceMatrix cbl::data::CovarianceMatrix::cut | ( | const std::vector< bool > | mask | ) | const |
cut the data, for Data1D
[in] | mask | std::vector containing values to be masked |
Definition at line 275 of file CovarianceMatrix.cpp.
|
inline |
|
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} \]
order | the matrix order (or the number of data points) |
nmeasures | number of measures used to compute the covariance |
Definition at line 122 of file CovarianceMatrix.h.
|
protectedvirtual |
set internal attributes
matrix | the covariance matrix |
nmeasures | number of measures |
prec | the precision required in the inversion of the covariance matrix |
Reimplemented in cbl::data::TaperedCovarianceMatrix.
Definition at line 64 of file CovarianceMatrix.cpp.
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.
dataset | vector of pointers of object of type Data |
normalization | the normalization factor |
prec | the precision required in the inversion of the covariance matrix |
Definition at line 205 of file CovarianceMatrix.cpp.
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
dataset | vector of pointers of object of type Data |
normalization | the normalization factor |
prec | the precision required in the inversion of the covariance matrix |
Definition at line 233 of file CovarianceMatrix.cpp.
vector< vector< double > > cbl::data::CovarianceMatrix::operator() | ( | ) | const |
get the covariance matrix
Definition at line 86 of file CovarianceMatrix.cpp.
|
inline |
get the value of the covariance matrix at index i,j
i | index |
j | index |
Definition at line 211 of file CovarianceMatrix.h.
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
covariance | object of class CovarianceMatrix |
Definition at line 296 of file CovarianceMatrix.cpp.
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
covariance | object of class CovarianceMatrix |
Definition at line 316 of file CovarianceMatrix.cpp.
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\).
covariance | object of class CovarianceMatrix |
Definition at line 336 of file CovarianceMatrix.cpp.
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\).
covariance | object of class CovarianceMatrix |
Definition at line 368 of file CovarianceMatrix.cpp.
|
inline |
return the covariance matrix order
Definition at line 341 of file CovarianceMatrix.h.
vector< vector< double > > cbl::data::CovarianceMatrix::precision | ( | ) | const |
get the precision matrix
Definition at line 104 of file CovarianceMatrix.cpp.
|
inline |
get the value of the precision matrix at index i,j
i | index |
j | index |
Definition at line 248 of file CovarianceMatrix.h.
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
Definition at line 113 of file CovarianceMatrix.cpp.
|
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
i | index |
j | index |
Definition at line 284 of file CovarianceMatrix.h.
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
filename | file containing the covariance matrix in the format: column 0 \( \rightarrow \) xi, column 1 \( \rightarrow \) xj, column cov_col → cov(xi,xj) |
cov_col | covariance matrix column, starting from 0 |
skipped_lines | comment lines to be skipped |
nmeasures | number of measures used to compute the covariance |
prec | the precision required in the inversion of the covariance matrix |
Definition at line 155 of file CovarianceMatrix.cpp.
|
inline |
set the covariance matrix by passing a std::vector<std::vector<double>> object;
covariance | std::vector<std::vector<double>> containing the covariance matrix |
prec | the precision required in the inversion of the covariance matrix |
nmeasures | number of measures used to compute the covariance |
Definition at line 362 of file CovarianceMatrix.h.
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
standard_deviation | std::vector containing the standard deviation |
nmeasures | number of measures used to compute the covariance |
Definition at line 140 of file CovarianceMatrix.cpp.
vector< double > cbl::data::CovarianceMatrix::standard_deviation | ( | ) | const |
get the standard deviation
Definition at line 122 of file CovarianceMatrix.cpp.
|
inline |
get value of the standard deviation at index i
i | index |
Definition at line 311 of file CovarianceMatrix.h.
vector< double > cbl::data::CovarianceMatrix::variance | ( | ) | const |
|
inline |
get value of the variance at index i
i | index |
Definition at line 327 of file CovarianceMatrix.h.
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
dir | output directory |
file | output file |
the first line of the output file
precision | the float precision |
rank | cpu index (for MPI usage) |
Definition at line 183 of file CovarianceMatrix.cpp.