ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lme.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 <admodel.h>
12 # include <df1b2fun.h>
13 # include <adrndeff.h>
14 
21 {
22  int mmin=variance_components_vector->indexmin();
23  int mmax=variance_components_vector->indexmax();
24  dmatrix *tmpHess=0;
25  switch(hesstype)
26  {
27  case 3:
28  cerr << "error -- Has not been impelmented" << endl;
29  ad_exit(1);
30  break;
31  case 4:
32  if (Hess_components==0)
33  {
34  int umin=Hess.indexmin();
35  int umax=Hess.indexmax();
36  tmpHess=new dmatrix(umin,umax,umin,umax);
37  Hess_components=new d3_array(mmin,mmax,umin,umax,umin,umax);
38  if (tmpHess==0 || Hess_components==0)
39  {
40  cerr << "error allocating memory" << endl;
41  ad_exit(1);
42  }
45  dvector x(1,nvar);
48  for (int i=1;i<=nvar;i++) y(i)=x(i);
49  step=get_newton_raphson_info_banded(pfmin);
50  *tmpHess=Hess;
51  }
52  break;
53  default:
54  cerr << "Illegal value for hesstype here" << endl;
55  ad_exit(1);
56  }
57 
58  dvector df0=exp(-2.0*value(*variance_components_vector));
59 
60  dvector vsave(mmin,mmax);
61  vsave=value(*variance_components_vector);
62  for(int ic=mmin;ic<=mmax;ic++)
63  {
64  (*variance_components_vector)(ic)+=0.2;
65 
66  // test newton raphson
67  switch(hesstype)
68  {
69  case 3:
70  bHess->initialize();
71  break;
72  case 4:
73  Hess.initialize();
74  break;
75  default:
76  cerr << "Illegal value for hesstype here" << endl;
77  ad_exit(1);
78  }
79 
82  dvector x(1,nvar);
85  for (int i=1;i<=nvar;i++) y(i)=x(i);
86  step=get_newton_raphson_info_banded(pfmin);
87 
88  switch(hesstype)
89  {
90  case 3:
91  cerr << "error -- Has not been impelmented" << endl;
92  ad_exit(1);
93  //(*bHess_components)(ic)= *bHess;
94  break;
95  case 4:
96  if (!tmpHess)
97  {
98  throw std::bad_alloc();
99  }
100  else
101  {
102  if (var_flag==1)
103  {
104  (*Hess_components)(ic)= (Hess-*tmpHess)/0.2;
105  }
106  else
107  {
108  double dfp=
109  exp(-2.0*value(*variance_components_vector)(ic));
110  (*Hess_components)(ic)= (Hess-*tmpHess)/(dfp-df0(ic));
111  }
112  (*variance_components_vector)(ic)-=0.2;
113  }
114  break;
115  default:
116  cerr << "Illegal value for hesstype here" << endl;
117  ad_exit(1);
118  }
119  }
120  *variance_components_vector=vsave;
121 }
122 
129 {
130  int mmin=variance_components_vector->indexmin();
131  int mmax=variance_components_vector->indexmax();
132 
134  independent_variables xx(1,mmax-mmin+1);
135  initial_params::xinit(xx); // get the initial values into the
136  dvar_vector vxx=dvar_vector(xx);
137  initial_params::reset(vxx); // get current x values into the model
138  int umin=Hess.indexmin();
139  int umax=Hess.indexmax();
140  dvar_matrix vHess(umin,umax,umin,umax);
141  vHess.initialize();
142  switch(hesstype)
143  {
144  case 3:
145  cerr << "error -- Has not been impelmented" << endl;
146  ad_exit(1);
147  break;
148  case 4:
149  {
150  for(int ic=mmin;ic<=mmax;ic++)
151  {
152  if (var_flag==1)
153  {
154  vHess+=
155  (*variance_components_vector)(ic)*((*Hess_components)(ic));
156  }
157  else
158  {
159  vHess+= exp(-2.0*(*variance_components_vector)(ic))*
160  ((*Hess_components)(ic));
161  }
162  }
163  }
164  break;
165  default:
166  cerr << "Illegal value for hesstype here" << endl;
167  ad_exit(1);
168  }
169  return vHess;
170 }
171 
172 
178  (const dvector& _x,const double& _f,function_minimizer * pfmin)
179 {
180  // for use when there is no separability
182  ADUNCONST(double,f)
183  //int i,j;
184 
187  initial_params::reset(x); // get current x values into the model
189 
191  dvector g=get_gradient_lme(pfmin);
192 
194  // this is the main loop to do inner optimization
195  //for (i=1;i<=xsize;i++) { y(i)=x(i); }
196  //for (i=1;i<=usize;i++) { y(i+xsize)=uhat(i); }
197 
198  dvar_matrix vHess=get_hessian_from_components_lme(pfmin);
199  //cout << setprecision(12) << "norm(vHess) = " << norm(value(vHess)) << endl;
200 
201  dvariable ld;
202  dvariable tmp=0.0;
203  dvariable sgn;
204 
205  dvector step=value(solve(vHess,g,tmp,sgn));
206  if (value(sgn)<=0)
207  {
208  cerr << "sgn sucks" << endl;
209  }
210  int mmin=variance_components_vector->indexmin();
211  int mmax=variance_components_vector->indexmax();
212  int nv=mmax-mmin+1;
213  dvector g1(1,nv);
214  ld=0.5*tmp;
215  gradcalc(nv,g1);
216  uhat-=step;
217 
219  double maxg=max(fabs(get_gradient_lme(uhat,pfmin)));
220  if (maxg > 1.e-12)
221  {
222  cout << "max g = " << std::scientific << setprecision(10) << maxg << endl;
223  }
224 
225  double f2=0.0;
226  dvector g2=get_gradient_lme_hp(f2,pfmin);
227  f=f2+value(ld);
228  return g1+g2;
229 }
230 
237 {
238  dvector g(1,usize);
239  dvector ub(1,usize);
240  independent_variables u(1,usize);
241  gradcalc(0,g);
242  initial_params::xinit(u); // get the initial values into the
243  uhat=u;
244 
245  dvariable pen=0.0;
246  dvariable vf=0.0;
249  pfmin->AD_uf_inner();
251 
253  vf+=pen;
254  double f = value(vf);
255  gradcalc(usize, g);
256  return g;
257 }
258 
264  (const dvector& x,function_minimizer * pfmin)
265 {
266  dvector g(1,usize);
267  dvector ub(1,usize);
268  independent_variables u(1,usize);
269  u=x;
270  gradcalc(0,g);
271 
272  dvariable pen=0.0;
273  dvariable vf=0.0;
276  pfmin->AD_uf_inner();
278 
280  vf+=pen;
281  double f = value(vf);
282  gradcalc(usize, g);
283  return g;
284 }
285 
291  (const double& _f,function_minimizer * pfmin)
292 {
293  ADUNCONST(double,f)
294  //double fb=1.e+100;
295  dvector g(1,xsize);
296  dvector ub(1,xsize);
297  independent_variables u(1,xsize);
298  //gradcalc(xsize,g);
301  initial_params::xinit(u); // get the initial values into the
302 
303  dvariable pen=0.0;
304  dvariable vf=0.0;
307  pfmin->AD_uf_inner();
309 
311  vf+=pen;
312  f=value(vf);
313  gradcalc(xsize,g);
314  return g;
315 }
static void reset_all(const dvector &)
Description not yet available.
Definition: df1b2f18.cpp:17
virtual void AD_uf_inner()
Definition: xmodelm4.cpp:40
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
dvector get_gradient_lme(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:264
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
static void xinit_all(const dvector &x)
Description not yet available.
Definition: nvarcall.cpp:31
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
Definition: fvar_ma7.cpp:48
void get_hessian_components_banded_lme(function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:20
dvar_matrix get_hessian_from_components_lme(function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:128
dvector get_gradient_lme_hp(const double &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:291
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
dvector banded_calculations_lme(const dvector &_x, const double &_f, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:178
static void restore_start_phase(void)
Definition: model.cpp:275
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
static void set_active_only_random_effects(void)
Definition: model.cpp:251
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
static int nvarcalc_all(void)
Description not yet available.
Definition: nvarcall.cpp:17
static void set_inactive_random_effects(void)
Definition: model.cpp:288
Description not yet available.
Definition: fvar.hpp:2819
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
void reset_gradient_stack(void)
Rewind buffer.
Definition: sgradclc.cpp:367
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
#define max(a, b)
Definition: cbivnorm.cpp:189
Description not yet available.
Definition: fvar.hpp:3727
int indexmin(void) const
Definition: fvar.hpp:2568
static double fun_without_pen
Definition: admodel.h:2395
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259