toms462.c

filter: Statementstmt 68.3% 97 / 142 Branchbran 58.6% 34 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.5% 131 / 200 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary BivariateCDF.xs »
1 # include <stdlib.h>
2 # include <stdio.h>
3 # include <math.h>
4 # include <time.h>
5  
6 # include "toms462.h"
7  
8 /******************************************************************************/
9  
10 0 void bivariate_normal_cdf_values ( int *n_data, double *x, double *y,
11 double *r, double *fxy )
12  
13 /******************************************************************************/
14 /*
15 Purpose:
16  
17 BIVARIATE_NORMAL_CDF_VALUES returns some values of the bivariate normal CDF.
18  
19 Discussion:
20  
21 FXY is the probability that two variables A and B, which are
22 related by a bivariate normal distribution with correlation R,
23 respectively satisfy A <= X and B <= Y.
24  
25 Mathematica can evaluate the bivariate normal CDF via the commands:
26  
27 <<MultivariateStatistics`
28 cdf = CDF[MultinormalDistribution[{0,0}{{1,r},{r,1}}],{x,y}]
29  
30 Licensing:
31  
32 This code is distributed under the GNU LGPL license.
33  
34 Modified:
35  
36 23 November 2010
37  
38 Author:
39  
40 John Burkardt
41  
42 Reference:
43  
44 National Bureau of Standards,
45 Tables of the Bivariate Normal Distribution and Related Functions,
46 NBS, Applied Mathematics Series, Number 50, 1959.
47  
48 Parameters:
49  
50 Input/output, int *N_DATA. The user sets N_DATA to 0 before the
51 first call. On each call, the routine increments N_DATA by 1, and
52 returns the corresponding data; when there is no more data, the
53 output value of N_DATA will be 0 again.
54  
55 Output, double *X, *Y, the parameters of the function.
56  
57 Output, double *R, the correlation value.
58  
59 Output, double *FXY, the value of the function.
60 */
61 {
62 # define N_MAX 41
63  
64 static double fxy_vec[N_MAX] = {
65 0.02260327218569867E+00,
66 0.1548729518584100E+00,
67 0.4687428083352184E+00,
68 0.7452035868929476E+00,
69 0.8318608306874188E+00,
70 0.8410314261134202E+00,
71 0.1377019384919464E+00,
72 0.1621749501739030E+00,
73 0.1827411243233119E+00,
74 0.2010067421506235E+00,
75 0.2177751155265290E+00,
76 0.2335088436446962E+00,
77 0.2485057781834286E+00,
78 0.2629747825154868E+00,
79 0.2770729823404738E+00,
80 0.2909261168683812E+00,
81 0.3046406378726738E+00,
82 0.3183113449213638E+00,
83 0.3320262544108028E+00,
84 0.3458686754647614E+00,
85 0.3599150462310668E+00,
86 0.3742210899871168E+00,
87 0.3887706405282320E+00,
88 0.4032765198361344E+00,
89 0.4162100291953678E+00,
90 0.6508271498838664E+00,
91 0.8318608306874188E+00,
92 0.0000000000000000,
93 0.1666666666539970,
94 0.2500000000000000,
95 0.3333333333328906,
96 0.5000000000000000,
97 0.7452035868929476,
98 0.1548729518584100,
99 0.1548729518584100,
100 0.06251409470431653,
101 0.7452035868929476,
102 0.1548729518584100,
103 0.1548729518584100,
104 0.06251409470431653,
105 0.6337020457912916 };
106 static double r_vec[N_MAX] = {
107 0.500, 0.500, 0.500, 0.500, 0.500,
108 0.500, -0.900, -0.800, -0.700, -0.600,
109 -0.500, -0.400, -0.300, -0.200, -0.100,
110 0.000, 0.100, 0.200, 0.300, 0.400,
111 0.500, 0.600, 0.700, 0.800, 0.900,
112 0.673, 0.500, -1.000, -0.500, 0.000,
113 0.500, 1.000, 0.500, 0.500, 0.500,
114 0.500, 0.500, 0.500, 0.500, 0.500,
115 0.500 };
116 static double x_vec[N_MAX] = {
117 -2.0, -1.0, 0.0, 1.0, 2.0,
118 3.0, -0.2, -0.2, -0.2, -0.2,
119 -0.2, -0.2, -0.2, -0.2, -0.2,
120 -0.2, -0.2, -0.2, -0.2, -0.2,
121 -0.2, -0.2, -0.2, -0.2, -0.2,
122 1.0, 2.0, 0.0, 0.0, 0.0,
123 0.0, 0.0, 1.0, 1.0, -1.0,
124 -1.0, 1.0, 1.0, -1.0, -1.0,
125 0.7071067811865475 };
126 static double y_vec[N_MAX] = {
127 1.0, 1.0, 1.0, 1.0, 1.0,
128 1.0, 0.5, 0.5, 0.5, 0.5,
129 0.5, 0.5, 0.5, 0.5, 0.5,
130 0.5, 0.5, 0.5, 0.5, 0.5,
131 0.5, 0.5, 0.5, 0.5, 0.5,
132 0.5, 1.0, 0.0, 0.0, 0.0,
133 0.0, 0.0, 1.0, -1.0, 1.0,
134 -1.0, 1.0, -1.0, 1.0, -1.0,
135 0.7071067811865475 };
136  
137 0 ▶ if ( *n_data < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( *n_data < 0 ) 0 0 0
138 {
139 0 *n_data = 0;
140 }
141  
142 0 *n_data = *n_data + 1;
143  
144 0 ▶ if ( N_MAX < *n_data )
Branch: 1 branch
BranchTrueFalseTotal
if ( N_MAX < *n_data ) 0 0 0
145 {
146 0 *n_data = 0;
147 0 *r = 0.0;
148 0 *x = 0.0;
149 0 *y = 0.0;
150 0 *fxy = 0.0;
151 }
152 else
153 {
154 0 *r = r_vec[*n_data-1];
155 0 *x = x_vec[*n_data-1];
156 0 *y = y_vec[*n_data-1];
157 0 *fxy = fxy_vec[*n_data-1];
158 }
159  
160 0 return;
161 # undef N_MAX
162 }
163 /******************************************************************************/
164  
165 22 double bivnor ( double ah, double ak, double r )
166  
167 /******************************************************************************/
168 /*
169 Purpose:
170  
171 BIVNOR computes the bivariate normal CDF.
172  
173 Discussion:
174  
175 BIVNOR computes the probability for two normal variates X and Y
176 whose correlation is R, that AH <= X and AK <= Y.
177  
178 Licensing:
179  
180 This code is distributed under the GNU LGPL license.
181  
182 Modified:
183  
184 13 April 2012
185  
186 Author:
187  
188 Original FORTRAN77 version by Thomas Donnelly.
189 C version by John Burkardt.
190  
191 Reference:
192  
193 Thomas Donnelly,
194 Algorithm 462: Bivariate Normal Distribution,
195 Communications of the ACM,
196 October 1973, Volume 16, Number 10, page 638.
197  
198 Parameters:
199  
200 Input, double AH, AK, the lower limits of integration.
201  
202 Input, double R, the correlation between X and Y.
203  
204 Output, double BIVNOR, the bivariate normal CDF.
205  
206 Local Parameters:
207  
208 Local, int IDIG, the number of significant digits
209 to the right of the decimal point desired in the answer.
210 */
211 {
212 double a2;
213 double ap;
214 double b;
215 double cn;
216 double con;
217 double conex;
218 double ex;
219 double g2;
220 double gh;
221 double gk;
222 double gw;
223 double h2;
224 double h4;
225 int i;
226 static int idig = 15;
227 int is;
228 double rr;
229 double s1;
230 double s2;
231 double sgn;
232 double sn;
233 double sp;
234 double sqr;
235 double t;
236 static double twopi = 6.283185307179587;
237 double w2;
238 double wh;
239 double wk;
240  
241 22 b = 0.0;
242  
243 22 gh = gauss ( - ah ) / 2.0;
244 22 gk = gauss ( - ak ) / 2.0;
245  
246 22 ▶ if ( r == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( r == 0.0 ) 2 20 22
247 {
248 2 b = 4.00 * gh * gk;
249 2 b = r8_max ( b, 0.0 );
250 2 b = r8_min ( b, 1.0 );
251 2 return b;
252 }
253  
254 20 rr = ( 1.0 + r ) * ( 1.0 - r );
255  
256 20 ▶ if ( rr < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( rr < 0.0 ) 0 20 20
257 {
258 0 fprintf ( stderr, "\n" );
259 0 fprintf ( stderr, "BIVNOR - Fatal error!\n" );
260 0 fprintf ( stderr, " 1 < |R|.\n" );
261 0 exit ( 0 );
262 }
263  
264 20 ▶ if ( rr == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( rr == 0.0 ) 0 20 20
265 {
266 0 ▶ if ( r < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( r < 0.0 ) 0 0 0
267 {
268 0 ▶ if ( ah + ak < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ah + ak < 0.0 ) 0 0 0
269 {
270 0 b = 2.0 * ( gh + gk ) - 1.0;
271 }
272 }
273 else
274 {
275 0 ▶ if ( ah - ak < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ah - ak < 0.0 ) 0 0 0
276 {
277 0 b = 2.0 * gk;
278 }
279 else
280 {
281 0 b = 2.0 * gh;
282 }
283 }
284 0 b = r8_max ( b, 0.0 );
285 0 b = r8_min ( b, 1.0 );
286 0 return b;
287 }
288  
289 20 sqr = sqrt ( rr );
290  
291 20 ▶ if ( idig == 15 )
Branch: 1 branch
BranchTrueFalseTotal
if ( idig == 15 ) 20 0 20
292 {
293 20 con = twopi * 1.0E-15 / 2.0;
294 }
295 else
296 {
297 0 con = twopi / 2.0;
298 0 ▶ for ( i = 1; i <= idig; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i <= idig; i++ ) 0 0 0
299 {
300 0 con = con / 10.0;
301 }
302 }
303 /*
304 (0,0)
305 */
306 20 ▶ if ( ah == 0.0 && ak == 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
if ( ah == 0.0 && ak == 0.0 ) 1 19 20
if ( ah == 0.0 && ak == 0.0 ) 0 1 1
307 {
308 0 b = 0.25 + asin ( r ) / twopi;
309 0 b = r8_max ( b, 0.0 );
310 0 b = r8_min ( b, 1.0 );
311 0 return b;
312 }
313 /*
314 (0,nonzero)
315 */
316 20 ▶ if ( ah == 0.0 && ak != 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
if ( ah == 0.0 && ak != 0.0 ) 1 19 20
if ( ah == 0.0 && ak != 0.0 ) 1 0 1
317 {
318 1 b = gk;
319 1 wh = -ak;
320 1 wk = ( ah / ak - r ) / sqr;
321 1 gw = 2.0 * gk;
322 1 is = 1;
323 }
324 /*
325 (nonzero,0)
326 */
327 19 ▶ else if ( ah != 0.0 && ak == 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
else if ( ah != 0.0 && ak == 0.0 ) 19 0 19
else if ( ah != 0.0 && ak == 0.0 ) 1 18 19
328 {
329 1 b = gh;
330 1 wh = -ah;
331 1 wk = ( ak / ah - r ) / sqr;
332 1 gw = 2.0 * gh;
333 1 is = -1;
334 }
335 /*
336 (nonzero,nonzero)
337 */
338 18 ▶ else if ( ah != 0.0 && ak != 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
else if ( ah != 0.0 && ak != 0.0 ) 18 0 18
else if ( ah != 0.0 && ak != 0.0 ) 18 0 18
339 {
340 18 b = gh + gk;
341 18 ▶ if ( ah * ak < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ah * ak < 0.0 ) 3 15 18
342 {
343 3 b = b - 0.5;
344 }
345 18 wh = - ah;
346 18 wk = ( ak / ah - r ) / sqr;
347 18 gw = 2.0 * gh;
348 18 is = -1;
349 }
350  
351 for ( ; ; )
352 {
353 38 sgn = -1.0;
354 38 t = 0.0;
355  
356 38 ▶ if ( wk != 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( wk != 0.0 ) 38 0 38
357 {
358 38 ▶ if ( r8_abs ( wk ) == 1.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( r8_abs ( wk ) == 1.0 ) 0 38 38
359 {
360 0 t = wk * gw * ( 1.0 - gw ) / 2.0;
361 0 b = b + sgn * t;
362 }
363 else
364 {
365 38 ▶ if ( 1.0 < r8_abs ( wk ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( 1.0 < r8_abs ( wk ) ) 17 21 38
366 {
367 17 sgn = -sgn;
368 17 wh = wh * wk;
369 17 g2 = gauss ( wh );
370 17 wk = 1.0 / wk;
371  
372 17 ▶ if ( wk < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( wk < 0.0 ) 3 14 17
373 {
374 3 b = b + 0.5;
375 }
376 17 b = b - ( gw + g2 ) / 2.0 + gw * g2;
377 }
378 38 h2 = wh * wh;
379 38 a2 = wk * wk;
380 38 h4 = h2 / 2.0;
381 38 ex = exp ( - h4 );
382 38 w2 = h4 * ex;
383 38 ap = 1.0;
384 38 s2 = ap - ex;
385 38 sp = ap;
386 38 s1 = 0.0;
387 38 sn = s1;
388 38 conex = r8_abs ( con / wk );
389  
390 for ( ; ; )
391 {
392 169 cn = ap * s2 / ( sn + sp );
393 169 s1 = s1 + cn;
394  
395 169 ▶ if ( r8_abs ( cn ) <= conex )
Branch: 1 branch
BranchTrueFalseTotal
if ( r8_abs ( cn ) <= conex ) 38 131 169
396 {
397 38 break;
398 }
399 131 sn = sp;
400 131 sp = sp + 1.0;
401 131 s2 = s2 - w2;
402 131 w2 = w2 * h4 / sp;
403 131 ap = - ap * a2;
404 }
405 38 t = ( atan ( wk ) - wk * s1 ) / twopi;
406 38 b = b + sgn * t;
407 }
408 }
409 38 ▶ if ( 0 <= is )
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 <= is ) 19 19 38
410 {
411 19 break;
412 }
413 19 ▶ if ( ak == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ak == 0.0 ) 1 18 19
414 {
415 1 break;
416 }
417 18 wh = -ak;
418 18 wk = ( ah / ak - r ) / sqr;
419 18 gw = 2.0 * gk;
420 18 is = 1;
421 }
422  
423 20 b = r8_max ( b, 0.0 );
424 20 b = r8_min ( b, 1.0 );
425  
426 20 return b;
427 }
428 /******************************************************************************/
429  
430 61 double gauss ( double t )
431  
432 /******************************************************************************/
433 /*
434 Purpose:
435  
436 GAUSS returns the area of the lower tail of the normal curve.
437  
438 Licensing:
439  
440 This code is distributed under the GNU LGPL license.
441  
442 Modified:
443  
444 13 April 2012
445  
446 Author:
447  
448 John Burkardt
449  
450 Parameters:
451  
452 Input, double T, the evaluation point.
453  
454 Output, double GAUSS, the lower normal tail area.
455 */
456 {
457 double value;
458  
459 61 value = ( 1.0 + erf ( t / sqrt ( 2.0 ) ) ) / 2.0;
460  
461 61 return value;
462 }
463 /******************************************************************************/
464  
465 283 double r8_abs ( double x )
466  
467 /******************************************************************************/
468 /*
469 Purpose:
470  
471 R8_ABS returns the absolute value of an R8.
472  
473 Licensing:
474  
475 This code is distributed under the GNU LGPL license.
476  
477 Modified:
478  
479 07 May 2006
480  
481 Author:
482  
483 John Burkardt
484  
485 Parameters:
486  
487 Input, double X, the quantity whose absolute value is desired.
488  
489 Output, double R8_ABS, the absolute value of X.
490 */
491 {
492 double value;
493  
494 283 ▶ if ( 0.0 <= x )
Branch: 1 branch
BranchTrueFalseTotal
if ( 0.0 <= x ) 171 112 283
495 {
496 171 value = + x;
497 }
498 else
499 {
500 112 value = - x;
501 }
502 283 return value;
503 }
504 /******************************************************************************/
505  
506 22 double r8_max ( double x, double y )
507  
508 /******************************************************************************/
509 /*
510 Purpose:
511  
512 R8_MAX returns the maximum of two R8's.
513  
514 Licensing:
515  
516 This code is distributed under the GNU LGPL license.
517  
518 Modified:
519  
520 07 May 2006
521  
522 Author:
523  
524 John Burkardt
525  
526 Parameters:
527  
528 Input, double X, Y, the quantities to compare.
529  
530 Output, double R8_MAX, the maximum of X and Y.
531 */
532 {
533 double value;
534  
535 22 ▶ if ( y < x )
Branch: 1 branch
BranchTrueFalseTotal
if ( y < x ) 22 0 22
536 {
537 22 value = x;
538 }
539 else
540 {
541 0 value = y;
542 }
543 22 return value;
544 }
545 /******************************************************************************/
546  
547 22 double r8_min ( double x, double y )
548  
549 /******************************************************************************/
550 /*
551 Purpose:
552  
553 R8_MIN returns the minimum of two R8's.
554  
555 Licensing:
556  
557 This code is distributed under the GNU LGPL license.
558  
559 Modified:
560  
561 07 May 2006
562  
563 Author:
564  
565 John Burkardt
566  
567 Parameters:
568  
569 Input, double X, Y, the quantities to compare.
570  
571 Output, double R8_MIN, the minimum of X and Y.
572 */
573 {
574 double value;
575  
576 22 ▶ if ( y < x )
Branch: 1 branch
BranchTrueFalseTotal
if ( y < x ) 0 22 22
577 {
578 0 value = y;
579 }
580 else
581 {
582 22 value = x;
583 }
584 22 return value;
585 }
586 /******************************************************************************/
587  
588 0 void timestamp ( void )
589  
590 /******************************************************************************/
591 /*
592 Purpose:
593  
594 TIMESTAMP prints the current YMDHMS date as a time stamp.
595  
596 Example:
597  
598 31 May 2001 09:45:54 AM
599  
600 Licensing:
601  
602 This code is distributed under the GNU LGPL license.
603  
604 Modified:
605  
606 24 September 2003
607  
608 Author:
609  
610 John Burkardt
611  
612 Parameters:
613  
614 None
615 */
616 {
617 # define TIME_SIZE 40
618  
619 static char time_buffer[TIME_SIZE];
620 const struct tm *tm;
621 size_t len;
622 time_t now;
623  
624 0 now = time ( NULL );
625 0 tm = localtime ( &now );
626  
627 0 len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
628  
629 0 fprintf ( stdout, "%s\n", time_buffer );
630  
631 0 return;
632 # undef TIME_SIZE
633 }
634  
↑ Summary BivariateCDF.xs »