-
Notifications
You must be signed in to change notification settings - Fork 0
/
erflib.c
executable file
·222 lines (176 loc) · 5.57 KB
/
erflib.c
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
// erflib.c
// File imported from Perseo
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "erflib.h"
/**
* erflib.c
*
* Fornisce una libraria di funzioni per determinare
* la densita' di probabilita' di una variabile casuale
* normale, la sua funzione cumulativa (erf) e la cumulativa
* complementare (erfc).
* Note queste fornisce dei metodi per valutare funzioni
* cumulative di variabili casuali con probabilita' relative
* uguali a quelle delle variabili normali ma con dominio
* chiuso e limitato.
* Sono messe a disposizione inoltre funzioni che determi-
* nano, per le varibili a dominio limitato, gli estremi degli
* intervalli nota la probabilita' di avere valore ivi contenuto.
*
* doubleizzata da Maurizio Mattia.
* Bibliografia Numerical Recipies in C
* Versione 0.1, 18 agosto 1997.
*/
#define _TEST_ERFLIB /* Se definita compila il main che testa le funzioni. */
/**
* Returns the complementary error function erfc(x) with
* fractional error everywhere less than 1.2 x 10^-7.
*/
double erflibClass::erfcc(double x)
{
double t, z, ans;
z = fabs(x);
t = 1.0/(1.0+0.5*z);
ans = t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+
t*(0.09678418+t*(-0.18628806+t*(0.27886807+t*(-1.13520398+
t*(1.48851587+t*(-0.82215223+t*0.17087277)))))))));
return x >= 0.0 ? ans : 2.0-ans;
}
/**
* Cumulative density function of normal distribution with
* variance sigma^2 and mean mu.
*/
double erflibClass::normalCumulative (double x, double mu, double sigma)
{
return 1-0.5*erfcc((x-mu)/(sqrt(2.0)*sigma));
}
/**
* Normal distribution with variance sigma^2 and mean mu.
*/
double erflibClass::normal (double x, double mu, double sigma)
{
return 1/(sqrt(3.1415927*2)*sigma)*exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
}
/**
* Cumulative density function for a random variable with
* relative probabilies equals to a normal variable with
* mean mu and variance sigma^2, and limited domain [a,b].
*/
double erflibClass::cutNormalCumulative (double x,
double mu, double sigma,
double a, double b)
{
double n0;
n0 = normalCumulative(a, mu, sigma);
return (normalCumulative(x, mu, sigma)-n0)/(normalCumulative(b, mu, sigma)-n0);
}
/**
* Find the x value which gives cutNormalCumulative p.
*/
double erflibClass::findProbability (double p,
double mu, double sigma,
double a, double b)
{
double Error = 1e-6;
int MaxIterations = 256;
int i=0;
double x, xmin, xmax;
if (p <= 0.0) return a;
if (p >= 1.0) return b;
xmin = a; xmax = b;
while ((xmax-xmin) > Error && i<MaxIterations)
{
x = (xmax+xmin)*0.5;
if (cutNormalCumulative(x, mu, sigma, a, b) < p)
xmin = x;
else
xmax = x;
i++;
}
if (i == MaxIterations)
fprintf(stderr, "Iterazioni: %i. Errore: %g.\n", i, xmax-xmin);
return (xmax+xmin)*0.5;
}
/**
* Builds or updates a generic look-up table (LUT) for the
* off-line Monte Carlo. If needed allocates the memory for
* the table. Table is a reference to an array that will
* have IntervalNum elements. The gaussian distribution has
* mean mu and standard deviation sigma, and the range of
* the elements is [xmin,xmax].
* If Table is not a NULL pointer the LUT is not allocated
* and will be used the memory pointed by it, without any
* control. For this reason the previous size of the LUT
* have to match the new one in order to avoid any program
* crash.
* Returns 0 if the function builds correctly the LUT,
* 1 otherwise.
*/
void erflibClass::makeGaussianLUT (double *Table, int IntervalNum,
double mu, double sigma,
double xmin, double xmax)
{
double p, dp;
double x0, x1;
int i;
/*** Allocates memory, if needed. ***/
//if ((*Table) == NULL)
// if (!(*Table = malloc(sizeof(double)*IntervalNum))) return 1;
/*** Table initialization. ***/
if (sigma > 0.0) {
dp = 1.0/(double)IntervalNum;
x1 = xmin;
for (p=dp, i=0; p<=1.0; p+=dp, i++)
{
x0 = x1;
x1 = findProbability(p, mu, sigma, xmin, xmax);
Table[i] = (x1+x0)*0.5;
}
} else
for (i=0; i<IntervalNum; i++)
Table[i] = mu;
}
uint32_t erflibClass::roundr2i (double r)
{
static double i;
i = floor(r);
if (i+0.5 > r)
return (uint32_t)i;
return (uint32_t)(i+1.0);
}
#ifdef TEST_ERFLIB
/**
* Main function of test program.
*/
int main ()
{
double xmin=0.0, xmax=1.0;
int xsteps=256;
double mu=1.0, sigma=0.25;
double x0, x1;
double x, dx;
dx =(xmax-xmin)/xsteps;
/*
for (x=xmin; x<=xmax; x+=dx)
printf("%f %f\n", x,normalCumulative(x, mu,sigma));
*/
/*
for (x=xmin; x<=xmax; x+=dx)
printf("%f %f %f\n", x,
normalCumulative(x+dx, mu,sigma)-normalCumulative(x, mu,sigma),
normal(x+dx*0.5, mu, sigma)*dx);
*/
/*** Deve venire fuori una gaussiana con media mu e d.s. sigma. ***/
x1 = 0;
for (x=xmin+dx, x0=xmin; x<=xmax; x+=dx)
{
x0 = x1;
x1 = findProbability(x, mu, sigma, 0.0, 2.0);
printf("%f %f %f\n", (x1+x0)*0.5, dx/(x1-x0), normal((x1+x0)*0.5, mu, sigma));
}
return 0;
}
#endif