ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2im2.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 banded_symmetric_dmatrix& bHess,const dvector& _xadjoint,
25  const dvector& _uadjoint,
26  const banded_symmetric_dmatrix& _bHessadjoint,function_minimizer * pmin)
27 {
28  ADUNCONST(dvector,xadjoint)
29  ADUNCONST(dvector,uadjoint)
31  int bw=bHess.bandwidth();
32 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
33  const int xs = [](unsigned int size)->int
34  {
35  assert(size <= INT_MAX);
36  return static_cast<int>(size);
37  }(x.size());
38  const int us = [](unsigned int size)->int
39  {
40  assert(size <= INT_MAX);
41  return static_cast<int>(size);
42  }(u0.size());
43 #else
44  const int xs = static_cast<int>(x.size());
45  const int us = static_cast<int>(u0.size());
46 #endif
48  int nvar = xs + us + ((bw + 1) * (2 * us - bw))/2;
49  independent_variables y(1,nvar);
50 
51  // need to set random effects active together with whatever
52  // init parameters should be active in this phase
55  /*int onvar=*/initial_params::nvarcalc();
56  initial_params::xinit(y); // get the initial values into the
57  y(1,xs)=x;
58 
59  int i,j;
60 
61  // Here need hooks for sparse matrix structures
62  int ii=xs+us+1;
63  for (i=1;i<=us;i++)
64  {
65  int jmin=admax(1,i-bw+1);
66  for (j=jmin;j<=i;j++)
67  y(ii++)=bHess(i,j);
68  }
69 
71  banded_symmetric_dvar_matrix vHess(1,us,bw);
72  initial_params::reset(vy); // get the initial values into the
73  ii=xs+us+1;
74  for (i=1;i<=us;i++)
75  {
76  int jmin=admax(1,i-bw+1);
77  for (j=jmin;j<=i;j++)
78  vHess(i,j)=vy(ii++);
79  }
80 
81  int nsamp=pmin->lapprox->num_importance_samples;
82  dvar_vector sample_value(1,nsamp);
83  sample_value.initialize();
84 
85  int ierr = 0;
87  if (ierr)
88  {
89  cerr << "error in choleski decomp" << endl;
90  ad_exit(1);
91  }
92 
93  dvariable vf=0.0;
95  print_importance_sampling_weights_flag==0)
96  {
97  for (int is=1;is<=nsamp;is++)
98  {
99  dvar_vector tau=solve_trans(ch,pmin->lapprox->epsilon(is));
100 
101  vy(xs+1,xs+us).shift(1)+=tau;
102  initial_params::reset(vy); // get the values into the model
103  vy(xs+1,xs+us).shift(1)-=tau;
104 
106  pmin->AD_uf_outer();
107 
108  sample_value(is)=*objective_function_value::pobjfun
109  -0.5*norm2(pmin->lapprox->epsilon(is));
110  }
111  }
112  else
113  {
114  dvector normal_weight(1,nsamp);
115  int is;
116  for (is=1;is<=nsamp;is++)
117  {
118  dvar_vector tau=solve_trans(ch,pmin->lapprox->epsilon(is));
119 
120  vy(xs+1,xs+us).shift(1)+=tau;
121  initial_params::reset(vy); // get the values into the model
122  vy(xs+1,xs+us).shift(1)-=tau;
123 
125  pmin->AD_uf_outer();
126 
127  sample_value(is)=*objective_function_value::pobjfun;
128  normal_weight(is)=0.5*norm2(pmin->lapprox->epsilon(is));
129  }
130  dvector tmp1(value(sample_value)-normal_weight);
131  double min_vf=min(tmp1);
132  dvector tmp=exp(tmp1-min_vf);
133  cout << "The unsorted normalized importance samplng weights are " << endl
134  << tmp << endl;
135  cout << "The sorted normalized importance samplng weights are " << endl
136  << sort(tmp) << endl;
137  cout << "The sample value normal weight pairs are " << endl;
138  for (is=1;is<=nsamp;is++)
139  {
140  cout << sample_value(is) << " " << normal_weight(is) << endl;
141  }
142  cout << "The normalized sample value normal weight pairs are " << endl;
143  for (is=1;is<=nsamp;is++)
144  {
145  cout << normal_weight(is) << " "
146  << sample_value(is)-normal_weight(is) << endl;
147  }
148  ad_exit(1);
149  }
150 
151  dvariable min_vf=min(sample_value);
152  vf=min_vf-log(mean(exp(min_vf-sample_value)));
153  vf-=us*0.91893853320467241;
154 
155  int sgn=0;
156  dvariable ld;
157 
158  ld=0.5*ln_det_choleski(vHess,sgn);
159 
160  vf+=ld;
161  double f=value(vf);
162  dvector g(1,nvar);
163  gradcalc(nvar,g);
164 
165  ii=1;
166  for (i=1;i<=xs;i++)
167  xadjoint(i)=g(ii++);
168  for (i=1;i<=us;i++)
169  uadjoint(i)=g(ii++);
170  for (i=1;i<=us;i++)
171  {
172  int jmin=admax(1,i-bw+1);
173  for (j=jmin;j<=i;j++)
174  bHessadjoint(i,j)=g(ii++);
175  }
176  return f;
177 }
Description not yet available.
Definition: fvar.hpp:7981
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
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 admax(int i, int j)
Definition: fvar.hpp:8979
#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
Description not yet available.
Definition: fvar.hpp:8190
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
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
dvector solve_trans(const lower_triangular_dmatrix &M, const dvector &y)
Description not yet available.
Definition: dmat36.cpp:80
#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
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
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
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
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
Description not yet available.
Definition: fvar.hpp:8065
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259
int bandwidth(void) const
Definition: fvar.hpp:7991