ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mod_rhes.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 <sstream>
12 using std::istringstream;
13 
14 #include <admodel.h>
15 #include <df1b2fun.h>
16 #include <adrndeff.h>
17 
18 #ifndef OPT_LIB
19  #include <cassert>
20  #include <climits>
21 #endif
22 
24  uostream& ofs1,ofstream& ofs,int usize,int xsize,dvector& u);
25 
27  const std::chrono::time_point<std::chrono::system_clock>& from,
28  const std::chrono::time_point<std::chrono::system_clock>& to);
29 
35  const banded_symmetric_dmatrix& _M, int& ierr)
36 {
38  int minsave=M.indexmin();
39  M.shift(1);
40  int n=M.indexmax();
41 
42  int bw=M.bandwidth();
44 #ifndef SAFE_INITIALIZE
45  L.initialize();
46 #endif
47 
48  int i,j,k;
49  double tmp;
50  if (M(1,1)<=0)
51  {
52  ierr=1;
53  return L;
54  }
55  L(1,1)=sqrt(M(1,1));
56  for (i=2;i<=bw;i++)
57  {
58  L(i,1)=M(i,1)/L(1,1);
59  }
60 
61  for (i=2;i<=n;i++)
62  {
63  for (j=i-bw+1;j<=i-1;j++)
64  {
65  if (j>1)
66  {
67  tmp=M(i,j);
68  for (k=i-bw+1;k<=j-1;k++)
69  {
70  if (k>0 && k>j-bw)
71  tmp-=L(i,k)*L(j,k);
72  }
73  L(i,j)=tmp/L(j,j);
74  }
75  }
76  tmp=M(i,i);
77  for (k=i-bw+1;k<=i-1;k++)
78  {
79  if (k>0)
80  tmp-=L(i,k)*L(i,k);
81  }
82  if (tmp<=0)
83  {
84  ierr=1;
85  return L;
86  }
87  L(i,i)=sqrt(tmp);
88  }
89  M.shift(minsave);
90  L.shift(minsave);
91 
92  return L;
93 }
94 
100 {
101 #if defined(USE_ADPVM)
103  {
104  switch (ad_comm::pvm_manager->mode)
105  {
106  case 1: //master
108  break;
109  case 2: //slave
111  break;
112  default:
113  cerr << "Illegal value for mode" << endl;
114  ad_exit(1);
115  }
116  }
117  else
118 #endif
119  {
121  }
122 }
123 dvector get_solution_vector(int npts);
124 
130 {
131  std::ostream& output_stream = get_output_stream();
132 
133 #ifdef DEBUG
134  int debug = 0;
135 #endif
136 
137  // get the number of active parameters
138  int nvar = initial_params::nvarcalc();
139 
140 #ifdef DEBUG
141  if (debug) cout<<endl<<"Starting hess_routine_noparallel_random_effects(). nvar = "<<nvar<<endl;
142 #endif
143 
144  //if (adjm_ptr) set_labels_for_hess(nvar);
145  independent_variables x(1,nvar);
146  initial_params::xinit(x); // get the initial values into the x vector
147 // dvector mle(1,nvar);
148 // mle = value(x); //x should be the mle. save for later.
149 // if (debug) cout<<"mle = "<<mle<<endl;
150  double delta=1.e-4;
151  dvector g1(1,nvar);
152  dvector g0(1,nvar);
153  dvector g2(1,nvar);
154  dvector gbest(1,nvar);
155  //dvector hess(1,nvar);
156  dvector hess1(1,nvar);
157  dvector hess2(1,nvar);
158  //double eps=.1;
160  gbest.fill_seqadd(1.e+50,0.);
161 
162  dvector ddd(1,nvar);
163  gradcalc(0,ddd);
164 
165  {
167  {
168  double f = 0.0;
169  g1=(*lapprox)(x,f,this);
170  g0=g1;
171  }
172  // modify so thaqt we have l_uu and dux for delta method
173  // DF feb 15 05
174  //if (lapprox->hesstype==2 || lapprox->hesstype==3)
175  if (lapprox->hesstype==2 )
176  {
178  {
179  //if (ad_comm::wd_flag)
180  adstring tmpstring = ad_comm::adprogram_name + ".rhes";
181  ofstream ofs((char*)(tmpstring));
182  ofs << " value std.dev" << endl;
185  int i,j;
186  int ii=1;
187  dvector & u= lapprox->uhat;
188  for (i=mmin;i<=mmax;i++)
189  {
191  {
193  dvector d=sqrt(diagonal(m));
194  int jmin=d.indexmin();
195  int jmax=d.indexmax();
196  for (j=jmin;j<=jmax;j++)
197  {
198  //if (ii<=u.indexmax())
199  {
200  ofs << setprecision(5) << setscientific()
201  << setw(14) << u(ii++) << " " << d(j) << endl;;
202  }
203  }
204  }
205  }
206  }
207  else if (lapprox->bHess)
208  {
209  //if (ad_comm::wd_flag)
210  adstring tmpstring = ad_comm::adprogram_name + ".rhes";
211  ofstream ofs((char*)(tmpstring));
212  ofs << " value std.dev" << endl;
213  int mmin=lapprox->bHess->indexmin();
214  int mmax=lapprox->bHess->indexmax();
215  //int i,j;
216  int i;
217  //int ii=1;
218  dvector & u= lapprox->uhat;
219  dvector e(mmin,mmax);
220  //choleski_decomp(*lapprox->bHess);
221  int ierr;
222 
224  ierr);
225  e.initialize();
226  for (i=mmin;i<=mmax;i++)
227  {
228  e(i)=1.0;
229  dvector v=solve(tmp,e);
230  e(i)=0;
231 
232  double d=sqrt(v*v);
233  ofs << setprecision(5) << setscientific()
234  << setw(14) << u(i) << " " << d << endl;;
235  }
236  }
237  }
238  else
239  {
240  //if (ad_comm::wd_flag)
241  dmatrix m;
242  adstring tmpstring = ad_comm::adprogram_name + ".rhes";
243  ofstream ofs((char*)(tmpstring));
244  ofs << " value std.dev" << endl;
245  //int ii=1;
246  tmpstring = ad_comm::adprogram_name + ".luu";
247  uostream ofs1((char*)(tmpstring));
248  dvector & u= lapprox->uhat;
249  if (lapprox->hesstype !=3)
250  {
251  if (allocated(lapprox->Hess))
252  {
253  m= inv(lapprox->Hess);
254  int mmin=m.indexmin();
255  int mmax=m.indexmax();
256  for (int i=mmin;i<=mmax;i++)
257  {
258  ofs << setprecision(5) << setscientific()
259  << setw(14) << u(i) << " " << sqrt(m(i,i)) << endl;;
260  }
261  // save l_uu and l_xu for covariance calculations
262  ofs1 << lapprox->usize << lapprox->xsize;
263  ofs1 << m;
264  }
265  else if (lapprox->sparse_triplet2)
266  {
270  lapprox->xsize,u);
271  // save l_uu and l_xu for covariance calculations
272  }
273  }
274  else
275  {
276  if (lapprox->bHess)
277  {
278  int ierr=0;
279  int mmin=lapprox->bHess->indexmin();
280  int mmax=lapprox->bHess->indexmax();
283  ivector e(mmin,mmax);
284  e.initialize();
285  if (ierr==0)
286  {
287  ofs1 << lapprox->usize << lapprox->xsize;
288  for (int i=mmin;i<=mmax;i++)
289  {
290  if (i>1) e(i-1)=0;
291  e(i)=1;
292  dvector w=solve_trans(C,solve(C,e));
293  ofs << setprecision(5) << setscientific()
294  << setw(14) << u(i) << " " << sqrt(w(i)) << endl;;
295  ofs1 << w;
296  }
297  }
298  else
299  {
300  }
301  }
302  }
303  if (!ofs)
304  {
305  cerr << "Error writing to file " << tmpstring << endl;
306  ad_exit(1);
307  }
308  // save l_uu and l_xu for covariance calculations
309  ofs1 << lapprox->Dux;
310  if (!ofs1)
311  {
312  cerr << "Error writing to file " << tmpstring << endl;
313  ad_exit(1);
314  }
315  ofs1.close();
316  }
317 
318  {
319  adstring tmpstring = ad_comm::adprogram_name + ".luu";
320  uistream uis1((char*)(tmpstring));
321  int i = 0, j = 0;
322  uis1 >> i >> j;
323  output_stream << i << " " << j << endl;
324  }
325 
326  int npts=2;
327  int on,nopt = 0;
328  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-hpts",nopt))>-1)
329  {
330  if (nopt !=1)
331  {
332  cerr << "Usage -hpts option needs non-negative integer -- ignored.\n";
333  }
334  else
335  {
336  npts=atoi(ad_comm::argv[on+1]);
337  }
338  }
339 
340  double _delta=0.0;
341 
342  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-hsize",nopt))>-1)
343  {
344  if (!nopt)
345  {
346  cerr << "Usage -hsize option needs number -- ignored" << endl;
347  }
348  else
349  {
350  istringstream ist(ad_comm::argv[on+1]);
351  ist >> _delta;
352 
353  if (_delta<=0)
354  {
355  cerr << "Usage -hsize option needs positive number -- ignored.\n";
356  _delta=0.0;
357  }
358  }
359  if (_delta>0)
360  {
361  delta=_delta;
362  }
363  }
364 
365  // get a number which is exactly representable
366  double sdelta=1.0+delta;
367  sdelta-=1.0;
368 
369  std::chrono::time_point<std::chrono::system_clock> from_start;
371  {
372  from_start = std::chrono::system_clock::now();
373 
374  cout << "Calculating Hessian";
375  if (nvar >= 10) cout << " (" << nvar << " variables)";
376  cout << ": ";
377  cout.flush();
378  }
379 
381  const int num = nvar / defaults::percentage_number;
382  int index = num + (nvar % 5);
383  {
384  //
385  uostream uos("hessian.bin");
386  uos << npts;
387  for (int i=1;i<=nvar;i++)
388  {
389  output_stream << "Estimating row " << i << " out of " << nvar
390  << " for hessian" << endl;
391 
393  {
394  if (nvar >= 10)
395  {
396  if (i == index)
397  {
398  if (percentage > defaults::percentage) cout << ", ";
399 
400  cout << percentage << "%";
401  percentage += 20;
402  index += num;
403  }
404  }
405  else
406  {
407  if (i > 1) cout << ", ";
408  cout << i;
409  }
410  cout.flush();
411  }
412 
413  for (int j=-npts;j<=npts;j++)
414  {
415  if (j !=0)
416  {
417  double f=0.0;
418  double xsave=x(i);
419  x(i)=xsave+j*sdelta;
420  g1=(*lapprox)(x,f,this);
421  x(i)=xsave;
422  uos << i << j << sdelta << g1;
423  }
424  else
425  {
426  uos << i << j << sdelta << g0;
427  }
428  }
429  }
431  {
432  print_elapsed_time(from_start, std::chrono::system_clock::now());
433  }
434  }
435  // check for accuracy
436  {
437  uistream uis("hessian.bin");
438  uis >> npts;
440  v.shift(-npts);
441  dmatrix tmp(-npts,npts,1,nvar);
442  dmatrix hess(1,nvar,1,nvar);
443  ivector iind(-npts,npts);
444  ivector jind(-npts,npts);
445  double sd = 0;
446  int i;
447  for (i=1;i<=nvar;i++)
448  {
449  for (int j=-npts;j<=npts;j++)
450  {
451  uis >> iind(j) >> jind(j) >> sd >> tmp(j);
452  }
453  hess(i)=(v*tmp).shift(1);
454  hess(i)/=sd;
455  }
456  {
457  adstring tmpstring="admodel.hes";
458  if (ad_comm::wd_flag)
459  {
460  tmpstring = ad_comm::adprogram_name + ".hes";
461  }
462  uostream ofs((char*)tmpstring);
463  ofs << nvar; //writing to admodel.hes
464  dmatrix shess(1,nvar,1,nvar);
465  double maxerr=0.0;
466  for (i=1;i<=nvar;i++)
467  {
468  for (int j=1;j<=nvar;j++)
469  {
470  shess(i,j)=(hess(i,j)-hess(j,i))/
471  (1.e-3+sfabs(hess(i,j))+fabs(hess(j,i)));
472  if (shess(i,j)>maxerr) maxerr=shess(i,j);
473  }
474  }
475  ofstream ofs1("hesscheck");
476  ofs1 << "maxerr = " << maxerr << endl << endl;
477  ofs1 << setw(10) << hess << endl << endl;
478  ofs1 << setw(10) << shess << endl;
479  ofs << hess;
482  dvector tscale(1,nvar); // need to get scale from somewhere
483  /*int check=*/initial_params::stddev_scale(tscale,x);
484  ofs << tscale;
485  ofs << -987;
486  dvector mle(1,nvar);
488  ofs << mle;
489 #ifdef DEBUG
490  if (debug) {
491  cout<<"admodel.hes:"<<endl;
492  cout<<nvar<<endl;
493  cout<<hess<<endl;
494  cout<<gradient_structure::Hybrid_bounded_flag<<endl;
495  cout<<tscale<<endl;
496  cout<<-987<<endl;
497  cout<<mle<<endl;
498  cout<<"end of hess_routine_noparallel_random_effects()"<<endl<<endl;
499  }
500 #endif
501  }
502  }
503  /*
504  first_hessian_flag=0;
505  double sdelta1;
506  double sdelta2;
507  lapprox->fmc1.fringe=1.e-9;
508  for (int i=1;i<=nvar;i++)
509  {
510  hess_calcreport(i,nvar);
511 
512  double f=0.0;
513  double xsave=x(i);
514  sdelta1=x(i)+delta;
515  sdelta1-=x(i);
516  x(i)=xsave+sdelta1;
517  g1=(*lapprox)(x,f,this);
518 
519  sdelta2=x(i)-delta;
520  sdelta2-=x(i);
521  x(i)=xsave+sdelta2;
522  g2=(*lapprox)(x,f,this);
523  x(i)=xsave;
524  hess1=(g1-g2)/(sdelta1-sdelta2);
525 
526  sdelta1=x(i)+eps*delta;
527  sdelta1-=x(i);
528  x(i)=xsave+sdelta1;
529  g1=(*lapprox)(x,f,this);
530 
531  x(i)=xsave-eps*delta;
532  sdelta2=x(i)-eps*delta;
533  sdelta2-=x(i);
534  x(i)=xsave+sdelta2;
535  g2=(*lapprox)(x,f,this);
536  x(i)=xsave;
537 
538  initial_params::set_inactive_only_random_effects();
539  initial_params::reset(dvar_vector(x));
540  double eps2=eps*eps;
541  hess2=(g1-g2)/(sdelta1-sdelta2);
542  hess=(eps2*hess1-hess2) /(eps2-1.);
543 
544  ofs << hess;
545  }
546  */
547  }
548 }
549 
550 #if defined(USE_ADPVM)
551 
557 {
558  int nvar=initial_params::nvarcalc(); // get the number of active parameters
559  //if (adjm_ptr) set_labels_for_hess(nvar);
560  independent_variables x(1,nvar);
561  initial_params::xinit(x); // get the initial values into the x vector
562  double delta=1.e-6;
563  double eps=.1;
565 
566  dvector ddd(1,nvar);
567  gradcalc(0,ddd);
568  {
569  {
570  (*lapprox)(x,f,this);
571  }
572  double sdelta1;
573  double sdelta2;
574  lapprox->fmc1.fringe=1.e-9;
575  for (int i=1;i<=nvar;i++)
576  {
577  double f=0.0;
578  double xsave=x(i);
579  sdelta1=x(i)+delta;
580  sdelta1-=x(i);
581  x(i)=xsave+sdelta1;
582  (*lapprox)(x,f,this);
583 
584  sdelta2=x(i)-delta;
585  sdelta2-=x(i);
586  x(i)=xsave+sdelta2;
587  (*lapprox)(x,f,this);
588  x(i)=xsave;
589 
590  sdelta1=x(i)+eps*delta;
591  sdelta1-=x(i);
592  x(i)=xsave+sdelta1;
593  (*lapprox)(x,f,this);
594 
595  x(i)=xsave-eps*delta;
596  sdelta2=x(i)-eps*delta;
597  sdelta2-=x(i);
598  x(i)=xsave+sdelta2;
599  (*lapprox)(x,f,this);
600  x(i)=xsave;
601 
604  }
605  }
606 }
607 #endif // #if defined(USE_ADPVM)
608 
614 {
615  int i;
616  int n1=2*n+1;
617  dmatrix tmp(1,n1,1,n1);
618  dvector v(1,n1);
619  v.initialize();
620  v(2)=1.0;
621  dvector c(1,n1);
622  c.fill_seqadd(-n,1);
623  tmp.initialize();
624  tmp(1)=1;
625  tmp(2)=c;
626  for (i=3;i<=n1;i++)
627  {
628  tmp(i)=elem_prod(tmp(i-1),c);
629  }
630  dmatrix tmp1=inv(tmp);
631  return tmp1*v;
632 }
Description not yet available.
Definition: fvar.hpp:7981
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
dcompressed_triplet * sparse_triplet2
Definition: adrndeff.h:271
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
void hess_routine_slave_random_effects(void)
Description not yet available.
Definition: mod_rhes.cpp:556
const int percentage
Definition: fvar.hpp:9506
Description not yet available.
dvector get_solution_vector(int npts)
Description not yet available.
Definition: mod_rhes.cpp:613
dvector diagonal(const dmatrix &matrix)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat31.cpp:12
int indexmax() const
Definition: fvar.hpp:3822
#define x
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
#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
constexpr const int percentage_number
Definition: fvar.hpp:9507
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
int indexmin() const
Definition: fvar.hpp:2917
static char ** argv
Definition: fvar.hpp:8866
void fill_seqadd(double, double)
Fills dvector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:58
double fringe
Definition: fvar.hpp:3185
void print_elapsed_time(const std::chrono::time_point< std::chrono::system_clock > &from, const std::chrono::time_point< std::chrono::system_clock > &to)
Definition: mod_sd.cpp:94
int indexmax(void) const
Definition: fvar.hpp:7999
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:29
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
static adstring adprogram_name
Definition: fvar.hpp:8860
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
static int debug
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
static int nvarcalc()
Definition: model.cpp:152
banded_symmetric_dmatrix * bHess
Definition: adrndeff.h:265
double sfabs(const double v1)
Description not yet available.
Definition: dvect14.cpp:20
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
dvector solve_trans(const lower_triangular_dmatrix &M, const dvector &y)
Description not yet available.
Definition: dmat36.cpp:80
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
banded_lower_triangular_dmatrix quiet_choleski_decomp(const banded_symmetric_dmatrix &_M, int &ierr)
Description not yet available.
Definition: mod_rhes.cpp:34
Description not yet available.
Definition: fvar.hpp:3398
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
Description not yet available.
Definition: fvar.hpp:8120
#define M
Definition: rngen.cpp:57
Description not yet available.
Definition: fvar.hpp:9345
Description not yet available.
Definition: fvar.hpp:2819
void shift(int)
Description not yet available.
Definition: dmat28.cpp:125
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
static void copy_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:9
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
int indexmax() const
Definition: fvar.hpp:2921
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
void hess_routine_noparallel_random_effects(void)
Description not yet available.
Definition: mod_rhes.cpp:129
static int Hybrid_bounded_flag
Description not yet available.
Definition: fvar.hpp:9410
#define w
double eps
Definition: ftweak.cpp:13
int indexmin(void) const
Definition: fvar.hpp:7995
Description not yet available.
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
void get_inverse_sparse_hessian(dcompressed_triplet &st, hs_symbolic &S, uostream &ofs1, ofstream &ofs, int usize, int xsize, dvector &u)
Definition: hs_sparse.cpp:3575
int indexmin() const
Definition: fvar.hpp:3818
Description not yet available.
Definition: fvar.hpp:3516
static int output_flag
Definition: admodel.h:1972
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
void hess_routine_random_effects(void)
Description not yet available.
Definition: mod_rhes.cpp:99
static unsigned int wd_flag
Definition: fvar.hpp:8864
static int first_hessian_flag
Definition: admodel.h:1855
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259