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