This example shows how to generate correlated random samples using the Cholesky decomposition
try {
const std::vector<double> mean = {1., 2.};
const std::vector<double> std = {0.1, 0.3};
const double correlation_factor = 0.5;
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]}};
int seed = 666;
int nExtractions = 100000;
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;
}
std::cout << std::endl << "Input covariance : " << std::endl;
std::cout << std::endl << "Measured covariance : " << std::endl;
std::vector<std::vector<double>> measured_covariance;
}
return 0;
}
Useful generic functions.
int main()
main function to create the logo of the CosmoBolognaLib
const char * what() const noexcept override
the error description
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
double Average(const std::vector< double > vect)
the average of a std::vector
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix
double Sigma(const std::vector< double > vect)
the standard deviation of a std::vector
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
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