ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2im3.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 #ifndef OPT_LIB
15  #include <cassert>
16  #include <climits>
17 #endif
18 
24  const dvector& u0,const dmatrix& Hess,const dvector& _xadjoint,
25  const dvector& _uadjoint,const dmatrix& _Hessadjoint,
26  function_minimizer * pmin)
27 {
28  ADUNCONST(dvector,xadjoint)
29  ADUNCONST(dvector,uadjoint)
30  //ADUNCONST(dmatrix,Hessadjoint)
31 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
32  const int xs = [](unsigned int size)->int
33  {
34  assert(size <= INT_MAX);
35  return static_cast<int>(size);
36  }(x.size());
37  const int us = [](unsigned int size)->int
38  {
39  assert(size <= INT_MAX);
40  return static_cast<int>(size);
41  }(u0.size());
42 #else
43  const int xs = static_cast<int>(x.size());
44  const int us = static_cast<int>(u0.size());
45 #endif
47  int nsc=pmin->lapprox->num_separable_calls;
48  const ivector lrea = (*pmin->lapprox->num_local_re_array)(1,nsc);
49  int hroom = int(sum(square(lrea)));
50  int nvar = xs + us + hroom;
51  independent_variables y(1,nvar);
52 
53  // need to set random effects active together with whatever
54  // init parameters should be active in this phase
57  /*int onvar=*/initial_params::nvarcalc();
58  initial_params::xinit(y); // get the initial values into the
59  // do we need this next line?
60  y(1,xs)=x;
61 
62  // contribution for quadratic prior
64  {
65  //Hess+=quadratic_prior::get_cHessian_contribution();
66  int & vxs = (int&)(xs);
68  }
69  // Here need hooks for sparse matrix structures
70 
71  dvar3_array & block_diagonal_vhessian=
73  block_diagonal_vhessian.initialize();
74  dvar3_array& block_diagonal_ch=
76  //dvar3_array(*pmin->lapprox->block_diagonal_ch);
77  int ii=xs+us+1;
79  for (int ic=1;ic<=nsc;ic++)
80  {
81  int lus=lrea(ic);
82  for (int i=1;i<=lus;i++)
83  for (int j=1;j<=lus;j++)
84  y(ii++)=bdH(ic)(i,j);
85  }
86 
87  dvector g(1,nvar);
88  gradcalc(0,g);
91  //initial_params::stddev_vscale(d,vy);
92  ii=xs+us+1;
94  {
95  cerr << "can't do importance sampling with bounded random effects"
96  " at present" << endl;
97  ad_exit(1);
98  }
99  else
100  {
101  for (int ic=1;ic<=nsc;ic++)
102  {
103  int lus=lrea(ic);
104  for (int i=1;i<=lus;i++)
105  {
106  for (int j=1;j<=lus;j++)
107  {
108  block_diagonal_vhessian(ic,i,j)=vy(ii++);
109  }
110  }
111  block_diagonal_ch(ic)=
112  choleski_decomp(inv(block_diagonal_vhessian(ic)));
113  }
114  }
115 
116  int nsamp=pmin->lapprox->num_importance_samples;
117 
118  dvariable vf=0.0;
119 
120  dvar_vector sample_value(1,nsamp);
121  sample_value.initialize();
122 
123  dvar_vector tau(1,us);;
124  for (int is=1;is<=nsamp;is++)
125  {
126  int offset=0;
128  for (int ic=1;ic<=nsc;ic++)
129  {
130  int lus=lrea(ic);
131  tau(offset+1,offset+lus).shift(1)=block_diagonal_ch(ic)*
132  pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
133  offset+=lus;
134  }
135 
136  // have to reorder the terms to match the block diagonal hessian
137  imatrix & ls=*(pmin->lapprox->block_diagonal_re_list);
138  int mmin=ls.indexmin();
139  int mmax=ls.indexmax();
140 
141  ii=1;
142  for (int i=mmin;i<=mmax;i++)
143  {
144  int cmin=ls(i).indexmin();
145  int cmax=ls(i).indexmax();
146  for (int j=cmin;j<=cmax;j++)
147  {
148  vy(ls(i,j))+=tau(ii++);
149  }
150  }
151  if (ii-1 != us)
152  {
153  cerr << "error in interface" << endl;
154  ad_exit(1);
155  }
156  initial_params::reset(vy); // get the values into the model
157  ii=1;
158  for (int i=mmin;i<=mmax;i++)
159  {
160  int cmin=ls(i).indexmin();
161  int cmax=ls(i).indexmax();
162  for (int j=cmin;j<=cmax;j++)
163  {
164  vy(ls(i,j))-=tau(ii++);
165  }
166  }
167 
169  //int istop=0;
170  //if (is==65) { }
171  pmin->AD_uf_outer();
172 
173  if (pmin->lapprox->use_outliers==0)
174  {
175  double neps=0.5*norm2(pmin->lapprox->epsilon(is));
176 
177  (*pmin->lapprox->importance_sampling_values)(is)=
179 
180  (*pmin->lapprox->importance_sampling_weights)(is)=neps;
181 
182  sample_value(is)=*objective_function_value::pobjfun
183  -neps;
184  }
185  else
186  {
187  dvector& e=pmin->lapprox->epsilon(is);
188  double neps=-sum(log(.95*exp(-0.5*square(e))+
189  0.0166666667*exp(-square(e)/18.0)));
190 
191  (*pmin->lapprox->importance_sampling_values)(is)=
193 
194  sample_value(is)=*objective_function_value::pobjfun
195  -neps;
196  }
197  }
198 
199  nsc=pmin->lapprox->num_separable_calls;
200  dmatrix weights(1,nsc,1,nsamp);
201  for (int is=1;is<=nsamp;is++)
202  {
203  int offset=0;
204  for (int ic=1;ic<=nsc;ic++)
205  {
206  int lus=lrea(ic);
207  dvector e= pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
208  offset+=lus;
209  if (pmin->lapprox->use_outliers==0)
210  {
211  weights(ic,is)=0.5*norm2(e);
212  }
213  else
214  {
215  weights(ic,is)=-sum(log(.95*exp(-0.5*square(e))+
216  0.0166666667*exp(-square(e)/18.0)));
217  }
218  }
219  }
220  dvar_vector lcomp(1,nsc);
221  for (int isc=1;isc<=nsc;isc++)
222  {
223  dvar_vector & comps=
225 
226  dvar_vector diff=comps-weights(isc);
227  double dmin=min(value(diff));
228  lcomp(isc)=-dmin+log(mean(exp(-diff+dmin)));
229 
230  for (int is=1;is<=nsamp;is++)
231  {
232  }
233  }
234 
236  print_importance_sampling_weights_flag==1)
237  {
238  double min_vf=min(value(sample_value));
239  dvector tmp=exp(value(sample_value)-min_vf);
240  cout << "The unsorted normalized importance samplng weights are " << endl
241  << tmp << endl;
242  cout << "The sorted normalized importance samplng weights are " << endl
243  << sort(tmp) << endl;
244  ad_exit(1);
245  }
246 
247 
248  double min_vf=min(value(sample_value));
249  vf=min_vf-log(mean(exp(min_vf-sample_value)));
250  vf-=us*0.91893853320467241;
251 
252  int sgn=0;
253  dvariable ld=0.0;
255  {
256  for (int ic=1;ic<=nsc;ic++)
257  {
258  ld+=ln_det(block_diagonal_vhessian(ic),sgn);
259  }
260  ld*=0.5;
261  }
262  else
263  {
264  for (int ic=1;ic<=nsc;ic++)
265  {
266  ld+=ln_det_choleski(block_diagonal_vhessian(ic));
267  }
268  ld*=0.5;
269  }
270 
271  vf+=ld;
272 
273  double f=value(vf);
274  gradcalc(nvar,g);
275 
276  // put uhat back into the model
278  vy(xs+1,xs+us).shift(1)=u0;
279  initial_params::reset(vy); // get the values into the model
281 
282  ii=1;
283  for (int i=1;i<=xs;i++)
284  xadjoint(i)=g(ii++);
285  for (int i=1;i<=us;i++)
286  uadjoint(i)=g(ii++);
287  for (int ic=1;ic<=nsc;ic++)
288  {
289  int lus=lrea(ic);
290  for (int i=1;i<=lus;i++)
291  {
292  for (int j=1;j<=lus;j++)
293  {
294  (*pmin->lapprox->block_diagonal_vhessianadjoint)(ic)(i,j)=g(ii++);
295  }
296  }
297  }
298  return f;
299 }
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
Description not yet available.
Definition: adrndeff.h:182
double calculate_importance_sample_block_diagonal(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2im3.cpp:23
int indexmax() const
Definition: imatrix.h:142
Description not yet available.
Definition: imatrix.h:69
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
dvar_vector & shift(int min)
Description not yet available.
Definition: fvar_arr.cpp:28
static void set_active_random_effects(void)
Definition: model.cpp:267
int indexmin() const
Definition: imatrix.h:138
#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
dvar_matrix * importance_sampling_components
Definition: adrndeff.h:224
void initialize(void)
Description not yet available.
Definition: f3arr.cpp:17
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
exitptr ad_exit
Definition: gradstrc.cpp:53
#define dmin(a, b)
Definition: cbivnorm.cpp:190
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
static int nvarcalc()
Definition: model.cpp:152
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
static int no_ln_det_choleski_flag
Definition: fvar.hpp:8841
dvar3_array * block_diagonal_vch
Definition: adrndeff.h:228
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
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
#define min(a, b)
Definition: cbivnorm.cpp:188
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
Description not yet available.
Definition: fvar.hpp:2819
Description not yet available.
Definition: fvar.hpp:4197
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
d3_array * block_diagonal_vhessianadjoint
Definition: adrndeff.h:232
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
dvar3_array * block_diagonal_vhessian
Definition: adrndeff.h:233
static int have_bounded_random_effects
Definition: df1b2fun.h:1353
virtual void AD_uf_outer()
Definition: xmodelm4.cpp:39
double ln_det_choleski(const banded_symmetric_dmatrix &MM, int &ierr)
Definition: dmat38.cpp:218
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
static void get_cHessian_contribution(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:591
void initialize(const dvector &ww)
Description not yet available.
Definition: fvar_a24.cpp:63
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
Description not yet available.
Definition: fvar.hpp:3727
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259