ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vbivnorm.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Authors: Alan Genz and Yihong Ge
5  * Copyright (c) 2009-2012 ADMB foundation
6  *
7  * Ported to C++ and extensively modified by David Fournier
8  */
14 /* t.f -- translated by f2c (version 20000121).
15  You must link the resulting object file with the libraries:
16  -lf2c -lm (in that order)
17 */
18 #include <fvar.hpp>
19 //#include <fstream.h>
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /* f2c.h -- Standard Fortran to C header file */
25 
30 #ifndef F2C_INCLUDE
31 #define F2C_INCLUDE
32 
33 typedef long int integer;
34 typedef unsigned long int uinteger;
35 //typedef char *address;
36 typedef short int shortint;
37 typedef double real;
38 typedef double doublereal;
39 typedef struct { real r, i; } complex;
40 typedef struct { doublereal r, i; } doublecomplex;
41 typedef long int logical;
42 typedef short int shortlogical;
43 typedef char logical1;
44 typedef char integer1;
45 #ifdef INTEGER_STAR_8 /* Adjust for integer*8. */
46 typedef long long longint; /* system-dependent */
47 typedef unsigned long long ulongint; /* system-dependent */
48 #define qbit_clear(a,b) ((a) & ~((ulongint)1 << (b)))
49 #define qbit_set(a,b) ((a) | ((ulongint)1 << (b)))
50 #endif
51 
52 #define TRUE_ (1)
53 #define FALSE_ (0)
54 
55 /* Extern is for use with -E */
56 #ifndef Extern
57 #define Extern extern
58 #endif
59 
60 /* I/O stuff */
61 
62 #ifdef f2c_i2
63 /* for -i2 */
64 typedef short flag;
65 typedef short ftnlen;
66 typedef short ftnint;
67 #else
68 typedef long int flag;
69 typedef long int ftnlen;
70 typedef long int ftnint;
71 #endif
72 
73 /*external read, write*/
74 typedef struct
75 { flag cierr;
76  ftnint ciunit;
77  flag ciend;
78  char *cifmt;
79  ftnint cirec;
80 } cilist;
81 
82 /*internal read, write*/
83 typedef struct
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 { flag oerr;
95  ftnint ounit;
96  char *ofnm;
97  ftnlen ofnmlen;
98  char *osta;
99  char *oacc;
100  char *ofm;
101  ftnint orl;
102  char *oblnk;
103 } olist;
104 
105 /*close*/
106 typedef struct
107 { flag cerr;
108  ftnint cunit;
109  char *csta;
110 } cllist;
111 
112 /*rewind, backspace, endfile*/
113 typedef struct
114 { flag aerr;
115  ftnint aunit;
116 } alist;
117 
118 /* inquire */
119 typedef struct
120 { flag inerr;
121  ftnint inunit;
122  char *infile;
123  ftnlen infilen;
124  ftnint *inex; /*parameters in standard's order*/
125  ftnint *inopen;
126  ftnint *innum;
127  ftnint *innamed;
128  char *inname;
129  ftnlen innamlen;
130  char *inacc;
131  ftnlen inacclen;
132  char *inseq;
133  ftnlen inseqlen;
134  char *indir;
135  ftnlen indirlen;
136  char *infmt;
137  ftnlen infmtlen;
138  char *inform;
139  ftnint informlen;
140  char *inunf;
141  ftnlen inunflen;
142  ftnint *inrecl;
143  ftnint *innrec;
144  char *inblank;
145  ftnlen inblanklen;
146 } inlist;
147 
148 #define VOID void
149  //
150  // union Multitype { /* for multiple entry points */
151  // integer1 g;
152  // shortint h;
153  // integer i;
154  // /* longint j; */
155  // real r;
156  // doublereal d;
157  // complex c;
158  // doublecomplex z;
159  // };
160  //
161  // typedef union Multitype Multitype;
162  //
163 /*typedef long int Long;*/ /* No longer used; formerly in Namelist */
164 
165 struct Vardesc { /* for Namelist */
166  char *name;
167  char *addr;
168  ftnlen *dims;
169  int type;
170  };
171 typedef struct Vardesc Vardesc;
172 
173 struct Namelist {
174  char *name;
175  Vardesc **vars;
176  int nvars;
177  };
178 typedef struct Namelist Namelist;
179 
180 #define abs(x) ((x) >= 0 ? (x) : -(x))
181 #define dabs(x) (doublereal)abs(x)
182 #define min(a,b) ((a) <= (b) ? (a) : (b))
183 #define max(a,b) ((a) >= (b) ? (a) : (b))
184 #define dmin(a,b) (doublereal)min(a,b)
185 #define dmax(a,b) (doublereal)max(a,b)
186 #define bit_test(a,b) ((a) >> (b) & 1)
187 #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
188 #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
189 
190 /* procedure parameter types for -A and -C++ */
191 
192 #define F2C_proc_par_types 1
193 #ifdef __cplusplus
194 typedef int /* Unknown procedure type */ (*U_fp)(...);
195 typedef shortint (*J_fp)(...);
196 typedef integer (*I_fp)(...);
197 typedef real (*R_fp)(...);
198 typedef doublereal (*D_fp)(...), (*E_fp)(...);
199 typedef /* Complex */ VOID (*C_fp)(...);
200 typedef /* Double Complex */ VOID (*Z_fp)(...);
201 typedef logical (*L_fp)(...);
202 typedef shortlogical (*K_fp)(...);
203 typedef /* Character */ VOID (*H_fp)(...);
204 typedef /* Subroutine */ int (*S_fp)(...);
205 #else
206 typedef int /* Unknown procedure type */ (*U_fp)();
207 typedef shortint (*J_fp)();
208 typedef integer (*I_fp)();
209 typedef real (*R_fp)();
210 typedef doublereal (*D_fp)(), (*E_fp)();
211 typedef /* Complex */ VOID (*C_fp)();
212 typedef /* Double Complex */ VOID (*Z_fp)();
213 typedef logical (*L_fp)();
214 typedef shortlogical (*K_fp)();
215 typedef /* Character */ VOID (*H_fp)();
216 typedef /* Subroutine */ int (*S_fp)();
217 #endif
218 /* E_fp is for real functions when -R is not specified */
219 typedef VOID C_f; /* complex function */
220 typedef VOID H_f; /* character function */
221 typedef VOID Z_f; /* double complex function */
222 typedef doublereal E_f; /* real function with -R not specified */
223 
224 /* undef any lower-case symbols that your C compiler predefines, e.g.: */
225 
226 #ifndef Skip_f2c_Undefs
227 #undef cray
228 #undef gcos
229 #undef mc68010
230 #undef mc68020
231 #undef mips
232 #undef pdp11
233 #undef sgi
234 #undef sparc
235 #undef sun
236 #undef sun2
237 #undef sun3
238 #undef sun4
239 #undef u370
240 #undef u3b
241 #undef u3b2
242 #undef u3b5
243 #undef unix
244 #undef vax
245 #endif
246 #endif
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 dvariable mvbvu_(const dvariable *sh,const dvariable *sk,
253  const dvariable *r__);
254 
262 dvariable cumbvn(const dvariable& x,const dvariable& y,const dvariable& rho)
263 {
266  dvariable retval;
267  dvariable mx=-x;
268  dvariable my=-y;
269  retval=mvbvu_(&mx,&my,&rho);
271  return retval;
272 }
273 
283 dvariable cumbvn(const dvariable& xl,const dvariable& yl,
284  const dvariable& xu,const dvariable& yu,const dvariable& rho)
285 {
288  dvariable my=cumbvn(xl,yl,rho);
289  my+=cumbvn(xu,yu,rho);
290  my-=cumbvn(xl,yu,rho);
291  my-=cumbvn(xu,yl,rho);
293  return my;
294 }
295 
297 
298 dvariable mvbvu_(const dvariable *sh,const dvariable *sk,const dvariable *r__)
299 {
300  //cout << " " << *r__;
303  //dvariable pr;
304  //if (*zz>0)
305  // pr=sfabs(*zz);
306  //else
307  // pr=-sfabs(*zz);
308  //dvariable * r__=&pr;
309  /* Initialized data */
310  //if (abs(*r__)<1.e-6)
311  // cout << " " << *r__ << endl;
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},
338  {-.9815606342467191, -.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  dvariable 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*/ dvariable a, b, c__, d__, h__;
358  static integer i__;
359  //static doublereal k;
360  /*static*/ dvariable k;
361  static integer lg;
362  //static doublereal as;
363  /*static*/ dvariable as;
364  static integer ng;
365  /*static*/ dvariable bs,rs,xs;
366  /*static*/ dvariable hs, hk, sn, asr, bvn;
367 
368 
369 /* A function for computing bivariate normal probabilities; */
370 /* developed using */
371 /* Drezner, Z. and Wesolowsky, G. O. (1989), */
372 /* On the Computation of the Bivariate Normal Integral, */
373 /* J. Stat. Comput. Simul.. 35 pp. 101-107. */
374 /* with extensive modications for double precisions by */
375 /* Alan Genz and Yihong Ge */
376 /* Department of Mathematics */
377 /* Washington State University */
378 /* Pullman, WA 99164-3113 */
379 /* Email : alangenz@wsu.edu */
380 
381 /* BVN - calculate the probability that X is larger than SH and Y is */
382 /* larger than SK. */
383 
384 /* Parameters */
385 
386 /* SH REAL, integration limit */
387 /* SK REAL, integration limit */
388 /* R REAL, correlation coefficient */
389 /* LG INTEGER, number of Gauss Rule Points and Weights */
390 
391 /* Gauss Legendre Points and Weights, N = 6 */
392 /* Gauss Legendre Points and Weights, N = 12 */
393 /* Gauss Legendre Points and Weights, N = 20 */
394  /*
395  if (abs(abs(*r__) - (double).0)<1.e-5)
396  cout << " " << *r__ << endl;
397  if (abs(abs(*r__) - (double).3)<1.e-5)
398  cout << " " << *r__ << endl;
399  if (abs(abs(*r__) - (double).75)<1.e-5)
400  cout << " " << *r__ << endl;
401  if (abs(abs(*r__) - (double).925)<1.e-5)
402  cout << " " << *r__ << endl;
403  if (abs(abs(*r__) - (double)1.00)<1.e-5)
404  cout << " " << *r__ << endl;
405  */
406 
407  if (abs(value(*r__)) < (double).3) {
408  ng = 1;
409  lg = 3;
410  } else if (abs(value(*r__)) < (double).75) {
411  ng = 2;
412  lg = 6;
413  } else {
414  ng = 3;
415  lg = 10;
416  }
417  h__ = *sh;
418  k = *sk;
419  hk = h__ * k;
420  bvn = 0.;
421  if (abs(value(*r__)) < (double).925) {
422  hs = (h__ * h__ + k * k) / 2;
423  asr = asin(*r__);
424  i__1 = lg;
425  for (i__ = 1; i__ <= i__1; ++i__) {
426  sn = sin(asr * (x[i__ + ng * 10 - 11] + 1) / 2);
427  bvn += w[i__ + ng * 10 - 11] * exp((sn * hk - hs) / (1 - sn * sn))
428  ;
429  sn = sin(asr * (-x[i__ + ng * 10 - 11] + 1) / 2);
430  bvn += w[i__ + ng * 10 - 11] * exp((sn * hk - hs) / (1 - sn * sn))
431  ;
432  }
433  d__1 = -h__;
434  d__2 = -k;
435  bvn = bvn * asr / 12.566370614359172 + mvphi_(&d__1) * mvphi_(&d__2);
436  } else {
437  if (value(*r__) < 0.) {
438  k = -k;
439  hk = -hk;
440  }
441  if (abs(value(*r__)) < 1.) {
442  as = (1 - *r__) * (*r__ + 1);
443  a = sqrt(as);
444 /* Computing 2nd power */
445  d__1 = h__ - k;
446  bs = d__1 * d__1;
447  c__ = (4 - hk) / 8;
448  d__ = (12 - hk) / 16;
449  bvn = a * exp(-(bs / as + hk) / 2) * (1 - c__ * (bs - as) * (1 -
450  d__ * bs / 5) / 3 + c__ * d__ * as * as / 5);
451  if (hk > -160.) {
452  b = sqrt(bs);
453  d__1 = -b / a;
454  bvn -= exp(-hk / 2) * sqrt(6.283185307179586) * mvphi_(&d__1)
455  * b * (1 - c__ * bs * (1 - d__ * bs / 5) / 3);
456  }
457  a /= 2;
458  i__1 = lg;
459  for (i__ = 1; i__ <= i__1; ++i__) {
460 /* Computing 2nd power */
461  d__1 = a * (x[i__ + ng * 10 - 11] + 1);
462  xs = d__1 * d__1;
463  rs = sqrt(1 - xs);
464  bvn += a * w[i__ + ng * 10 - 11] * (exp(-bs / (xs * 2) - hk /
465  (rs + 1)) / rs - exp(-(bs / xs + hk) / 2) * (c__ * xs
466  * (d__ * xs + 1) + 1));
467 /* Computing 2nd power */
468  d__1 = -x[i__ + ng * 10 - 11] + 1;
469  xs = as * (d__1 * d__1) / 4;
470  rs = sqrt(1 - xs);
471  bvn += a * w[i__ + ng * 10 - 11] * exp(-(bs / xs + hk) / 2) *
472  (exp(-hk * (1 - rs) / ((rs + 1) * 2)) / rs - (c__ *
473  xs * (d__ * xs + 1) + 1));
474  }
475  bvn = -bvn / 6.283185307179586;
476  }
477  if (*r__ > 0.) {
478  d__1 = -max(h__,k);
479  bvn += mvphi_(&d__1);
480  }
481  if (*r__ < 0.) {
482 /* Computing MAX */
483  d__3 = -h__;
484  d__4 = -k;
485  d__1 = 0., d__2 = mvphi_(&d__3) - mvphi_(&d__4);
486  bvn = -bvn + max(d__1,d__2);
487  }
488  }
489  ret_val = bvn;
491  return ret_val;
492 } /* mvbvu_ */
493 
494 #undef x
495 #undef w
496 
497 //dvariable mvphi_(dvariable *z__)
498 //{
499 // return cumd_norm(*z__);
500 //}
502 
504 {
505  if (debug_switch) cout << " " << *z__;
508  /* System generated locals */
509  dvariable d__1,ret_val;
510 
511  /* Builtin functions */
512  //double exp(doublereal);
513 
514  /* Local variables */
515  /*static*/ dvariable zabs, expntl,p;
516 
517 
518 /* Normal distribution probabilities accurate to 1.e-15. */
519 /* Z = no. of standard deviations from the mean. */
520 
521 /* Based upon algorithm 5666 for the error function, from: */
522 /* Hart, J.F. et al, 'Computer Approximations', Wiley 1968 */
523 
524 /* Programmer: Alan Miller */
525 
526 /* Latest revision - 30 March 1986 */
527 
528 
529  if (*z__>0)
530  zabs = *z__;
531  else
532  zabs = -*z__;
533 
534  //zabs = abs(*z__);
535 
536 /* |Z| > 37 */
537  /*
538  if (abs(zabs - (double).0)<1.e-5)
539  cout << " " << *z__ << endl;
540 
541  if (abs(zabs - (double)37.0)<1.e-5)
542  cout << " " << *z__ << endl;
543 
544  if (abs(zabs - (double) 7.071067811865475 )<1.e-5)
545  cout << " " << *z__ << endl;
546  */
547 
548  if (zabs > 37.) {
549  p = 0.;
550  } else {
551 /* |Z| <= 37 */
552 
553 /* Computing 2nd power */
554  d__1 = zabs;
555  expntl = exp(-(d__1 * d__1) / 2);
556 
557 /* |Z| < CUTOFF = 10/SQRT(2) */
558 
559  if (zabs < 7.071067811865475) {
560  p = expntl * ((((((zabs * .03526249659989109 + .7003830644436881)
561  * zabs + 6.37396220353165) * zabs + 33.912866078383) *
562  zabs + 112.0792914978709) * zabs + 221.2135961699311) *
563  zabs + 220.2068679123761) / (((((((zabs *
564  .08838834764831844 + 1.755667163182642) * zabs +
565  16.06417757920695) * zabs + 86.78073220294608) * zabs +
566  296.5642487796737) * zabs + 637.3336333788311) * zabs +
567  793.8265125199484) * zabs + 440.4137358247522);
568 
569 /* |Z| >= CUTOFF. */
570 
571  } else {
572  p = expntl / (zabs + 1 / (zabs + 2 / (zabs + 3 / (zabs + 4 / (
573  zabs + .65))))) / 2.506628274631001;
574  }
575  }
576  if (*z__ > 0.) {
577  p = 1 - p;
578  }
579  ret_val = p;
580 
582  return ret_val;
583 } /* mvphi_ */
#define VOID
Definition: vbivnorm.cpp:148
long int ftnlen
Definition: cbivnorm.cpp:67
#define w
int type
Definition: cbivnorm.cpp:175
char * name
Definition: cbivnorm.cpp:180
float real
Definition: cbivnorm.cpp:35
char logical1
Definition: cbivnorm.cpp:41
char * name
Definition: cbivnorm.cpp:172
int(* S_fp)()
Definition: cbivnorm.cpp:222
#define abs(x)
Definition: vbivnorm.cpp:180
shortint(* J_fp)()
Definition: cbivnorm.cpp:213
short int shortlogical
Definition: cbivnorm.cpp:40
char * addr
Definition: cbivnorm.cpp:173
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
unsigned long int uinteger
Definition: cbivnorm.cpp:32
dvariable mvbvu_(const dvariable *sh, const dvariable *sk, const dvariable *r__)
Definition: vbivnorm.cpp:298
VOID(* Z_fp)()
Definition: cbivnorm.cpp:218
integer(* I_fp)()
Definition: cbivnorm.cpp:214
doublereal E_f
Definition: cbivnorm.cpp:228
char integer1
Definition: cbivnorm.cpp:42
#define x
long int ftnint
Definition: cbivnorm.cpp:68
real(* R_fp)()
Definition: cbivnorm.cpp:215
Vardesc ** vars
Definition: cbivnorm.cpp:181
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
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
int debug_switch
Definition: vbivnorm.cpp:501
VOID C_f
Definition: cbivnorm.cpp:225
VOID(* C_fp)()
Definition: cbivnorm.cpp:217
static _THREAD gradient_structure * _instance
doublereal(*)(* E_fp)()
Definition: cbivnorm.cpp:216
long int integer
Definition: cbivnorm.cpp:31
long int logical
Definition: cbivnorm.cpp:39
VOID H_f
Definition: cbivnorm.cpp:226
VOID(* H_fp)()
Definition: cbivnorm.cpp:221
dvariable mvphi_(dvariable *)
Definition: vbivnorm.cpp:503
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
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#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
int nvars
Definition: cbivnorm.cpp:182
logical(* L_fp)()
Definition: cbivnorm.cpp:219
VOID Z_f
Definition: cbivnorm.cpp:227
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
short int shortint
Definition: cbivnorm.cpp:34
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