ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2im4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2im4.cpp 2295 2014-09-04 23:35:11Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
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   //ADUNCONST(dmatrix,Hessadjoint)
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   // need to set random effects active together with whatever
00037   // init parameters should be active in this phase
00038   initial_params::set_inactive_only_random_effects();
00039   initial_params::set_active_random_effects();
00040   /*int onvar=*/initial_params::nvarcalc();
00041   initial_params::xinit(y);    // get the initial values into the
00042   // do we need this next line?
00043   y(1,xs)=x;
00044 
00045   int i,j;
00046 
00047   // contribution for quadratic prior
00048   if (quadratic_prior::get_num_quadratic_prior()>0)
00049   {
00050     //Hess+=quadratic_prior::get_cHessian_contribution();
00051     int & vxs = (int&)(xs);
00052     quadratic_prior::get_cHessian_contribution(Hess,vxs);
00053   }
00054  // Here need hooks for sparse matrix structures
00055 
00056   dvar3_array & block_diagonal_vhessian=
00057     *pmin->lapprox->block_diagonal_vhessian;
00058   block_diagonal_vhessian.initialize();
00059   dvar3_array& block_diagonal_ch=
00060     *pmin->lapprox->block_diagonal_vch;
00061     //dvar3_array(*pmin->lapprox->block_diagonal_ch);
00062   int ii=xs+us+1;
00063   d3_array& bdH=(*pmin->lapprox->block_diagonal_hessian);
00064   int ic;
00065   for (ic=1;ic<=nsc;ic++)
00066   {
00067     int lus=lrea(ic);
00068     for (i=1;i<=lus;i++)
00069       for (j=1;j<=lus;j++)
00070         y(ii++)=bdH(ic)(i,j);
00071   }
00072 
00073   dvector g(1,nvar);
00074   gradcalc(0,g);
00075   gradient_structure::set_YES_DERIVATIVES();
00076   dvar_vector vy=dvar_vector(y);
00077   //initial_params::stddev_vscale(d,vy);
00078   ii=xs+us+1;
00079   if (initial_df1b2params::have_bounded_random_effects)
00080   {
00081     cerr << "can't do importance sampling with bounded random effects"
00082      " at present" << endl;
00083     ad_exit(1);
00084   }
00085   else
00086   {
00087     for (int ic=1;ic<=nsc;ic++)
00088     {
00089       int lus=lrea(ic);
00090       for (i=1;i<=lus;i++)
00091       {
00092         for (j=1;j<=lus;j++)
00093         {
00094           block_diagonal_vhessian(ic,i,j)=vy(ii++);
00095         }
00096       }
00097       if (lus>0)
00098         block_diagonal_ch(ic)=
00099           choleski_decomp(inv(block_diagonal_vhessian(ic)));
00100     }
00101   }
00102 
00103    int nsamp=pmin->lapprox->num_importance_samples;
00104 
00105    dvariable vf=0.0;
00106 
00107    dvar_vector sample_value(1,nsamp);
00108    sample_value.initialize();
00109 
00110    dvar_vector tau(1,us);;
00111    int is;
00112    for (is=1;is<=nsamp;is++)
00113    {
00114      int offset=0;
00115      pmin->lapprox->importance_sampling_counter=is;
00116      for (ic=1;ic<=nsc;ic++)
00117      {
00118        int lus=lrea(ic);
00119        if (lus>0)
00120          tau(offset+1,offset+lus).shift(1)=block_diagonal_ch(ic)*
00121            pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
00122        offset+=lus;
00123      }
00124 
00125      // have to reorder the terms to match the block diagonal hessian
00126      imatrix & ls=*(pmin->lapprox->block_diagonal_re_list);
00127      int mmin=ls.indexmin();
00128      int mmax=ls.indexmax();
00129 
00130      int ii=1;
00131      int i;
00132      for (i=mmin;i<=mmax;i++)
00133      {
00134        int cmin=ls(i).indexmin();
00135        int cmax=ls(i).indexmax();
00136        for (int j=cmin;j<=cmax;j++)
00137        {
00138          vy(ls(i,j))+=tau(ii++);
00139        }
00140      }
00141      if (ii-1 != us)
00142      {
00143        cerr << "error in interface" << endl;
00144        ad_exit(1);
00145      }
00146      initial_params::reset(vy);    // get the values into the model
00147      ii=1;
00148      for (i=mmin;i<=mmax;i++)
00149      {
00150        int cmin=ls(i).indexmin();
00151        int cmax=ls(i).indexmax();
00152        for (int j=cmin;j<=cmax;j++)
00153        {
00154          vy(ls(i,j))-=tau(ii++);
00155        }
00156      }
00157 
00158      *objective_function_value::pobjfun=0.0;
00159      pmin->AD_uf_outer();
00160 
00161      if (pmin->lapprox->use_outliers==0)
00162      {
00163        double neps=0.5*norm2(pmin->lapprox->epsilon(is));
00164 
00165        (*pmin->lapprox->importance_sampling_values)(is)=
00166            neps-value(*objective_function_value::pobjfun);
00167 
00168        (*pmin->lapprox->importance_sampling_weights)(is)=neps;
00169 
00170         sample_value(is)=*objective_function_value::pobjfun
00171           -neps;
00172      }
00173      else
00174      {
00175        dvector& e=pmin->lapprox->epsilon(is);
00176        double neps=-sum(log(.95*exp(-0.5*square(e))+
00177           0.0166666667*exp(-square(e)/18.0)));
00178 
00179        (*pmin->lapprox->importance_sampling_values)(is)=
00180          neps-value(*objective_function_value::pobjfun);
00181 
00182        sample_value(is)=*objective_function_value::pobjfun
00183          -neps;
00184      }
00185    }
00186 
00187    nsc=pmin->lapprox->num_separable_calls;
00188    dmatrix weights(1,nsc,1,nsamp);
00189    for (is=1;is<=nsamp;is++)
00190    {
00191      int offset=0;
00192      for (ic=1;ic<=nsc;ic++)
00193      {
00194        int lus=lrea(ic);
00195        dvector e= pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
00196        offset+=lus;
00197        if (pmin->lapprox->use_outliers==0)
00198        {
00199          weights(ic,is)=0.5*norm2(e);
00200        }
00201        else
00202        {
00203          weights(ic,is)=-sum(log(.95*exp(-0.5*square(e))+
00204           0.0166666667*exp(-square(e)/18.0)));
00205        }
00206      }
00207    }
00208    dvar_vector lcomp(1,nsc);
00209    for (int isc=1;isc<=nsc;isc++)
00210    {
00211      dvar_vector & comps=
00212        (*pmin->lapprox->importance_sampling_components)(isc);
00213 
00214      dvar_vector diff=comps-weights(isc);
00215      double dmin=min(value(diff));
00216      lcomp(isc)=dmin-log(mean(exp(dmin-diff)));
00217    }
00218 
00219    //double ns=lcomp.indexmax()-lcomp.indexmin()+1;
00220    //double min_vf=min(value(lcomp));
00221    vf= sum(lcomp);
00222    vf-=us*0.91893853320467241;
00223 
00224    int sgn=0;
00225    dvariable ld=0.0;
00226    if (ad_comm::no_ln_det_choleski_flag)
00227    {
00228      for (int ic=1;ic<=nsc;ic++)
00229      {
00230        ld+=ln_det(block_diagonal_vhessian(ic),sgn);
00231      }
00232      ld*=0.5;
00233    }
00234    else
00235    {
00236      for (int ic=1;ic<=nsc;ic++)
00237      {
00238        if (allocated(block_diagonal_vhessian(ic)))
00239          ld+=ln_det_choleski(block_diagonal_vhessian(ic));
00240      }
00241      ld*=0.5;
00242    }
00243 
00244    vf+=ld;
00245 
00246    double f=value(vf);
00247    gradcalc(nvar,g);
00248 
00249    // put uhat back into the model
00250    gradient_structure::set_NO_DERIVATIVES();
00251    vy(xs+1,xs+us).shift(1)=u0;
00252    initial_params::reset(vy);    // get the values into the model
00253    gradient_structure::set_YES_DERIVATIVES();
00254 
00255   ii=1;
00256   for (i=1;i<=xs;i++)
00257     xadjoint(i)=g(ii++);
00258   for (i=1;i<=us;i++)
00259     uadjoint(i)=g(ii++);
00260   for (ic=1;ic<=nsc;ic++)
00261   {
00262     int lus=lrea(ic);
00263     for (i=1;i<=lus;i++)
00264     {
00265       for (j=1;j<=lus;j++)
00266       {
00267         (*pmin->lapprox->block_diagonal_vhessianadjoint)(ic)(i,j)=g(ii++);
00268       }
00269     }
00270   }
00271   return f;
00272 }