toms462.c

filter: Statementstmt 52.1% 74 / 142 Branchbran 39.6% 23 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 48.5% 97 / 200 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« BivariateCDF.c ↑ Summary
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 1 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 1 b = 0.0;
242  
243 1 gh = gauss ( - ah ) / 2.0;
244 1 gk = gauss ( - ak ) / 2.0;
245  
246 1 ▶ if ( r == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( r == 0.0 ) 0 1 1
247 {
248 0 b = 4.00 * gh * gk;
249 0 b = r8_max ( b, 0.0 );
250 0 b = r8_min ( b, 1.0 );
251 0 return b;
252 }
253  
254 1 rr = ( 1.0 + r ) * ( 1.0 - r );
255  
256 1 ▶ if ( rr < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( rr < 0.0 ) 0 1 1
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 1 ▶ if ( rr == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( rr == 0.0 ) 0 1 1
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 1 sqr = sqrt ( rr );
290  
291 1 ▶ if ( idig == 15 )
Branch: 1 branch
BranchTrueFalseTotal
if ( idig == 15 ) 1 0 1
292 {
293 1 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 1 ▶ if ( ah == 0.0 && ak == 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
if ( ah == 0.0 && ak == 0.0 ) 0 1 1
if ( ah == 0.0 && ak == 0.0 ) 0 0 0
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 1 ▶ if ( ah == 0.0 && ak != 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
if ( ah == 0.0 && ak != 0.0 ) 0 1 1
if ( ah == 0.0 && ak != 0.0 ) 0 0 0
317 {
318 0 b = gk;
319 0 wh = -ak;
320 0 wk = ( ah / ak - r ) / sqr;
321 0 gw = 2.0 * gk;
322 0 is = 1;
323 }
324 /*
325 (nonzero,0)
326 */
327 1 ▶ else if ( ah != 0.0 && ak == 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
else if ( ah != 0.0 && ak == 0.0 ) 1 0 1
else if ( ah != 0.0 && ak == 0.0 ) 0 1 1
328 {
329 0 b = gh;
330 0 wh = -ah;
331 0 wk = ( ak / ah - r ) / sqr;
332 0 gw = 2.0 * gh;
333 0 is = -1;
334 }
335 /*
336 (nonzero,nonzero)
337 */
338 1 ▶ else if ( ah != 0.0 && ak != 0.0 )
Branch: 2 branches
BranchTrueFalseTotal
else if ( ah != 0.0 && ak != 0.0 ) 1 0 1
else if ( ah != 0.0 && ak != 0.0 ) 1 0 1
339 {
340 1 b = gh + gk;
341 1 ▶ if ( ah * ak < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ah * ak < 0.0 ) 0 1 1
342 {
343 0 b = b - 0.5;
344 }
345 1 wh = - ah;
346 1 wk = ( ak / ah - r ) / sqr;
347 1 gw = 2.0 * gh;
348 1 is = -1;
349 }
350  
351 for ( ; ; )
352 {
353 2 sgn = -1.0;
354 2 t = 0.0;
355  
356 2 ▶ if ( wk != 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( wk != 0.0 ) 2 0 2
357 {
358 2 ▶ if ( r8_abs ( wk ) == 1.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( r8_abs ( wk ) == 1.0 ) 0 2 2
359 {
360 0 t = wk * gw * ( 1.0 - gw ) / 2.0;
361 0 b = b + sgn * t;
362 }
363 else
364 {
365 2 ▶ if ( 1.0 < r8_abs ( wk ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( 1.0 < r8_abs ( wk ) ) 0 2 2
366 {
367 0 sgn = -sgn;
368 0 wh = wh * wk;
369 0 g2 = gauss ( wh );
370 0 wk = 1.0 / wk;
371  
372 0 ▶ if ( wk < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( wk < 0.0 ) 0 0 0
373 {
374 0 b = b + 0.5;
375 }
376 0 b = b - ( gw + g2 ) / 2.0 + gw * g2;
377 }
378 2 h2 = wh * wh;
379 2 a2 = wk * wk;
380 2 h4 = h2 / 2.0;
381 2 ex = exp ( - h4 );
382 2 w2 = h4 * ex;
383 2 ap = 1.0;
384 2 s2 = ap - ex;
385 2 sp = ap;
386 2 s1 = 0.0;
387 2 sn = s1;
388 2 conex = r8_abs ( con / wk );
389  
390 for ( ; ; )
391 {
392 8 cn = ap * s2 / ( sn + sp );
393 8 s1 = s1 + cn;
394  
395 8 ▶ if ( r8_abs ( cn ) <= conex )
Branch: 1 branch
BranchTrueFalseTotal
if ( r8_abs ( cn ) <= conex ) 2 6 8
396 {
397 2 break;
398 }
399 6 sn = sp;
400 6 sp = sp + 1.0;
401 6 s2 = s2 - w2;
402 6 w2 = w2 * h4 / sp;
403 6 ap = - ap * a2;
404 }
405 2 t = ( atan ( wk ) - wk * s1 ) / twopi;
406 2 b = b + sgn * t;
407 }
408 }
409 2 ▶ if ( 0 <= is )
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 <= is ) 1 1 2
410 {
411 1 break;
412 }
413 1 ▶ if ( ak == 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ak == 0.0 ) 0 1 1
414 {
415 0 break;
416 }
417 1 wh = -ak;
418 1 wk = ( ah / ak - r ) / sqr;
419 1 gw = 2.0 * gk;
420 1 is = 1;
421 }
422  
423 1 b = r8_max ( b, 0.0 );
424 1 b = r8_min ( b, 1.0 );
425  
426 1 return b;
427 }
428 /******************************************************************************/
429  
430 2 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 2 value = ( 1.0 + erf ( t / sqrt ( 2.0 ) ) ) / 2.0;
460  
461 2 return value;
462 }
463 /******************************************************************************/
464  
465 14 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 14 ▶ if ( 0.0 <= x )
Branch: 1 branch
BranchTrueFalseTotal
if ( 0.0 <= x ) 10 4 14
495 {
496 10 value = + x;
497 }
498 else
499 {
500 4 value = - x;
501 }
502 14 return value;
503 }
504 /******************************************************************************/
505  
506 1 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 1 ▶ if ( y < x )
Branch: 1 branch
BranchTrueFalseTotal
if ( y < x ) 1 0 1
536 {
537 1 value = x;
538 }
539 else
540 {
541 0 value = y;
542 }
543 1 return value;
544 }
545 /******************************************************************************/
546  
547 1 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 1 ▶ if ( y < x )
Branch: 1 branch
BranchTrueFalseTotal
if ( y < x ) 0 1 1
577 {
578 0 value = y;
579 }
580 else
581 {
582 1 value = x;
583 }
584 1 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  
« BivariateCDF.c ↑ Summary