ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cbivnorm.cpp
Go to the documentation of this file.
1 
8 /*
9 t.f -- translated by f2c (version 20000121).
10 You must link the resulting object file with the libraries:
11 -lf2c -lm (in that order)
12 */
13 #include <fvar.hpp>
14 //#include <fstream.h>
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /* f2c.h -- Standard Fortran to C header file */
20 
25 #ifndef F2C_INCLUDE
26 #define F2C_INCLUDE
27 
28 #if defined(USE_DDOUBLE)
29 # define doublereal dd_real
30 #endif
31 typedef long int integer;
32 typedef unsigned long int uinteger;
33 //typedef char *address;
34 typedef short int shortint;
35 typedef float real;
36 typedef double doublereal;
37 typedef struct { real r, i; } complex;
38 typedef struct { doublereal r, i; } doublecomplex;
39 typedef long int logical;
40 typedef short int shortlogical;
41 typedef char logical1;
42 typedef char integer1;
43 #ifdef INTEGER_STAR_8 /* Adjust for integer*8. */
44 typedef long long longint; /* system-dependent */
45 typedef unsigned long long ulongint; /* system-dependent */
46 #define qbit_clear(a,b) ((a) & ~((ulongint)1 << (b)))
47 #define qbit_set(a,b) ((a) | ((ulongint)1 << (b)))
48 #endif
49 
50 #define TRUE_ (1)
51 #define FALSE_ (0)
52 
53 /* Extern is for use with -E */
54 #ifndef Extern
55 #define Extern extern
56 #endif
57 
58 /* I/O stuff */
59 
60 #ifdef f2c_i2
61 /* for -i2 */
62 typedef short flag;
63 typedef short ftnlen;
64 typedef short ftnint;
65 #else
66 typedef long int flag;
67 typedef long int ftnlen;
68 typedef long int ftnint;
69 #endif
70 
71 /*external read, write*/
72 typedef struct
73 {
74  flag cierr;
75  ftnint ciunit;
76  flag ciend;
77  char *cifmt;
78  ftnint cirec;
79 } cilist;
80 
81 /*internal read, write*/
82 typedef struct
83 {
84  flag icierr;
85  char *iciunit;
86  flag iciend;
87  char *icifmt;
88  ftnint icirlen;
89  ftnint icirnum;
90 } icilist;
91 
92 /*open*/
93 typedef struct
94 {
95  flag oerr;
96  ftnint ounit;
97  char *ofnm;
98  ftnlen ofnmlen;
99  char *osta;
100  char *oacc;
101  char *ofm;
102  ftnint orl;
103  char *oblnk;
104 } olist;
105 
106 /*close*/
107 typedef struct
108 {
109  flag cerr;
110  ftnint cunit;
111  char *csta;
112 } cllist;
113 
114 /*rewind, backspace, endfile*/
115 typedef struct
116 {
117  flag aerr;
118  ftnint aunit;
119 } alist;
120 
121 /* inquire */
122 typedef struct
123 {
124  flag inerr;
125  ftnint inunit;
126  char *infile;
127  ftnlen infilen;
128  ftnint *inex; /*parameters in standard's order*/
129  ftnint *inopen;
130  ftnint *innum;
131  ftnint *innamed;
132  char *inname;
133  ftnlen innamlen;
134  char *inacc;
135  ftnlen inacclen;
136  char *inseq;
137  ftnlen inseqlen;
138  char *indir;
139  ftnlen indirlen;
140  char *infmt;
141  ftnlen infmtlen;
142  char *inform;
143  ftnint informlen;
144  char *inunf;
145  ftnlen inunflen;
146  ftnint *inrecl;
147  ftnint *innrec;
148  char *inblank;
149  ftnlen inblanklen;
150 } inlist;
151 
152 #define VOID void
153  //
154  //
155  // union Multitype { /* for multiple entry points */
156  // integer1 g;
157  // shortint h;
158  // integer i;
159  // /* longint j; */
160  // real r;
161  // doublereal d;
162  // complex c;
163  // doublecomplex z;
164  // };
165  //
166  // typedef union Multitype Multitype;
167  //
168 
169 /*typedef long int Long;*/ /* No longer used; formerly in Namelist */
170 
171 struct Vardesc { /* for Namelist */
172  char *name;
173  char *addr;
174  ftnlen *dims;
175  int type;
176  };
177 typedef struct Vardesc Vardesc;
178 
179 struct Namelist {
180  char *name;
182  int nvars;
183  };
184 typedef struct Namelist Namelist;
185 
186 #define abs(x) ((x) >= 0 ? (x) : -(x))
187 #define dabs(x) (doublereal)abs(x)
188 #define min(a,b) ((a) <= (b) ? (a) : (b))
189 #define max(a,b) ((a) >= (b) ? (a) : (b))
190 #define dmin(a,b) (doublereal)min(a,b)
191 #define dmax(a,b) (doublereal)max(a,b)
192 #define bit_test(a,b) ((a) >> (b) & 1)
193 #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
194 #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
195 
196 /* procedure parameter types for -A and -C++ */
197 
198 #define F2C_proc_par_types 1
199 #ifdef __cplusplus
200 typedef int /* Unknown procedure type */ (*U_fp)(...);
201 typedef shortint (*J_fp)(...);
202 typedef integer (*I_fp)(...);
203 typedef real (*R_fp)(...);
204 typedef doublereal (*D_fp)(...), (*E_fp)(...);
205 typedef /* Complex */ VOID (*C_fp)(...);
206 typedef /* Double Complex */ VOID (*Z_fp)(...);
207 typedef logical (*L_fp)(...);
208 typedef shortlogical (*K_fp)(...);
209 typedef /* Character */ VOID (*H_fp)(...);
210 typedef /* Subroutine */ int (*S_fp)(...);
211 #else
212 typedef int /* Unknown procedure type */ (*U_fp)();
213 typedef shortint (*J_fp)();
214 typedef integer (*I_fp)();
215 typedef real (*R_fp)();
216 typedef doublereal (*D_fp)(), (*E_fp)();
217 typedef /* Complex */ VOID (*C_fp)();
218 typedef /* Double Complex */ VOID (*Z_fp)();
219 typedef logical (*L_fp)();
220 typedef shortlogical (*K_fp)();
221 typedef /* Character */ VOID (*H_fp)();
222 typedef /* Subroutine */ int (*S_fp)();
223 #endif
224 /* E_fp is for real functions when -R is not specified */
225 typedef VOID C_f; /* complex function */
226 typedef VOID H_f; /* character function */
227 typedef VOID Z_f; /* double complex function */
228 typedef doublereal E_f; /* real function with -R not specified */
229 
230 /* undef any lower-case symbols that your C compiler predefines, e.g.: */
231 
232 #ifndef Skip_f2c_Undefs
233 #undef cray
234 #undef gcos
235 #undef mc68010
236 #undef mc68020
237 #undef mips
238 #undef pdp11
239 #undef sgi
240 #undef sparc
241 #undef sun
242 #undef sun2
243 #undef sun3
244 #undef sun4
245 #undef u370
246 #undef u3b
247 #undef u3b2
248 #undef u3b5
249 #undef unix
250 #undef vax
251 #endif
252 #endif
253 double cmvbvu_(const double *sh,const double *sk,
254  const double *r__);
255 
256 #ifdef __cplusplus
257 }
258 #endif
259 
267 double cumbvn(const double& x,const double& y,const double& rho)
268 {
271 
272  double retval;
273  double mx=-x;
274  double my=-y;
275  retval=cmvbvu_(&mx,&my,&rho);
277  return retval;
278 }
279 
289 double cumbvn(const double& xl,const double& yl,
290  const double& xu,const double& yu,const double& rho)
291 {
294  double my=cumbvn(xl,yl,rho);
295  my+=cumbvn(xu,yu,rho);
296  my-=cumbvn(xl,yu,rho);
297  my-=cumbvn(xu,yl,rho);
299  return my;
300 }
301 
302 #ifdef __cplusplus
303 extern "C" {
304 #endif
305 /* BVN - calculate the probability that X is larger than SH and Y is */
306 /* larger than SK. */
307 double cmvbvu_(const double *sh,const double *sk,const double *r__)
308 {
311  /* Initialized data */
312 
313  static struct {
314  doublereal e_1[3];
315  doublereal fill_2[7];
316  doublereal e_3[6];
317  doublereal fill_4[4];
318  doublereal e_5[10];
319  } equiv_21 = { {.1713244923791705, .3607615730481384,
320  .4679139345726904}, {0, 0, 0, 0, 0, 0, 0},
321  {.04717533638651177, .1069393259953183,
322  .1600783285433464, .2031674267230659, .2334925365383547,
323  .2491470458134029}, {0, 0, 0, 0}, {.01761400713915212,
324  .04060142980038694, .06267204833410906, .08327674157670475,
325  .1019301198172404, .1181945319615184, .1316886384491766,
326  .1420961093183821, .1491729864726037, .1527533871307259}};
327 
328 #define w ((doublereal *)&equiv_21)
329 
330  static struct {
331  doublereal e_1[3];
332  doublereal fill_2[7];
333  doublereal e_3[6];
334  doublereal fill_4[4];
335  doublereal e_5[10];
336  } equiv_22 = { {-.9324695142031522, -.6612093864662647,
337  -.238619186083197}, {0, 0, 0, 0, 0, 0, 0}, {-.9815606342467191,
338  -.904117256370475,
339  -.769902674194305, -.5873179542866171, -.3678314989981802,
340  -.1252334085114692}, {0, 0, 0, 0}, {-.9931285991850949,
341  -.9639719272779138, -.9122344282513259, -.8391169718222188,
342  -.7463319064601508, -.636053680726515, -.5108670019508271,
343  -.3737060887154196, -.2277858511416451, -.07652652113349733}};
344 
345 #define x ((doublereal *)&equiv_22)
346 
347 
348  /* System generated locals */
349  integer i__1;
350  double ret_val, d__1, d__2,d__3,d__4;
351 
352  /* Builtin functions */
353  //double asin(doublereal), sin(doublereal), exp(doublereal), sqrt(
354 // doublereal);
355 
356  /* Local variables */
357  static double a, b, c__, d__, h__;
358  static integer i__;
359  //static doublereal k;
360  static double k;
361  extern double cmvphi_(double *);
362  static integer lg;
363  //static doublereal as;
364  static double as;
365  static integer ng;
366  static double bs,rs,xs;
367  static double hs, hk, sn, asr, bvn;
368 
369 
370 /* A function for computing bivariate normal probabilities; */
371 /* developed using */
372 /* Drezner, Z. and Wesolowsky, G. O. (1989), */
373 /* On the Computation of the Bivariate Normal Integral, */
374 /* J. Stat. Comput. Simul.. 35 pp. 101-107. */
375 /* with extensive modications for double precisions by */
376 /* Alan Genz and Yihong Ge */
377 /* Department of Mathematics */
378 /* Washington State University */
379 /* Pullman, WA 99164-3113 */
380 /* Email : alangenz@wsu.edu */
381 
382 /* BVN - calculate the probability that X is larger than SH and Y is */
383 /* larger than SK. */
384 
385 /* Parameters */
386 
387 /* SH REAL, integration limit */
388 /* SK REAL, integration limit */
389 /* R REAL, correlation coefficient */
390 /* LG INTEGER, number of Gauss Rule Points and Weights */
391 
392 /* Gauss Legendre Points and Weights, N = 6 */
393 /* Gauss Legendre Points and Weights, N = 12 */
394 /* Gauss Legendre Points and Weights, N = 20 */
395  if (abs(*r__) < (float).3) {
396  ng = 1;
397  lg = 3;
398  } else if (abs(*r__) < (float).75) {
399  ng = 2;
400  lg = 6;
401  } else {
402  ng = 3;
403  lg = 10;
404  }
405  h__ = *sh;
406  k = *sk;
407  hk = h__ * k;
408  bvn = 0.;
409  if (abs(*r__) < (float).925) {
410  hs = (h__ * h__ + k * k) / 2;
411  asr = asin(*r__);
412  i__1 = lg;
413  for (i__ = 1; i__ <= i__1; ++i__) {
414  sn = sin(asr * (x[i__ + ng * 10 - 11] + 1) / 2);
415  bvn += w[i__ + ng * 10 - 11] * exp((sn * hk - hs) / (1 - sn * sn));
416  sn = sin(asr * (-x[i__ + ng * 10 - 11] + 1) / 2);
417  bvn += w[i__ + ng * 10 - 11] * exp((sn * hk - hs) / (1 - sn * sn));
418  }
419  d__1 = -h__;
420  d__2 = -k;
421  bvn = bvn * asr / 12.566370614359172 + cmvphi_(&d__1) * cmvphi_(&d__2);
422  } else {
423  if (*r__ < 0.) {
424  k = -k;
425  hk = -hk;
426  }
427  if (abs(*r__) < 1.) {
428  as = (1 - *r__) * (*r__ + 1);
429  a = sqrt(as);
430 /* Computing 2nd power */
431  d__1 = h__ - k;
432  bs = d__1 * d__1;
433  c__ = (4 - hk) / 8;
434  d__ = (12 - hk) / 16;
435  bvn = a * exp(-(bs / as + hk) / 2) * (1 - c__ * (bs - as) * (1 -
436  d__ * bs / 5) / 3 + c__ * d__ * as * as / 5);
437  if (hk > -160.) {
438  b = sqrt(bs);
439  d__1 = -b / a;
440  bvn -= exp(-hk / 2) * sqrt(6.283185307179586) * cmvphi_(&d__1)
441  * b * (1 - c__ * bs * (1 - d__ * bs / 5) / 3);
442  }
443  a /= 2;
444  i__1 = lg;
445  for (i__ = 1; i__ <= i__1; ++i__) {
446 /* Computing 2nd power */
447  d__1 = a * (x[i__ + ng * 10 - 11] + 1);
448  xs = d__1 * d__1;
449  rs = sqrt(1 - xs);
450  bvn += a * w[i__ + ng * 10 - 11] * (exp(-bs / (xs * 2) - hk /
451  (rs + 1)) / rs - exp(-(bs / xs + hk) / 2) * (c__ * xs
452  * (d__ * xs + 1) + 1));
453 /* Computing 2nd power */
454  d__1 = -x[i__ + ng * 10 - 11] + 1;
455  xs = as * (d__1 * d__1) / 4;
456  rs = sqrt(1 - xs);
457  bvn += a * w[i__ + ng * 10 - 11] * exp(-(bs / xs + hk) / 2) *
458  (exp(-hk * (1 - rs) / ((rs + 1) * 2)) / rs - (c__ *
459  xs * (d__ * xs + 1) + 1));
460  }
461  bvn = -bvn / 6.283185307179586;
462  }
463  if (*r__ > 0.) {
464  d__1 = -max(h__,k);
465  bvn += cmvphi_(&d__1);
466  }
467  if (*r__ < 0.) {
468 /* Computing MAX */
469  d__3 = -h__;
470  d__4 = -k;
471  d__1 = 0., d__2 = cmvphi_(&d__3) - cmvphi_(&d__4);
472  bvn = -bvn + max(d__1,d__2);
473  }
474  }
475  ret_val = bvn;
477  return ret_val;
478 } /* cmvbvu_ */
479 
480 #undef x
481 #undef w
482 
483 double cmvphi_(double *z__)
484 {
487 
488  /* System generated locals */
489  doublereal ret_val, d__1;
490 
491  /* Builtin functions */
492 #if !defined(USE_DDOUBLE)
493  double exp(doublereal);
494 #endif
495 
496  /* Local variables */
497  static doublereal zabs, p, expntl;
498 
499 
500 /* Normal distribution probabilities accurate to 1.e-15. */
501 /* Z = no. of standard deviations from the mean. */
502 
503 /* Based upon algorithm 5666 for the error function, from: */
504 /* Hart, J.F. et al, 'Computer Approximations', Wiley 1968 */
505 
506 /* Programmer: Alan Miller */
507 
508 /* Latest revision - 30 March 1986 */
509 
510 
511  zabs = abs(*z__);
512 
513 /* |Z| > 37 */
514 
515  if (zabs > 37.) {
516  p = 0.;
517  } else {
518 /* |Z| <= 37 */
519 
520 /* Computing 2nd power */
521  d__1 = zabs;
522  expntl = exp(-(d__1 * d__1) / 2);
523 
524 /* |Z| < CUTOFF = 10/SQRT(2) */
525 
526  if (zabs < 7.071067811865475) {
527  p = expntl * ((((((zabs * .03526249659989109 + .7003830644436881)
528  * zabs + 6.37396220353165) * zabs + 33.912866078383) *
529  zabs + 112.0792914978709) * zabs + 221.2135961699311) *
530  zabs + 220.2068679123761) / (((((((zabs *
531  .08838834764831844 + 1.755667163182642) * zabs +
532  16.06417757920695) * zabs + 86.78073220294608) * zabs +
533  296.5642487796737) * zabs + 637.3336333788311) * zabs +
534  793.8265125199484) * zabs + 440.4137358247522);
535 
536 /* |Z| >= CUTOFF. */
537 
538  } else {
539  p = expntl / (zabs + 1 / (zabs + 2 / (zabs + 3 / (zabs + 4 / (
540  zabs + .65))))) / 2.506628274631001;
541  }
542  }
543  if (*z__ > 0.) {
544  p = 1 - p;
545  }
546  ret_val = p;
548  return ret_val;
549 } /* cmvphi_ */
550 
551 #ifdef __cplusplus
552 }
553 #endif
double cmvbvu_(const double *sh, const double *sk, const double *r__)
Definition: cbivnorm.cpp:307
ftnint icirnum
Definition: cbivnorm.cpp:89
long int ftnlen
Definition: cbivnorm.cpp:67
int type
Definition: cbivnorm.cpp:175
char * inblank
Definition: cbivnorm.cpp:148
ftnlen inacclen
Definition: cbivnorm.cpp:135
char * name
Definition: cbivnorm.cpp:180
float real
Definition: cbivnorm.cpp:35
ftnlen inunflen
Definition: cbivnorm.cpp:145
char logical1
Definition: cbivnorm.cpp:41
ftnlen infilen
Definition: cbivnorm.cpp:127
char * name
Definition: cbivnorm.cpp:172
int(* S_fp)()
Definition: cbivnorm.cpp:222
shortint(* J_fp)()
Definition: cbivnorm.cpp:213
ftnint icirlen
Definition: cbivnorm.cpp:88
short int shortlogical
Definition: cbivnorm.cpp:40
char * addr
Definition: cbivnorm.cpp:173
#define x
ftnint ounit
Definition: cbivnorm.cpp:96
flag icierr
Definition: cbivnorm.cpp:84
char * iciunit
Definition: cbivnorm.cpp:85
char * cifmt
Definition: cbivnorm.cpp:77
ftnlen ofnmlen
Definition: cbivnorm.cpp:98
ftnint cirec
Definition: cbivnorm.cpp:78
char * osta
Definition: cbivnorm.cpp:99
char * icifmt
Definition: cbivnorm.cpp:87
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
char * infile
Definition: cbivnorm.cpp:126
unsigned long int uinteger
Definition: cbivnorm.cpp:32
flag ciend
Definition: cbivnorm.cpp:76
flag oerr
Definition: cbivnorm.cpp:95
ftnint * inex
Definition: cbivnorm.cpp:128
char * ofnm
Definition: cbivnorm.cpp:97
ftnlen indirlen
Definition: cbivnorm.cpp:139
doublereal r
Definition: cbivnorm.cpp:38
char * oacc
Definition: cbivnorm.cpp:100
char * inacc
Definition: cbivnorm.cpp:134
ftnint inunit
Definition: cbivnorm.cpp:125
VOID(* Z_fp)()
Definition: cbivnorm.cpp:218
integer(* I_fp)()
Definition: cbivnorm.cpp:214
doublereal E_f
Definition: cbivnorm.cpp:228
#define VOID
Definition: cbivnorm.cpp:152
flag cerr
Definition: cbivnorm.cpp:109
char integer1
Definition: cbivnorm.cpp:42
long int ftnint
Definition: cbivnorm.cpp:68
real(* R_fp)()
Definition: cbivnorm.cpp:215
flag cierr
Definition: cbivnorm.cpp:74
ftnlen innamlen
Definition: cbivnorm.cpp:133
Vardesc ** vars
Definition: cbivnorm.cpp:181
flag iciend
Definition: cbivnorm.cpp:86
char * inform
Definition: cbivnorm.cpp:142
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
#define abs(x)
Definition: cbivnorm.cpp:186
long int flag
Definition: cbivnorm.cpp:66
void RETURN_ARRAYS_INCREMENT()
Definition: gradstrc.cpp:478
shortlogical(* K_fp)()
Definition: cbivnorm.cpp:220
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
doublereal(* D_fp)()
Definition: cbivnorm.cpp:216
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
VOID C_f
Definition: cbivnorm.cpp:225
VOID(* C_fp)()
Definition: cbivnorm.cpp:217
static _THREAD gradient_structure * _instance
ftnlen inseqlen
Definition: cbivnorm.cpp:137
doublereal(*)(* E_fp)()
Definition: cbivnorm.cpp:216
char * oblnk
Definition: cbivnorm.cpp:103
ftnint * inrecl
Definition: cbivnorm.cpp:146
ftnint * innrec
Definition: cbivnorm.cpp:147
ftnint * innum
Definition: cbivnorm.cpp:130
ftnint informlen
Definition: cbivnorm.cpp:143
ftnlen infmtlen
Definition: cbivnorm.cpp:141
long int integer
Definition: cbivnorm.cpp:31
long int logical
Definition: cbivnorm.cpp:39
VOID H_f
Definition: cbivnorm.cpp:226
ftnlen inblanklen
Definition: cbivnorm.cpp:149
ftnint cunit
Definition: cbivnorm.cpp:110
VOID(* H_fp)()
Definition: cbivnorm.cpp:221
#define w
char * ofm
Definition: cbivnorm.cpp:101
char * indir
Definition: cbivnorm.cpp:138
ftnint aunit
Definition: cbivnorm.cpp:118
char * inname
Definition: cbivnorm.cpp:132
double cumbvn(const double &x, const double &y, const double &rho)
Cumulative bivariate normal distribution.
Definition: cbivnorm.cpp:267
void RETURN_ARRAYS_DECREMENT()
Definition: gradstrc.cpp:511
char * inseq
Definition: cbivnorm.cpp:136
char * infmt
Definition: cbivnorm.cpp:140
#define max(a, b)
Definition: cbivnorm.cpp:189
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
double doublereal
Definition: cbivnorm.cpp:36
flag aerr
Definition: cbivnorm.cpp:117
ftnint ciunit
Definition: cbivnorm.cpp:75
char * csta
Definition: cbivnorm.cpp:111
int nvars
Definition: cbivnorm.cpp:182
ftnint * innamed
Definition: cbivnorm.cpp:131
ftnint * inopen
Definition: cbivnorm.cpp:129
flag inerr
Definition: cbivnorm.cpp:124
logical(* L_fp)()
Definition: cbivnorm.cpp:219
VOID Z_f
Definition: cbivnorm.cpp:227
char * inunf
Definition: cbivnorm.cpp:144
double cmvphi_(double *z__)
Definition: cbivnorm.cpp:483
ftnint orl
Definition: cbivnorm.cpp:102
short int shortint
Definition: cbivnorm.cpp:34
real r
Definition: cbivnorm.cpp:37
ftnlen * dims
Definition: cbivnorm.cpp:174
dvector asin(const dvector &vec)
Returns dvector with principal value of the arc sine of vec, expressed in radians.
Definition: dvect6.cpp:229