ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cumdist.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 
13 double inv_cumd_norm(const double& x);
14 double cumd_norm(const double& x);
15 
20 double normal_tail_right(const double& x)
21 {
22  const double a3=5;
23  const double a4=9;
24  const double a5=129;
25  double x2=x*x;
26  double z=0.3989422804*exp(-.5*x2);
27  double b1=x2+2;
28  double b2=b1*(x2+4);
29  double b3=b2*(x2+6);
30  double b4=b3*(x2+8);
31  double b5=b4*(x2+10);
32  double tmp=z/x*(1.0 -1.0/b1 +1.0/b2 - a3/b3 +a4/b4 -a5/b5);
33  return tmp;
34 }
35 
40 double inv_cumd_norm_inner(double x)
41 {
42  const double c0=2.515517;
43  const double c1=0.802853;
44  const double c2=0.010328;
45  const double d1=1.432788;
46  const double d2=0.189269;
47  const double d3=0.001308;
48  if (x<=0 || x>=1.0)
49  {
50  cerr << "Illegal argument to inv_cumd_norm = " << x << endl;
51  return 0;
52  }
53 
54  if (x<0.5)
55  {
56  double t = sqrt(-2.*log(x));
57  double p=((c2*t+c1)*t+c0)/((((d3*t+d2)*t+d1)*t)+1)-t;
58  return p;
59  }
60  else if (x==0.5)
61  {
62  return 0.0;
63  }
64  else
65  {
66  double y=1.-x;
67  //double t = sqrt(-log(y*y));
68  double t = sqrt(-2.*log(y));
69  double p=t-((c2*t+c1)*t+c0)/((((d3*t+d2)*t+d1)*t)+1);
70  return p;
71  }
72 }
73 
78 double inv_cumd_norm(const double& x)
79 {
80  double y=inv_cumd_norm_inner(x);
81  y+=2.50662827*exp(.5*y*y)*(x-cumd_norm(y));
82  return y;
83 }
84 
90 double cumd_norm(const double& x)
91 {
92  const double b1=0.319381530;
93  const double b2=-0.356563782;
94  const double b3=1.781477937;
95  const double b4=-1.821255978;
96  const double b5=1.330274429;
97  const double p=.2316419;
98  if (x>=0)
99  {
100  double u=1./(1+p*x);
101  double y= ((((b5*u+b4)*u+b3)*u+b2)*u+b1)*u;
102  double z=1.0-0.3989422804*exp(-.5*x*x)*y;
103  return z;
104  }
105  else
106  {
107  double w=-x;
108  double u=1./(1+p*w);
109  double y= ((((b5*u+b4)*u+b3)*u+b2)*u+b1)*u;
110  double z=0.3989422804*exp(-.5*x*x)*y;
111  return z;
112  }
113 }
114 
119 double bounded_cumd_norm(const double x,double beta)
120 {
121  const double b1=0.319381530;
122  const double b2=-0.356563782;
123  const double b3=1.781477937;
124  const double b4=-1.821255978;
125  const double b5=1.330274429;
126  const double p=.2316419;
127  if (x>=0)
128  {
129  double u=1./(1+p*x);
130  double y= ((((b5*u+b4)*u+b3)*u+b2)*u+b1)*u;
131  double z=1.0-0.3989422804*exp(-.5*x*x)*y;
132  z=beta*(z-0.5)+0.5;
133  return z;
134  }
135  else
136  {
137  double w=-x;
138  double u=1./(1+p*w);
139  double y= ((((b5*u+b4)*u+b3)*u+b2)*u+b1)*u;
140  double z=0.3989422804*exp(-.5*x*x)*y;
141  z=beta*(z-0.5)+0.5;
142  return z;
143  }
144 }
double bounded_cumd_norm(const double x, double beta)
Description not yet available.
Definition: cumdist.cpp:119
#define x
double inv_cumd_norm(const double &x)
Description not yet available.
Definition: cumdist.cpp:78
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
#define w
double inv_cumd_norm_inner(double x)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: cumdist.cpp:40
double cumd_norm(const double &x)
Culative normal distribution; constant objects.
Definition: cumdist.cpp:90
double normal_tail_right(const double &x)
Description not yet available.
Definition: cumdist.cpp:20
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13