ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dep_hess.cpp
Go to the documentation of this file.
1 
8 #include <admodel.h>
9 
10 // estimate the matrix of second derivatives
11 
17 {
18  int nvar=initial_params::nvarcalc(); // get the number of active parameters
19  independent_variables x(1,nvar);
20  initial_params::xinit(x); // get the initial values into the x vector
21  dvector scale(1,nvar); // need to get scale from somewhere
22  dvector tscale(1,nvar); // need to get scale from somewhere
24  //check=initial_params::stddev_curvscale(curv,x);
25  double f = 0.0;
26  double delta=1.e-7;
27  dvector g1(1,nvar);
28  dvector depg(1,nvar);
29  dvector g2(1,nvar);
30  dvector gbest(1,nvar);
31  dmatrix hess(1,nvar,1,nvar);
32  dvector hess1(1,nvar);
33  dvector hess2(1,nvar);
34  double eps=.1;
36  gbest.fill_seqadd(1.e+50,0.);
37  {
38  gradcalc(0,depg);
39  dvariable vf=0.0;
41  userfunction();
42  vf=dep;
43  f = value(vf);
44  gradcalc(nvar, depg);
45  }
46  std::ostream& output_stream = get_output_stream();
47  double sdelta1;
48  double sdelta2;
49  int i;
50  for (i=1;i<=nvar;i++)
51  {
52  output_stream << "Estimating row " << i << " out of " << nvar
53  << " for dependent variable hessian" << endl;
54 
55  double xsave=x(i);
56  sdelta1=x(i)+delta;
57  sdelta1-=x(i);
58  x(i)=xsave+sdelta1;
59  dvariable vf=0.0;
61  gradcalc(0,depg);
63  userfunction();
64  vf=dep;
65  f = value(vf);
66  gradcalc(nvar, g1);
67  g1=elem_div(g1,tscale);
68 
69  sdelta2=x(i)-delta;
70  sdelta2-=x(i);
71  x(i)=xsave+sdelta2;
73  //gradcalc(nvar,depg);
74  vf=0.0;
76  userfunction();
77  vf=dep;
78  f = value(vf);
79  gradcalc(nvar, g2);
80  g2=elem_div(g2,tscale);
81  x(i)=xsave;
82  hess1=(g1-g2)/(sdelta1-sdelta2)/scale(i);
83 
84  sdelta1=x(i)+eps*delta;
85  sdelta1-=x(i);
86  x(i)=xsave+sdelta1;
88  //gradcalc(nvar,depg);
89  vf=0.0;
91  userfunction();
92  vf=dep;
93  f = value(vf);
94  gradcalc(nvar, g1);
95  g1=elem_div(g1,tscale);
96 
97  x(i)=xsave-eps*delta;
98  sdelta2=x(i)-eps*delta;
99  sdelta2-=x(i);
100  x(i)=xsave+sdelta2;
102  //gradcalc(nvar,depg);
103  vf=0.0;
105  userfunction();
106  vf=dep;
107  f = value(vf);
108  gradcalc(nvar, g2);
109  g2=elem_div(g2,tscale);
110  x(i)=xsave;
111 
112  hess2=(g1-g2)/(sdelta1-sdelta2)/scale(i);
113  double eps2=eps*eps;
114  hess(i)=(eps2*hess1-hess2) /(eps2-1.);
115  }
116  /*
117  for (i=1;i<=nvar;i++)
118  {
119  hess(i,i)/=(scale(i)*scale(i));
120  for (int j=1;j<i;j++)
121  {
122  double tmp=(hess(i,j)+hess(j,i))/(2.*scale(i)*scale(j));
123  hess(i,j)=tmp;
124  hess(j,i)=tmp;
125  }
126  hess(i,i)-=depg(i)*curv(i)/(scale(i)*scale(i)*scale(i));
127  }
128  */
129  return hess;
130 }
#define x
Vector of double precision numbers.
Definition: dvector.h:50
void fill_seqadd(double, double)
Fills dvector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:58
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
static int nvarcalc()
Definition: model.cpp:152
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
dvector * gbest
Definition: fvar.hpp:3656
Description not yet available.
Definition: fvar.hpp:2819
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
double eps
Definition: ftweak.cpp:13
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
dvector * g2
Definition: fvar.hpp:3651
virtual void userfunction(void)=0
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
dmatrix dep_hess_routine(const dvariable &dep)
Description not yet available.
Definition: dep_hess.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518