CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
correlated_samples.cpp

This example shows how to generate correlated random samples using the Cholesky decomposition

// ========================================================================================
// Example code: how to generate correlated random samples using the Cholesky decomposition
// ========================================================================================
#include "Func.h"
int main () {
try {
// the mean values
const std::vector<double> mean = {1., 2.};
// the standard deviation values
const std::vector<double> std = {0.1, 0.3};
// the sample correlation
const double correlation_factor = 0.5;
// the covariance matrix
std::vector<std::vector<double>> covariance = {{std[0]*std[0], std[0]*std[1]*correlation_factor},
{std[0]*std[1]*correlation_factor, std[1]*std[1]}};
// the seed
int seed = 666;
// the number of samples to be extracted
int nExtractions = 100000;
// construct the object to generate correlated samples; dataset has size nExtractions x nData
std::vector<std::vector<double>> dataset = cbl::generate_correlated_data(nExtractions, mean, covariance, seed);
// traspose it, to simplify the output verification
std::vector<std::vector<double>> datasetT = cbl::transpose(dataset);
// write data statistics
for (size_t i=0; i<datasetT.size(); i++) {
std::cout << "Data" << i+1 << " mean = " << mean[i] << " " << cbl::Average(datasetT[i]) << std::endl;
std::cout << " " << " std = " << std[i] << " " << cbl::Sigma(datasetT[i]) << std::endl;
}
// write the input covariance
std::cout << std::endl << "Input covariance : " << std::endl;
cbl::Print(covariance);
// write the output
std::cout << std::endl << "Measured covariance : " << std::endl;
std::vector<std::vector<double>> measured_covariance;
cbl::covariance_matrix(dataset, measured_covariance);
cbl::Print(measured_covariance);
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
Useful generic functions.
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
void Print(const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
function to print values with a proper homegenised format
Definition: Kernel.h:1142
double Average(const std::vector< double > vect)
the average of a std::vector
Definition: Func.cpp:870
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix
Definition: Kernel.h:1729
double Sigma(const std::vector< double > vect)
the standard deviation of a std::vector
Definition: Func.cpp:925
std::vector< double > generate_correlated_data(const std::vector< double > mean, const std::vector< std::vector< double >> covariance, const int idum=213123)
generate a covariant sample of n points using a covariance matrix
Definition: Func.cpp:2142
void covariance_matrix(const std::vector< std::vector< double >> mat, std::vector< std::vector< double >> &cov, const bool JK=false)
compute the covariance matrix from an input dataset
Definition: Func.cpp:761