13 double randf(
double min,
double max) {
14 return (max - min) * (double) rand() / RAND_MAX + min;
17 double randn(
double mean,
double stdv) {
20 double z1 = (double) rand() / RAND_MAX;
21 double z2 = (double) rand() / RAND_MAX;
23 x = sqrt(-2.0 * log(z1)) * cos(2.0 *
PI * z2);
30 std::vector<double>
randn(std::vector<double> means, std::vector<double> stdv) {
31 std::vector<double> samples;
32 samples = means +
randn(std::move(stdv));
36 std::vector<double>
randn(std::vector<double> stdv) {
38 std::vector<double> samples;
39 samples.reserve(stdv.size());
40 for (
double i : stdv) {
41 samples.push_back(
randn(0, i));
47 randn_Cholesky(std::vector<double> mean, std::vector<std::vector<double>> CholeskyLower_CovarianceMatrix) {
48 return mean +
randn_Cholesky(std::move(CholeskyLower_CovarianceMatrix));
51 std::vector<double>
randn_Cholesky(std::vector<std::vector<double>> CholeskyLower_CovarianceMatrix) {
53 std::vector<double> uncorrelated;
54 uncorrelated.reserve(CholeskyLower_CovarianceMatrix.size());
55 for (
int i = 0; i < CholeskyLower_CovarianceMatrix.size(); ++i) {
56 uncorrelated.push_back(
randn(0, 1));
58 return std::move(CholeskyLower_CovarianceMatrix) * uncorrelated;
62 std::vector<double>
randn(std::vector<std::vector<double>> DiagonalCovarianceMatrix) {
63 std::vector<double> samples;
64 samples.reserve(DiagonalCovarianceMatrix.size());
65 for (
int i = 0; i < DiagonalCovarianceMatrix.size(); i++) {
66 samples.push_back(
randn(0, sqrt(DiagonalCovarianceMatrix[i][i])));
double randf(double min, double max)
Draw uniformly distributed samples between two numbers.
Set of functions to draw from (multivariate, correlated) normal distributions.
std::vector< double > randn_Cholesky(std::vector< double > mean, std::vector< std::vector< double >> CholeskyLower_CovarianceMatrix)
Drawing non-zero mean samples from an dimensional correlated Gaussian. Invokes randn_Cholesky(std::v...
double randn(double mean, double stdv)
Draws from Gaussian (mean, standard deviation) using Box-Müller transform.
Linear algebra functions operating on standard library containers.