ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2imp.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 
23 double calculate_importance_sample(const dvector& x,const dvector& u0,
24  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
25  const dmatrix& _Hessadjoint,function_minimizer * pmin)
26 {
27  ADUNCONST(dvector,xadjoint)
28  ADUNCONST(dvector,uadjoint)
29  ADUNCONST(dmatrix,Hessadjoint)
30 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
31  const int xs = [](unsigned int size)->int
32  {
33  assert(size <= INT_MAX);
34  return static_cast<int>(size);
35  }(x.size());
36  const int us = [](unsigned int size)->int
37  {
38  assert(size <= INT_MAX);
39  return static_cast<int>(size);
40  }(u0.size());
41 #else
42  const int xs = static_cast<int>(x.size());
43  const int us = static_cast<int>(u0.size());
44 #endif
46  int nvar = xs + us + us * us;
47  independent_variables y(1,nvar);
48 
49  // need to set random effects active together with whatever
50  // init parameters should be active in this phase
53  /*int onvar=*/initial_params::nvarcalc();
54  initial_params::xinit(y); // get the initial values into the
55  // do we need this next line?
56  y(1,xs)=x;
57 
58  int i,j;
59  dvar_vector d(1,xs+us);
60 
61  // contribution for quadratic prior
63  {
64  //Hess+=quadratic_prior::get_cHessian_contribution();
65  int & vxs = (int&)(xs);
67  }
68  // Here need hooks for sparse matrix structures
69  int ii=xs+us+1;
70  for (i=1;i<=us;i++)
71  for (j=1;j<=us;j++)
72  y(ii++)=Hess(i,j);
73 
76  dvar_matrix vHess(1,us,1,us);
77  ii=xs+us+1;
79  {
80  cerr << "can't do importance sampling with bounded random effects"
81  " at present" << endl;
82  ad_exit(1);
83  }
84  else
85  {
86  for (i=1;i<=us;i++)
87  {
88  for (j=1;j<=us;j++)
89  {
90  vHess(i,j)=vy(ii++);
91  }
92  }
93  }
94 
95  int nsamp=pmin->lapprox->num_importance_samples;
96  dvar_vector sample_value(1,nsamp);
97  sample_value.initialize();
98  dvar_matrix ch=choleski_decomp(inv(vHess));
99 
100  dvariable vf=0.0;
101  for (int is=1;is<=nsamp;is++)
102  {
103  dvar_vector tau=ch*pmin->lapprox->epsilon(is);
104 
105  vy(xs+1,xs+us).shift(1)+=tau;
106  initial_params::reset(vy); // get the values into the model
107  vy(xs+1,xs+us).shift(1)-=tau;
108 
110  pmin->AD_uf_outer();
111 
112  sample_value(is)=*objective_function_value::pobjfun
113  -0.5*norm2(pmin->lapprox->epsilon(is));
114  }
115 
116 
118  print_importance_sampling_weights_flag==1)
119  {
120  double min_vf=min(value(sample_value));
121  dvector tmp=exp(value(sample_value)-min_vf);
122  cout << "The unsorted normalized importance samplng weights are " << endl
123  << tmp << endl;
124  cout << "The sorted normalized importance samplng weights are " << endl
125  << sort(tmp) << endl;
126  ad_exit(1);
127  }
128 
129 
130  dvariable min_vf=min(sample_value);
131  vf=min_vf-log(mean(exp(min_vf-sample_value)));
132  vf-=us*0.91893853320467241;
133 
134  int sgn=0;
135  dvariable ld;
137  ld=0.5*ln_det(vHess,sgn);
138  else
139  ld=0.5*ln_det_choleski(vHess);
140 
141  vf+=ld;
142  double f=value(vf);
143  dvector g(1,nvar);
144  gradcalc(nvar,g);
145 
146  // put uhat back into the model
148  vy(xs+1,xs+us).shift(1)=u0;
149  initial_params::reset(vy); // get the values into the model
151 
152  ii=1;
153  for (i=1;i<=xs;i++)
154  xadjoint(i)=g(ii++);
155  for (i=1;i<=us;i++)
156  uadjoint(i)=g(ii++);
157  for (i=1;i<=us;i++)
158  for (j=1;j<=us;j++)
159  Hessadjoint(i,j)=g(ii++);
160  return f;
161 }
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
Description not yet available.
Definition: adrndeff.h:182
double calculate_importance_sample(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: df1b2imp.cpp:23
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
#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
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
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
static int stddev_vscale(const dvar_vector &d, const dvar_vector &x)
Definition: model.cpp:191
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
#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
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
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
static int have_bounded_random_effects
Definition: df1b2fun.h:1353
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
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
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
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