ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
normmix2.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 typedef double (*pinit_f)(double y,double a);
14 
15 /*
16 static double cc=0.39894228040143267794;
17 static double cumd_normal_logistic_mixture(double x,double a)
18 {
19  // "normal" value for a is 3.0
20  double y;
21  if (x>-20.0)
22  {
23  y=0.95*cumd_norm(x)+0.05/(1.0+exp(-x/a));
24  }
25  else
26  {
27  y=0.95*cumd_norm(x)+0.05/(1.0+exp(-x/a));
28  }
29  return y;
30 }
31 
32 static double df_cumd_normal_logistic_mixture(double x,double a)
33 {
34  // "normal" value for a is 3.0
35  //double y=0.95*cumd_norm(x)+0.05*cumd_norm(x/a)
36  double x2=x*x;
37  double dfx;
38  if (x>-20.0)
39  {
40  dfx=cc*0.95*exp(-0.5*x2)+0.05/a*exp(-x/a)/square(1.0+exp(-x/a));
41  }
42  else
43  {
44  dfx=cc*0.95*exp(-0.5*x2)+0.05/a*exp(-x/a)/square(1.0+exp(-x/a));
45  }
46 
47  return dfx;
48 }
49 
50 static double cumd_normal_logistic_mixture_initx(double y,double a)
51 {
52  double x;
53  if (y>0.999)
54  {
55  x= a*inv_cumd_logistic((y-0.95)/0.05);
56  }
57  else if (y<.001)
58  {
59  x= 1.0-a*inv_cumd_logistic((.05-y)/0.05);
60  }
61  else
62  {
63  x=inv_cumd_norm(y);
64  }
65  return x;
66 }
67 */
68 
73 double nr_generic(double y,double a,pinit_f p_get_initial_x,
74  pinit_f pfun,pinit_f pdfun)
75 {
76  double x=(*p_get_initial_x)(y,a);
77 
78  const int imax=15;
79  int icount=0;
80  double h;
81  do
82  {
83  icount++;
84  double cy=(*pfun)(x,a);
85  double der=(*pdfun)(x,a);
86  double diff=y-cy;
87  h=diff/der;
88  x+=h;
89  if (fabs(h)<1.e-12) break;
90  }
91  while(icount<imax);
92  //cout << " x = " << x << " icount = " << icount << endl;
93  if (fabs(h)>1.e-8)
94  {
95  cerr << "shit" << endl;
96  }
97 
98  return x;
99 }
100 
101 /*
102 main()
103 {
104  gradient_structure gs(10000);
105  dvariable y;
106  cin >> y;
107  dvariable x=inv_cumd_normal_logistic_mixture(y,3.0);
108 }
109 */
#define x
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
prnstream & endl(prnstream &)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double(* pinit_f)(double y, double a)
Definition: cnorlogmix.cpp:15
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