-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileNormal.cpp
85 lines (57 loc) · 1.89 KB
/
ProfileNormal.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Profile.h"
#include "ProfileNormal.h"
ProfileNormal::ProfileNormal(boost::math::normal profile) : Profile() {
this->profile = profile;
}
double ProfileNormal::getMean() {
return boost::math::mean(profile);
}
double ProfileNormal::getVariance() {
return boost::math::variance(profile);
}
double ProfileNormal::getPDF(double x) {
return boost::math::pdf(profile, x);
}
double ProfileNormal::getCDF(double x) {
return boost::math::cdf(profile, x);
}
double ProfileNormal::getQuantile(double p) {
return boost::math::quantile(profile, p);
}
double ProfileNormal::getMGF(double t) {
return exp(t * boost::math::mean(profile) + boost::math::variance(profile) * t * t / 2);
}
double ProfileNormal::getSample() {
// Create a Mersenne twister random number generator
// that is seeded once with #seconds since 1970
static boost::mt19937 rng(static_cast<unsigned> (std::time(0)));
// select Normal probability distribution
boost::normal_distribution<double> dist(boost::math::mean(profile), sqrt(boost::math::variance(profile)));
// bind random number generator to distribution, forming a function
boost::variate_generator<boost::mt19937&, boost::normal_distribution<double> > sampler(rng, dist);
// sample from the distribution
return sampler();
}
double ProfileNormal::getProbabilityMassRisk() {
const int n = 10000;
const double x_min = -1000;
const double x_max = 0;
return getIntegralBySimpson(x_min, x_max, n);
}
double ProfileNormal::f(double x) {
return x * boost::math::pdf(profile, x);
}
double ProfileNormal::getIntegralBySimpson(double x_min, double x_max, int n) {
double delta = (x_max - x_min) / n;
double a;
a = f(x_min);
a += f(x_min + delta * n);
int i;
for(i = 1; i < n; i += 2){
a += 4.0 * f(x_min + delta * i);
}
for(i = 2; i < n; i += 2){
a += 2.0 * f(x_min + delta * i);
}
return a * delta / 3.0;
}