TPCCLIB
Loading...
Searching...
No Matches
normaldistr.c
Go to the documentation of this file.
1
5/******************************************************************************/
6#include "libtpcmodel.h"
7/******************************************************************************/
8
9/******************************************************************************/
17double ndtr (
19 double a
20) {
21 double x, y, z;
22
23 x=a*M_SQRT1_2;
24 z=fabs(x);
25
26 if(z<1.0) {
27 y=0.5+0.5*erf(x);
28 } else {
29 y=0.5*erfc(z);
30 if(x>0) y=1.0-y;
31 }
32 return(y);
33}
34/******************************************************************************/
35
36/******************************************************************************/
45 double x
46) {
47 double p = (x<0.0)? ndtr(x) : ndtr(-x);
48 return 2.0*p;
49}
50/******************************************************************************/
51
52/******************************************************************************/
62 double x
63) {
64 return 1.0-ndtr(x);
65}
66/******************************************************************************/
67
68/******************************************************************************/
69
70#if(0)
71
72
73/*
74 *
75 * Normal distribution function
76 *
77 *
78 *
79 * SYNOPSIS:
80 *
81 * double x, y, ndtr();
82 *
83 * y = ndtr( x );
84 *
85 *
86 *
87 * DESCRIPTION:
88 *
89 * Returns the area under the Gaussian probability density
90 * function, integrated from minus infinity to x:
91 *
92 * x
93 * -
94 * 1 | | 2
95 * ndtr(x) = --------- | exp( - t /2 ) dt
96 * sqrt(2pi) | |
97 * -
98 * -inf.
99 *
100 * = ( 1 + erf(z) ) / 2
101 * = erfc(z) / 2
102 *
103 * where z = x/sqrt(2). Computation is via the functions
104 * erf and erfc with care to avoid error amplification in computing exp(-x^2).
105 *
106 *
107 * ACCURACY:
108 *
109 * Relative error:
110 * arithmetic domain # trials peak rms
111 * IEEE -13,0 30000 1.3e-15 2.2e-16
112 *
113 *
114 * ERROR MESSAGES:
115 *
116 * message condition value returned
117 * erfc underflow x > 37.519379347 0.0
118 *
119 */
120/* erf.c
121 *
122 * Error function
123 *
124 *
125 *
126 * SYNOPSIS:
127 *
128 * double x, y, erf();
129 *
130 * y = erf( x );
131 *
132 *
133 *
134 * DESCRIPTION:
135 *
136 * The integral is
137 *
138 * x
139 * -
140 * 2 | | 2
141 * erf(x) = -------- | exp( - t ) dt.
142 * sqrt(pi) | |
143 * -
144 * 0
145 *
146 * The magnitude of x is limited to 9.231948545 for DEC
147 * arithmetic; 1 or -1 is returned outside this range.
148 *
149 * For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
150 * erf(x) = 1 - erfc(x).
151 *
152 *
153 *
154 * ACCURACY:
155 *
156 * Relative error:
157 * arithmetic domain # trials peak rms
158 * DEC 0,1 14000 4.7e-17 1.5e-17
159 * IEEE 0,1 30000 3.7e-16 1.0e-16
160 *
161 */
162 /* erfc.c
163 *
164 * Complementary error function
165 *
166 *
167 *
168 * SYNOPSIS:
169 *
170 * double x, y, erfc();
171 *
172 * y = erfc( x );
173 *
174 *
175 *
176 * DESCRIPTION:
177 *
178 *
179 * 1 - erf(x) =
180 *
181 * inf.
182 * -
183 * 2 | | 2
184 * erfc(x) = -------- | exp( - t ) dt
185 * sqrt(pi) | |
186 * -
187 * x
188 *
189 *
190 * For small x, erfc(x) = 1 - erf(x); otherwise rational
191 * approximations are computed.
192 *
193 * A special function expx2.c is used to suppress error amplification
194 * in computing exp(-x^2).
195 *
196 *
197 * ACCURACY:
198 *
199 * Relative error:
200 * arithmetic domain # trials peak rms
201 * IEEE 0,26.6417 30000 1.3e-15 2.2e-16
202 *
203 *
204 * ERROR MESSAGES:
205 *
206 * message condition value returned
207 * erfc underflow x > 9.231948545 (DEC) 0.0
208 *
209 *
210 */
211
212/*
213Cephes Math Library Release 2.9: November, 2000
214Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
215*/
216
217/*#include "mconf.h"*/
218
219/* Define this macro to suppress error propagation in exp(x^2)
220 by using the expx2 function. The tradeoff is that doing so
221 generates two calls to the exponential function instead of one. */
222
223//extern double MAXLOG;
224#define USE_EXPXSQ 1
225
226static double P[] = {
227 2.46196981473530512524E-10,
228 5.64189564831068821977E-1,
229 7.46321056442269912687E0,
230 4.86371970985681366614E1,
231 1.96520832956077098242E2,
232 5.26445194995477358631E2,
233 9.34528527171957607540E2,
234 1.02755188689515710272E3,
235 5.57535335369399327526E2
236};
237static double Q[] = {
238 /* 1.00000000000000000000E0,*/
239 1.32281951154744992508E1,
240 8.67072140885989742329E1,
241 3.54937778887819891062E2,
242 9.75708501743205489753E2,
243 1.82390916687909736289E3,
244 2.24633760818710981792E3,
245 1.65666309194161350182E3,
246 5.57535340817727675546E2
247};
248static double R[] = {
249 5.64189583547755073984E-1,
250 1.27536670759978104416E0,
251 5.01905042251180477414E0,
252 6.16021097993053585195E0,
253 7.40974269950448939160E0,
254 2.97886665372100240670E0
255};
256static double S[] = {
257 /* 1.00000000000000000000E0,*/
258 2.26052863220117276590E0,
259 9.39603524938001434673E0,
260 1.20489539808096656605E1,
261 1.70814450747565897222E1,
262 9.60896809063285878198E0,
263 3.36907645100081516050E0
264};
265static double T[] = {
266 9.60497373987051638749E0,
267 9.00260197203842689217E1,
268 2.23200534594684319226E3,
269 7.00332514112805075473E3,
270 5.55923013010394962768E4
271};
272static double U[] = {
273 /* 1.00000000000000000000E0,*/
274 3.35617141647503099647E1,
275 5.21357949780152679795E2,
276 4.59432382970980127987E3,
277 2.26290000613890934246E4,
278 4.92673942608635921086E4
279};
280
281#define UTHRESH 37.519379347
282
283
284double cumulativeNormal(double x) {
285 return(0.5*erfc(-x*M_SQRT1_2));
286}
287
291double ndtr (
293 double a
294) {
295 double x, y, z;
296
297 x = a * SQRTH;
298 z = fabs(x);
299
300 /* if( z < SQRTH ) */
301 if (z < 1.0) {
302 y = 0.5 + 0.5 * cephes_erf(x);
303
304 } else {
305#ifdef USE_EXPXSQ
306 /* See below for erfce. */
307 y = 0.5 * erfce(z);
308 /* Multiply by exp(-x^2 / 2) */
309 z = expx2(a, -1);
310 y = y * sqrt(z);
311#else
312 y = 0.5 * cephes_erfc(z);
313#endif
314 if (x > 0) {
315 y = 1.0 - y;
316 }
317 }
318
319 return y;
320}
325static double cephes_erfc (
326 double a
327)
328{
329 double p, q, x, y, z;
330
331 if (a < 0.0) {
332 x = -a;
333 } else {
334 x = a;
335 }
336
337 if (x < 1.0) {
338 return 1.0 - cephes_erf(a);
339 }
340
341 z = -a * a;
342
343 if (z < -MAXLOG) {
344 under:
345 fprintf(stderr,"Error: Underflow\n");
346 if (a < 0) {
347 return 2.0;
348 } else {
349 return 0.0;
350 }
351 }
352
353#ifdef USE_EXPXSQ
354 /* Compute z = exp(z). */
355 z = expx2(a, -1);
356#else
357 z = exp(z);
358#endif
359
360 if( x < 8.0 ) {
361 p = polevl(x, P, 8);
362 q = p1evl(x, Q, 8);
363 } else {
364 p = polevl(x, R, 5);
365 q = p1evl(x, S, 6);
366 }
367
368 y = (z * p)/q;
369
370 if (a < 0) {
371 y = 2.0 - y;
372 }
373
374 if (y == 0.0) {
375 goto under;
376 }
377
378 return y;
379}
380
381
388static double erfce (double x)
389{
390 double p,q;
391
392 if (x < 8.0) {
393 p = polevl(x, P, 8);
394 q = p1evl(x, Q, 8);
395 } else {
396 p = polevl(x, R, 5);
397 q = p1evl(x, S, 6);
398 }
399
400 return p/q;
401}
405static double cephes_erf (
406 double x
407)
408{
409 double y, z;
410
411 if (fabs(x) > 1.0) {
412 return 1.0 - cephes_erfc(x);
413 }
414
415 z = x * x;
416 y = x * polevl(z, T, 4) / p1evl(z, U, 5);
417
418 return y;
419
420}
421
423double ndtr_new(
424 double a
425) {
426 double x, y, z;
427
428 x = a * M_SQRT1_2;
429 z = fabs(x);
430
431 if(z<1.0) {
432 y=0.5+0.5*erf(x);
433 } else {
434 y=0.5*erfc(z);
435 if(x>0) y=1.0-y;
436 }
437 return(y);
438}
439#endif
Header file for libtpcmodel.
double normal_pvalue_1(double x)
Definition normaldistr.c:60
double ndtr(double a)
Definition normaldistr.c:17
double normal_pvalue_2(double x)
Definition normaldistr.c:43