Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00011 # include <admodel.h>
00012 # include <df1b2fun.h>
00013 # include <adrndeff.h>
00014
00019 double calculate_importance_sample_block_diagonal_option2(const dvector& x,
00020 const dvector& u0,const dmatrix& Hess,const dvector& _xadjoint,
00021 const dvector& _uadjoint,const dmatrix& _Hessadjoint,
00022 function_minimizer * pmin)
00023 {
00024 ADUNCONST(dvector,xadjoint)
00025 ADUNCONST(dvector,uadjoint)
00026
00027 const int xs=x.size();
00028 const int us=u0.size();
00029 gradient_structure::set_NO_DERIVATIVES();
00030 int nsc=pmin->lapprox->num_separable_calls;
00031 const ivector lrea = (*pmin->lapprox->num_local_re_array)(1,nsc);
00032 int hroom = int(sum(square(lrea)));
00033 int nvar=x.size()+u0.size()+hroom;
00034 independent_variables y(1,nvar);
00035
00036
00037
00038 initial_params::set_inactive_only_random_effects();
00039 initial_params::set_active_random_effects();
00040 initial_params::nvarcalc();
00041 initial_params::xinit(y);
00042
00043 y(1,xs)=x;
00044
00045
00046 if (quadratic_prior::get_num_quadratic_prior()>0)
00047 {
00048
00049 int & vxs = (int&)(xs);
00050 quadratic_prior::get_cHessian_contribution(Hess,vxs);
00051 }
00052
00053
00054 dvar3_array & block_diagonal_vhessian=
00055 *pmin->lapprox->block_diagonal_vhessian;
00056 block_diagonal_vhessian.initialize();
00057 dvar3_array& block_diagonal_ch=
00058 *pmin->lapprox->block_diagonal_vch;
00059
00060 int ii=xs+us+1;
00061 d3_array& bdH=(*pmin->lapprox->block_diagonal_hessian);
00062 for (int ic=1;ic<=nsc;ic++)
00063 {
00064 int lus=lrea(ic);
00065 for (int i=1;i<=lus;i++)
00066 for (int j=1;j<=lus;j++)
00067 y(ii++)=bdH(ic)(i,j);
00068 }
00069
00070 dvector g(1,nvar);
00071 gradcalc(0,g);
00072 gradient_structure::set_YES_DERIVATIVES();
00073 dvar_vector vy=dvar_vector(y);
00074
00075 ii=xs+us+1;
00076 if (initial_df1b2params::have_bounded_random_effects)
00077 {
00078 cerr << "can't do importance sampling with bounded random effects"
00079 " at present" << endl;
00080 ad_exit(1);
00081 }
00082 else
00083 {
00084 for (int ic=1;ic<=nsc;ic++)
00085 {
00086 int lus=lrea(ic);
00087 for (int i=1;i<=lus;i++)
00088 {
00089 for (int j=1;j<=lus;j++)
00090 {
00091 block_diagonal_vhessian(ic,i,j)=vy(ii++);
00092 }
00093 }
00094 if (lus>0)
00095 block_diagonal_ch(ic)=
00096 choleski_decomp(inv(block_diagonal_vhessian(ic)));
00097 }
00098 }
00099
00100 int nsamp=pmin->lapprox->num_importance_samples;
00101
00102 dvariable vf=0.0;
00103
00104 dvar_vector sample_value(1,nsamp);
00105 sample_value.initialize();
00106
00107 dvar_vector tau(1,us);;
00108 int is;
00109 for (is=1;is<=nsamp;is++)
00110 {
00111 int offset=0;
00112 pmin->lapprox->importance_sampling_counter=is;
00113 for (int ic=1;ic<=nsc;ic++)
00114 {
00115 int lus=lrea(ic);
00116 if (lus>0)
00117 tau(offset+1,offset+lus).shift(1)=block_diagonal_ch(ic)*
00118 pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
00119 offset+=lus;
00120 }
00121
00122
00123 imatrix & ls=*(pmin->lapprox->block_diagonal_re_list);
00124 int mmin=ls.indexmin();
00125 int mmax=ls.indexmax();
00126
00127 ii=1;
00128 for (int i=mmin;i<=mmax;i++)
00129 {
00130 int cmin=ls(i).indexmin();
00131 int cmax=ls(i).indexmax();
00132 for (int j=cmin;j<=cmax;j++)
00133 {
00134 vy(ls(i,j))+=tau(ii++);
00135 }
00136 }
00137 if (ii-1 != us)
00138 {
00139 cerr << "error in interface" << endl;
00140 ad_exit(1);
00141 }
00142 initial_params::reset(vy);
00143 ii=1;
00144 for (int i=mmin;i<=mmax;i++)
00145 {
00146 int cmin=ls(i).indexmin();
00147 int cmax=ls(i).indexmax();
00148 for (int j=cmin;j<=cmax;j++)
00149 {
00150 vy(ls(i,j))-=tau(ii++);
00151 }
00152 }
00153
00154 *objective_function_value::pobjfun=0.0;
00155 pmin->AD_uf_outer();
00156
00157 if (pmin->lapprox->use_outliers==0)
00158 {
00159 double neps=0.5*norm2(pmin->lapprox->epsilon(is));
00160
00161 (*pmin->lapprox->importance_sampling_values)(is)=
00162 neps-value(*objective_function_value::pobjfun);
00163
00164 (*pmin->lapprox->importance_sampling_weights)(is)=neps;
00165
00166 sample_value(is)=*objective_function_value::pobjfun
00167 -neps;
00168 }
00169 else
00170 {
00171 dvector& e=pmin->lapprox->epsilon(is);
00172 double neps=-sum(log(.95*exp(-0.5*square(e))+
00173 0.0166666667*exp(-square(e)/18.0)));
00174
00175 (*pmin->lapprox->importance_sampling_values)(is)=
00176 neps-value(*objective_function_value::pobjfun);
00177
00178 sample_value(is)=*objective_function_value::pobjfun
00179 -neps;
00180 }
00181 }
00182
00183 nsc=pmin->lapprox->num_separable_calls;
00184 dmatrix weights(1,nsc,1,nsamp);
00185 for (is=1;is<=nsamp;is++)
00186 {
00187 int offset=0;
00188 for (int ic=1;ic<=nsc;ic++)
00189 {
00190 int lus=lrea(ic);
00191 dvector e= pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
00192 offset+=lus;
00193 if (pmin->lapprox->use_outliers==0)
00194 {
00195 weights(ic,is)=0.5*norm2(e);
00196 }
00197 else
00198 {
00199 weights(ic,is)=-sum(log(.95*exp(-0.5*square(e))+
00200 0.0166666667*exp(-square(e)/18.0)));
00201 }
00202 }
00203 }
00204 dvar_vector lcomp(1,nsc);
00205 for (int isc=1;isc<=nsc;isc++)
00206 {
00207 dvar_vector & comps=
00208 (*pmin->lapprox->importance_sampling_components)(isc);
00209
00210 dvar_vector diff=comps-weights(isc);
00211 double dmin=min(value(diff));
00212 lcomp(isc)=dmin-log(mean(exp(dmin-diff)));
00213 }
00214
00215
00216
00217 vf= sum(lcomp);
00218 vf-=us*0.91893853320467241;
00219
00220 int sgn=0;
00221 dvariable ld=0.0;
00222 if (ad_comm::no_ln_det_choleski_flag)
00223 {
00224 for (int ic=1;ic<=nsc;ic++)
00225 {
00226 ld+=ln_det(block_diagonal_vhessian(ic),sgn);
00227 }
00228 ld*=0.5;
00229 }
00230 else
00231 {
00232 for (int ic=1;ic<=nsc;ic++)
00233 {
00234 if (allocated(block_diagonal_vhessian(ic)))
00235 ld+=ln_det_choleski(block_diagonal_vhessian(ic));
00236 }
00237 ld*=0.5;
00238 }
00239
00240 vf+=ld;
00241
00242 double f=value(vf);
00243 gradcalc(nvar,g);
00244
00245
00246 gradient_structure::set_NO_DERIVATIVES();
00247 vy(xs+1,xs+us).shift(1)=u0;
00248 initial_params::reset(vy);
00249 gradient_structure::set_YES_DERIVATIVES();
00250
00251 ii=1;
00252 for (int i=1;i<=xs;i++)
00253 xadjoint(i)=g(ii++);
00254 for (int i=1;i<=us;i++)
00255 uadjoint(i)=g(ii++);
00256 for (int ic=1;ic<=nsc;ic++)
00257 {
00258 int lus=lrea(ic);
00259 for (int i=1;i<=lus;i++)
00260 {
00261 for (int j=1;j<=lus;j++)
00262 {
00263 (*pmin->lapprox->block_diagonal_vhessianadjoint)(ic)(i,j)=g(ii++);
00264 }
00265 }
00266 }
00267 return f;
00268 }