ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2impspf.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 
19 class dvar_hs_smatrix;
20 
26  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
27  const dmatrix& _Hessadjoint,function_minimizer * pmin)
28 {
29  ADUNCONST(dvector,xadjoint)
30  ADUNCONST(dvector,uadjoint)
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 
48  int nvar=0;
49  if (pmin->lapprox->sparse_hessian_flag==0)
50  {
51  cerr << "Error we should not be here" << endl;
52  ad_exit(1);
53  }
54 
56  int smin=lst.indexmin();
57  int smax=lst.indexmax();
58  int sz= smax-smin+1;
59  nvar = xs + us + sz;
60  independent_variables y(1,nvar);
61 
62  // need to set random effects active together with whatever
63  // init parameters should be active in this phase
66  /*int onvar=*/initial_params::nvarcalc();
67  initial_params::xinit(y); // get the initial values into the
68  // do we need this next line?
69  y(1,xs)=x;
70 
71  dvar_vector d(1,xs+us);
72 
73  // contribution for quadratic prior
75  {
76  //Hess+=quadratic_prior::get_cHessian_contribution();
77  int & vxs = (int&)(xs);
79  }
80  // Here need hooks for sparse matrix structures
81 
82  int ii=xs+us+1;
83  for (int i=smin;i<=smax;i++)
84  y(ii++)=lst(i);
85 
88 
89 
90  ii=xs+us+1;
92  {
93  cerr << "can't do importance sampling with bounded random effects"
94  " at present" << endl;
95  ad_exit(1);
96  }
97  else
98  {
100  int mmin=lst.indexmin();
101  int mmax=lst.indexmax();
102  dvar_compressed_triplet * vsparse_triplet =
103  pmin->lapprox->vsparse_triplet;
104 
105  if (vsparse_triplet==0)
106  {
107  pmin->lapprox->vsparse_triplet=
108  new dvar_compressed_triplet(mmin,mmax,us,us);
109  vsparse_triplet = pmin->lapprox->vsparse_triplet;
110  for (int i=mmin;i<=mmax;i++)
111  {
112  (*vsparse_triplet)(1,i)=lst(1,i);
113  (*vsparse_triplet)(2,i)=lst(2,i);
114  }
115  }
116  else
117  {
118  if (!allocated(*vsparse_triplet))
119  {
120  (*vsparse_triplet).allocate(mmin,mmax,us,us);
121  for (int i=mmin;i<=mmax;i++)
122  {
123  (*vsparse_triplet)(1,i)=lst(1,i);
124  (*vsparse_triplet)(2,i)=lst(2,i);
125  }
126  }
127  }
128  dcompressed_triplet * vsparse_triplet_adjoint =
130 
131  if (vsparse_triplet_adjoint==0)
132  {
134  new dcompressed_triplet(mmin,mmax,us,us);
135  vsparse_triplet_adjoint = pmin->lapprox->vsparse_triplet_adjoint;
136  for (int i=mmin;i<=mmax;i++)
137  {
138  (*vsparse_triplet_adjoint)(1,i)=lst(1,i);
139  (*vsparse_triplet_adjoint)(2,i)=lst(2,i);
140  }
141  }
142  else
143  {
144  if (!allocated(*vsparse_triplet_adjoint))
145  {
146  (*vsparse_triplet_adjoint).allocate(mmin,mmax,us,us);
147  for (int i=mmin;i<=mmax;i++)
148  {
149  (*vsparse_triplet_adjoint)(1,i)=lst(1,i);
150  (*vsparse_triplet_adjoint)(2,i)=lst(2,i);
151  }
152  }
153  }
154  vsparse_triplet->get_x()=vy(ii,ii+mmax-mmin).shift(1);
155  }
156 
157  int nsamp=pmin->lapprox->num_importance_samples;
158  dvar_vector sample_value(1,nsamp);
159  sample_value.initialize();
160  //dvar_matrix ch=choleski_decomp(inv(vHess));
161 
162  dvar_compressed_triplet * vsparse_triplet
163  = pmin->lapprox->vsparse_triplet;
164 
165  dvar_hs_smatrix * dhs= return_choleski_decomp(*vsparse_triplet);
166 
167  dvariable vf=0.0;
168 
169  // get fhat for ocputing weighted sums.
170  initial_params::reset(vy); // get the values into the model
172  pmin->AD_uf_outer();
174  // check if we do this in a group of funnels
175  if (pmin->lapprox->isfunnel_flag==0)
176  {
177  for (int is=1;is<=nsamp;is++)
178  {
179  //dvar_vector tau=ch*pmin->lapprox->epsilon(is);
181  pmin->lapprox->epsilon(is));
182 
183  vy(xs+1,xs+us).shift(1)+=tau;
184  initial_params::reset(vy); // get the values into the model
185  vy(xs+1,xs+us).shift(1)-=tau;
186 
188  pmin->AD_uf_outer();
189 
190  /*
191  if (pmin->lapprox->istudent_flag==0)
192  */
193  {
194  sample_value(is)=*objective_function_value::pobjfun
195  -0.5*norm2(pmin->lapprox->epsilon(is));
196  }
197  /*
198  else
199  {
200  sample_value(is)=*objective_function_value::pobjfun
201  +sum(log_student_density(pmin->lapprox->istudent_flag,
202  pmin->lapprox->epsilon(is)));
203  }
204  */
206  (*(pmin->lapprox->importance_sampling_values))(is)
207  =value(sample_value(is))-fhat;
208  }
209  dvariable min_vf=min(sample_value);
210  vf=min_vf-log(mean(exp(min_vf-sample_value)));
211  }
212  else
213  {
214  int& nfunnelblocks=pmin->lapprox->nfunnelblocks;
215  int lbound=1;
216  int samplesize=nsamp/nfunnelblocks;
217  int ubound=samplesize;
218  int mean_count=0;
219  dvar_vector fvalues(1,nfunnelblocks);
220  ivector blocksizes(1,nfunnelblocks);
221  for (int iblock=1;iblock<=nfunnelblocks;iblock++)
222  {
223  funnel_dvariable fdv;
224  if (lbound>nsamp) break;
225  ad_begin_funnel();
226  int icount=0;
227 
228 
229  for (int is=lbound;is<=ubound;is++)
230  {
231  if (is>nsamp) break;
232  icount++;
234  pmin->lapprox->epsilon(is));
235  vy(xs+1,xs+us).shift(1)+=tau;
236  initial_params::reset(vy); // get the values into the model
237  vy(xs+1,xs+us).shift(1)-=tau;
238 
240  pmin->AD_uf_outer();
241  /*
242  if (pmin->lapprox->istudent_flag==0)
243  */
244  {
245  sample_value(icount)=*objective_function_value::pobjfun
246  -0.5*norm2(pmin->lapprox->epsilon(is));
247  }
248  /*
249  else
250  {
251  sample_value(icount)=*objective_function_value::pobjfun
252  +sum(log_student_density(pmin->lapprox->istudent_flag,
253  pmin->lapprox->epsilon(is)));
254  }
255  */
257  (*(pmin->lapprox->importance_sampling_values))(is)
258  =value(sample_value(icount))-fhat;
259  }
260 
261  if (icount>0)
262  {
263  mean_count+=1;
264  dvar_vector tsp=sample_value(1,icount);
265  double min_vf=min(value(tsp));
266  fdv=log(mean(exp(min_vf-tsp)))-min_vf;
267  dvariable tmp;
268  tmp=fdv;
269  fvalues(mean_count)=tmp;
270  blocksizes(mean_count)=icount;
271  }
272  lbound+=samplesize;
273  ubound+=samplesize;
274  }
275 
276  double fm=mean(value(fvalues(1,mean_count)));
277  dvar_vector nfval=exp(fvalues(1,mean_count)-fm);
278  vf=-fm-log(nfval*blocksizes(1,mean_count)/sum(blocksizes(1,mean_count)));
279  }
280  /*
281  if (pmin->lapprox->istudent_flag==0)
282  */
283  {
284  vf-=us*0.91893853320467241;
285  }
286  /*
287  else
288  {
289  (*(pmin->lapprox->importance_sampling_values))
290  +=us*0.91893853320467241;
291  }
292  */
293 
294  dvariable ld;
295 
296  //dvariable sld=0.5*ln_det_choleski(make_dvar_matrix(*vsparse_triplet));
297  dvariable sld=0.5*ln_det(*vsparse_triplet);
298  vf+=sld;
299 
300  double f=value(vf);
301  dvector g(1,nvar);
302  gradcalc(nvar,g);
303 
304  // put uhat back into the model
306  vy(xs+1,xs+us).shift(1)=u0;
307  initial_params::reset(vy); // get the values into the model
309 
310  ii=1;
311  for (int i=1;i<=xs;i++)
312  xadjoint(i)=g(ii++);
313  for (int i=1;i<=us;i++)
314  uadjoint(i)=g(ii++);
315 
316  dcompressed_triplet * vsparse_triplet_adjoint =
318  int mmin=vsparse_triplet_adjoint->indexmin();
319  int mmax=vsparse_triplet_adjoint->indexmax();
320  vsparse_triplet_adjoint->get_x()
321  =g(ii,ii+mmax-mmin).shift(1);
322 
323  return f;
324 }
Description not yet available.
Definition: fvar.hpp:1641
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
dcompressed_triplet * sparse_triplet2
Definition: adrndeff.h:271
int indexmin(void)
Definition: fvar.hpp:9354
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 indexmax(void)
Definition: fvar.hpp:9358
dvector & get_x(void)
Definition: fvar.hpp:9382
#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
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
double calculate_importance_sample_shess(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: df1b2impspf.cpp:25
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
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
static int nvarcalc()
Definition: model.cpp:152
static int stddev_vscale(const dvar_vector &d, const dvar_vector &x)
Definition: model.cpp:191
dcompressed_triplet * vsparse_triplet_adjoint
Definition: adrndeff.h:273
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
dvar_vector & get_x(void)
Definition: fvar.hpp:9327
#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:9345
Description not yet available.
Definition: fvar.hpp:2819
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
Description not yet available.
Definition: fvar.hpp:9293
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
void ad_begin_funnel(void)
Description not yet available.
Definition: xgradclc.cpp:386
static int have_bounded_random_effects
Definition: df1b2fun.h:1353
virtual void AD_uf_outer()
Definition: xmodelm4.cpp:39
dvar_compressed_triplet * vsparse_triplet
Definition: adrndeff.h:272
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
dvector return_choleski_factor_solve(hs_smatrix *PL, dvector &eps)
Definition: hs_sparse.cpp:2693
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
hs_smatrix * return_choleski_decomp(dcompressed_triplet &st)
Definition: hs_sparse.cpp:2636
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
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