ADMB Documentation  11.1.1927
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2im4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2im4.cpp 1919 2014-04-22 22:02:01Z 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_option2(const dvector& x,
00023   const dvector& u0,const dmatrix& Hess,const dvector& _xadjoint,
00024   const dvector& _uadjoint,const dmatrix& _Hessadjoint,
00025   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 =  int(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     for (int 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       if (lus>0)
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      for (ic=1;ic<=nsc;ic++)
00120      {
00121        int lus=lrea(ic);
00122        if (lus>0)
00123          tau(offset+1,offset+lus).shift(1)=block_diagonal_ch(ic)*
00124            pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
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        double neps=0.5*norm2(pmin->lapprox->epsilon(is));
00172 
00173        (*pmin->lapprox->importance_sampling_values)(is)=
00174            neps-value(*objective_function_value::pobjfun);
00175 
00176        (*pmin->lapprox->importance_sampling_weights)(is)=neps;
00177 
00178         sample_value(is)=*objective_function_value::pobjfun
00179           -neps;
00180      }
00181      else
00182      {
00183        dvector& e=pmin->lapprox->epsilon(is);
00184        double neps=-sum(log(.95*exp(-0.5*square(e))+
00185           0.0166666667*exp(-square(e)/18.0)));
00186 
00187        (*pmin->lapprox->importance_sampling_values)(is)=
00188          neps-value(*objective_function_value::pobjfun);
00189 
00190        sample_value(is)=*objective_function_value::pobjfun
00191          -neps;
00192      }
00193    }
00194 
00195    nsc=pmin->lapprox->num_separable_calls;
00196    dmatrix weights(1,nsc,1,nsamp);
00197    for (is=1;is<=nsamp;is++)
00198    {
00199      int offset=0;
00200      for (ic=1;ic<=nsc;ic++)
00201      {
00202        int lus=lrea(ic);
00203        dvector e= pmin->lapprox->epsilon(is)(offset+1,offset+lus).shift(1);
00204        offset+=lus;
00205        if (pmin->lapprox->use_outliers==0)
00206        {
00207          weights(ic,is)=0.5*norm2(e);
00208        }
00209        else
00210        {
00211          weights(ic,is)=-sum(log(.95*exp(-0.5*square(e))+
00212           0.0166666667*exp(-square(e)/18.0)));
00213        }
00214      }
00215    }
00216    dvar_vector lcomp(1,nsc);
00217    for (int isc=1;isc<=nsc;isc++)
00218    {
00219      dvar_vector & comps=
00220        (*pmin->lapprox->importance_sampling_components)(isc);
00221 
00222      dvar_vector diff=comps-weights(isc);
00223      double dmin=min(value(diff));
00224      lcomp(isc)=dmin-log(mean(exp(dmin-diff)));
00225    }
00226 
00227    //double ns=lcomp.indexmax()-lcomp.indexmin()+1;
00228    //double min_vf=min(value(lcomp));
00229    vf= sum(lcomp);
00230    vf-=us*0.91893853320467241;
00231 
00232    int sgn=0;
00233    dvariable ld=0.0;
00234    if (ad_comm::no_ln_det_choleski_flag)
00235    {
00236      for (int ic=1;ic<=nsc;ic++)
00237      {
00238        ld+=ln_det(block_diagonal_vhessian(ic),sgn);
00239      }
00240      ld*=0.5;
00241    }
00242    else
00243    {
00244      for (int ic=1;ic<=nsc;ic++)
00245      {
00246        if (allocated(block_diagonal_vhessian(ic)))
00247          ld+=ln_det_choleski(block_diagonal_vhessian(ic));
00248      }
00249      ld*=0.5;
00250    }
00251 
00252    vf+=ld;
00253 
00254    double f=value(vf);
00255    gradcalc(nvar,g);
00256 
00257    // put uhat back into the model
00258    gradient_structure::set_NO_DERIVATIVES();
00259    vy(xs+1,xs+us).shift(1)=u0;
00260    initial_params::reset(vy);    // get the values into the model
00261    gradient_structure::set_YES_DERIVATIVES();
00262 
00263   ii=1;
00264   for (i=1;i<=xs;i++)
00265     xadjoint(i)=g(ii++);
00266   for (i=1;i<=us;i++)
00267     uadjoint(i)=g(ii++);
00268   for (ic=1;ic<=nsc;ic++)
00269   {
00270     int lus=lrea(ic);
00271     for (i=1;i<=lus;i++)
00272     {
00273       for (j=1;j<=lus;j++)
00274       {
00275         (*pmin->lapprox->block_diagonal_vhessianadjoint)(ic)(i,j)=g(ii++);
00276       }
00277     }
00278   }
00279   return f;
00280 }
00281 #endif