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