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