ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2normmix2.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 <df1b2fun.h>
12 
13 static double cc=0.39894228040143267794;
14 
15 typedef double (*pinit_f)(double y,double a);
16 
17 double nr_generic(double y,double a,pinit_f p_get_initial_x,
18  pinit_f pfun,pinit_f pdfun);
19 
24 static double cumd_normal_logistic_mixture(double x,double a)
25 {
26  // "normal" value for a is 3.0
27  double y;
28  y=0.95*cumd_norm(x)+0.05/(1.0+exp(-x/a));
29  /*
30  if (x>-20.0)
31  {
32  y=0.95*cumd_norm(x)+0.05/(1.0+exp(-x));
33  }
34  else
35  {
36  y=0.95*cumd_norm(x)+0.05*exp(x)/(1.0+exp(x));
37  }
38  */
39  return y;
40 }
41 
46 static double df_cumd_normal_logistic_mixture(double x,double a)
47 {
48  // "normal" value for a is 3.0
49  //double y=0.95*cumd_norm(x)+0.05*cumd_norm(x/a)
50  double x2=x*x;
51  double dfx;
52  dfx=cc*0.95*exp(-0.5*x2)+0.05/a*exp(-x/a)/square(1.0+exp(-x/a));
53  /*
54  if (x>-20.0)
55  {
56  dfx=cc*0.95*exp(-0.5*x2)+0.05*exp(-x)/square(1.0+exp(-x));
57  }
58  else
59  {
60  dfx=cc*0.95*exp(-0.5*x2)+0.05*exp(x)/square(1.0+exp(x));
61  }
62  */
63 
64  return dfx;
65 }
66 
71 static double cumd_normal_logistic_mixture_initx(double y,double a)
72 {
73  double x;
74  if (y>0.999)
75  {
76  x= a*inv_cumd_logistic((y-0.95)/0.05);
77  }
78  else if (y<.001)
79  {
80  x= 1.0-a*inv_cumd_logistic((.05-y)/0.05);
81  }
82  else
83  {
84  x=inv_cumd_norm(y);
85  }
86  return x;
87 }
88 
94  double a)
95 {
97  df1b2variable z;
101 
102  // the derivatives of the inverse
103  double x2=x*x;
104  double e1=exp(-0.5*x2);
105 
106  double dgx=cc*0.95*e1+0.05/a*exp(-x/a)/square(1.0+exp(-x/a));
107 
108  /*
109  double xp=x+1.e-5;
110  double xm=x-1.e-5;
111  double e1p=exp(-0.5*xp*xp);
112  double e1m=exp(-0.5*xm*xm);
113  double dgxp=cc*0.95*e1p+0.05*exp(-xp)/square(1.0+exp(-xp));
114  double dgxm=cc*0.95*e1m+0.05*exp(-xm)/square(1.0+exp(-xm));
115  */
116 
117  //cout << (dgxp-dgxm)/(2.0*1.e-5) << endl;
118  //cout << (dgxp-2.0*dgx+dgxm)/(1.e-10) << endl;
119 
120  double d2g=-cc*0.95*x*e1+0.05/(a*a)*(
121  -exp(-x/a)/square(1.0+exp(-x/a))
122  +2.0*exp(-2.0*x/a)/cube(1.0+exp(-x/a)));
123 
124  double d3g=-cc*0.95*e1 +cc*x2*0.95*e1 +0.05/(a*a*a)*(
125  exp(-x/a)/square(1.0+exp(-x/a)) -6.0*exp(-2.0*x/a)/cube(1.0+exp(-x/a))
126  +6.0*exp(-3.0*x/a)/square(square(1.0+exp(-x/a))));
127 
128  double dfx=1.0/dgx;
129  double d2f=-d2g*cube(dfx);
130  double d3f=-d3g*cube(dfx)*dfx-3.0*d2g*d2f*square(dfx);
131 
132  double * yd=yy.get_u_dot();
133  double * zd=z.get_u_dot();
134  *z.get_u()=x;
135  for (unsigned int i=0;i<df1b2variable::nvar;i++)
136  {
137  *zd++ =dfx * *yd++;
138  }
140  f1b2gradlist->write_pass1(&yy,&z,dfx,d2f,d3f);
141 
142  return z;
143 }
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
double * get_u() const
Definition: df1b2fun.h:228
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
static double cumd_normal_logistic_mixture(double x, double a)
Description not yet available.
double inv_cumd_norm(const double &x)
Description not yet available.
Definition: cumdist.cpp:78
static double cc
static double df_cumd_normal_logistic_mixture(double x, double a)
Description not yet available.
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
Description not yet available.
Definition: df1b2fun.h:266
double inv_cumd_logistic(const double &x)
Description not yet available.
Definition: ccumdlog.cpp:30
double * get_u_dot() const
Definition: df1b2fun.h:229
static int no_derivatives
Definition: df1b2fun.h:759
static double cumd_normal_logistic_mixture_initx(double y, double a)
Description not yet available.
dvariable inv_cumd_normal_logistic_mixture(const prevariable &_yy, double a)
Definition: cnorlogmix.cpp:87
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
double(* pinit_f)(double y, double a)
Definition: cnorlogmix.cpp:15
int write_pass1(const df1b2variable *px, const df1b2variable *py, df1b2variable *pz, df1b2function2 *pf)
Description not yet available.
Definition: df1b2f12.cpp:17
double nr_generic(double y, double a, pinit_f p_get_initial_x, pinit_f pfun, pinit_f pdfun)
Description not yet available.
Definition: normmix2.cpp:73
static unsigned int nvar
Definition: df1b2fun.h:290
Description not yet available.
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double cumd_norm(const double &x)
Culative normal distribution; constant objects.
Definition: cumdist.cpp:90
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16