-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNormal.cpp
executable file
·257 lines (214 loc) · 5.6 KB
/
Normal.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "Normal.h"
/*float exponential ( float x ) {
return (float)(exp ( (double)x ));
}*/
float cdf1(float x)
{
float a1 = 0.254829592;
float a2 = -0.284496736;
float a3 = 1.421413741;
float a4 = -1.453152027;
float a5 = 1.061405429;
float p = 0.3275911;
// Save the sign of x
int sign = 1;
if (x < 0)
sign = -1;
x = fabs(x)/sqrt(2.0);
// A&S formula 7.1.26
float t = 1.0/(1.0 + p*x);
double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);
return 0.5*(1.0 + sign*y);
}
float cdf2(float x)
{
if(x<-10.)return 0.;
if(x>10.)return 1.;
// number of steps
int N=1000;
// range of integration
float a=0,b=x;
// local variables
float s,h,sum=0.;
// inialise the variables
h=(b-a)/N;
// add in the first few terms
sum = sum + exp(-a*a/2.) + 4.*exp(-(a+h)*(a+h)/2.);
// and the last one
sum = sum + exp(-b*b/2.);
// loop over terms 2 up to N-1
for(int i=1;i<N/2;i++)
{
s = a + 2*i*h;
sum = sum + 2.*exp(-s*s/2.);
s = s + h;
sum = sum + 4.*exp(-s*s/2.);
}
// complete the integral
sum = 0.5 + h*sum/3./sqrt(8.*atan(1.));
// return result
return sum;
}
float cdf3(float x, float mu, float sigma)
{
//After 3 sigmas, off the charts
if(x < (mu - 3*sigma))
{
return 0.;
}
if(x > (mu + 3*sigma))
{
return 1.;
}
//Number of steps
int N = 1000;
//Integration range
float a=mu, b=x;
//Local variables: s -> each of the terms from 2 to N-1, h -> integration range deltas, sum -> final value of the integral
float s,h,sum=0.;
//Initialization of h
h=(b-a)/N;
//Start by adding the simplest terms (0 and 1 (Remember, this is the 1st instance of the 4 term))
sum = sum + exp(-(pow((a-mu),2.)/((pow(sigma,2.))*2.))) + 4.*exp(-(pow((a+h-mu),2.)/((pow(sigma,2.))*2.)));
//Also add the last term
sum = sum + exp(-(pow((b-mu),2.)/((pow(sigma,2.))*2.)));
//Now loop over all intermediate terms
for(int i=1;i<N/2;i++)
{
s = a + 2*i*h - mu;
sum = sum + 2.*exp(-(pow(s,2.)/((pow(sigma,2.))*2.)));
s = s + h;
sum = sum + 4.*exp(-(pow(s,2.)/((pow(sigma,2.))*2.)));
}
//Complete with the missing pieces: 0.5 is from -infinity to mu (a) and we need to multiply by the distribution factor and by Simpson's factor (h/3)
//sum = 0.5 + (h*sum/3.)*(1/(sigma*sqrt(2*PI)));
sum = 0.5 + (h*sum/3.)*(1/(sigma*sqrt(8.*atan(1.))));
return sum;
}
float pdf(float x, float mu, float sigma)
{
//After 3 sigmas, off the charts
if(x < (mu - 3*sigma) || x > (mu + 3*sigma))
{
return 0.;
}
return exp(-(pow((x-mu),2.)/((pow(sigma,2.))*2.)))*(1/(sigma*sqrt(8.*atan(1.))));
}
float gaussian_random ( float mu, float sigma )
{
static float V1, V2, S;
static int phase = 0;
float X;
if(phase == 0) {
do {
float U1 = (float)rand() / RAND_MAX;
float U2 = (float)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return ((X*sigma)+mu);
}
float uneven_gaussian ( float mu, float sigma_left, float sigma_right )
{
float where = (float)rand() / RAND_MAX;
float value = mu;
// Let's say this is left
if(where <= 0.5)
{
value = gaussian_random(mu, sigma_left);
if(value > mu)
{
value = mu - (value - mu);
}
}
//Then, this is right
else
{
value = gaussian_random(mu, sigma_right);
if(value < mu)
{
value = mu + (mu - value);
}
}
return(value);
}
/*int main()
{
float x[] =
{
-3,
-1,
0.0,
0.5,
2.1
};
// Output computed by Mathematica
// y = Phi[x]
float y[] =
{
0.00134989803163,
0.158655253931,
0.5,
0.691462461274,
0.982135579437
};
float z[] =
{
17,
19,
20.0,
20.5,
22.1
};
int numTests = sizeof(x)/sizeof(float);
float maxError = 0.0;
for (int i = 0; i < numTests; ++i)
{
float error = fabs(y[i] - cdf1(x[i]));
if (error > maxError)
maxError = error;
}
std::cout << "%%%%%%%%%%%%%%%%%%%%%%%% CDF %%%%%%%%%%%%%%%%%%%%%%%%%%%%" << "\n\n";
std::cout << "Maximum error 1: " << maxError << "\n";
maxError = 0.0;
for (int i = 0; i < numTests; ++i)
{
float error = fabs(y[i] - cdf2(x[i]));
if (error > maxError)
maxError = error;
}
std::cout << "Maximum error 2: " << maxError << "\n";
maxError = 0.0;
for (int i = 0; i < numTests; ++i)
{
float error = fabs(y[i] - cdf3(z[i],20,1));
if (error > maxError)
maxError = error;
}
std::cout << "Maximum error 3: " << maxError << "\n";
std::cout << "%%%%%%%%%%%%%%%%%%%%%%%% PDF %%%%%%%%%%%%%%%%%%%%%%%%%%%%" << "\n\n";
float prob = 0.0, t = 0.0, mu = 20.0, sigma = 3.0;
do
{
std::cout << "Give an x: ";
std::cin >> t;
std::cout << "The probability is: " << pdf(t,mu,sigma) << "\n";
}
while(t > -50);
std::cout << "%%%%%%%%%%%%%%%%%%%%%%%% Random numbers %%%%%%%%%%%%%%%%%%%%%%%%%%%%" << "\n\n";
for(int i=1; i<= 100000; i++)
{
std::cout << gaussian_random(mu, sigma) << "\t";
if(i%6 == 0)
{
std::cout << "\n";
}
}
std::cout << "\n";
return 0;
}*/