-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paths_cpp_test.cpp
67 lines (58 loc) · 2.12 KB
/
s_cpp_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// normal_distribution
#include <iostream>
#include <string>
#include <random>
#include <chrono>
#include <math.h>
//****** PDF ******//
double normal_pdf(double x, double mu, double sigma){
return 1.0 / (std::sqrt(2.0 * M_PI) * sigma) * exp(-pow(x - mu, 2.0) / (2 * pow(sigma, 2.0)));
}
//****** Log PDF ******//
double normal_lpdf(double x, double mu, double sigma){
return -0.5 * log(2 * M_PI) - log(sigma) - pow(x - mu, 2.0) / (2 * pow(sigma, 2.0));
}
//****** GENERATE RANDOM SAMPLES ******//
// unseeded
std::vector<double> normal_rng(int n, double mu, double sigma){
// set seed according to clock, alternatively set number
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::normal_distribution<double> distribution(mu, sigma);
std::vector<double> samples(n);
for(int i = 0; i < n; i++)
samples[i] = distribution(generator);
return samples;
}
// seeded
std::vector<double> normal_rng(int n, double mu, double sigma, unsigned seed){
std::default_random_engine generator(seed);
std::normal_distribution<double> distribution(mu, sigma);
std::vector<double> samples(n);
for(int i = 0; i < n; i++)
samples[i] = distribution(generator);
return samples;
}
int main()
{
// parameter values
double mu = 0.0;
double sigma = 1.0;
// Example x value
double x = 0.0;
// Compute normal pdf at x = 0
double a_pdf = normal_pdf(x, mu, sigma);
std::cout << "normal_pdf(x| mu, sigma) = " << a_pdf << std::endl;
// Compute log normal pdf at x = 0
double a_lpdf = normal_lpdf(x, mu, sigma);
std::cout << "normal_lpdf(x| mu, sigma) = " << a_lpdf << std::endl;
// Generate n random samples
int n = 1000;
std::vector<double> samples = normal_rng(n, mu, sigma);
// properties of sample
float mean = accumulate(samples.begin(), samples.end(), 0.0) / samples.size();
double sq_sum = std::inner_product(samples.begin(), samples.end(), samples.begin(), 0.0);
std::cout << "The sample mean is " << mean << std::endl;
std::cout << "The standard deviation is " << std::sqrt(sq_sum / samples.size() - pow(mean, 2)) << std::endl;
return 0;
}