ADMB Documentation  11.1.1907
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2gh.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2gh.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 
00020 double do_gauss_hermite_block_diagonal(const dvector& x,
00021   const dvector& u0,const dmatrix& Hess,const dvector& _xadjoint,
00022   const dvector& _uadjoint,const dmatrix& _Hessadjoint,
00023   function_minimizer * pmin)
00024 {
00025   ADUNCONST(dvector,xadjoint)
00026   ADUNCONST(dvector,uadjoint)
00027   //ADUNCONST(dmatrix,Hessadjoint)
00028   const int xs=x.size();
00029   const int us=u0.size();
00030   gradient_structure::set_NO_DERIVATIVES();
00031   int nsc=pmin->lapprox->num_separable_calls;
00032   const ivector lrea = (*pmin->lapprox->num_local_re_array)(1,nsc);
00033   int hroom =  sum(square(lrea));
00034   int nvar=x.size()+u0.size()+hroom;
00035   independent_variables y(1,nvar);
00036 
00037   // need to set random effects active together with whatever
00038   // init parameters should be active in this phase
00039   initial_params::set_inactive_only_random_effects();
00040   initial_params::set_active_random_effects();
00041   /*int onvar=*/initial_params::nvarcalc();
00042   initial_params::xinit(y);    // get the initial values into the
00043   // do we need this next line?
00044   y(1,xs)=x;
00045 
00046   int i,j;
00047 
00048   // contribution for quadratic prior
00049   if (quadratic_prior::get_num_quadratic_prior()>0)
00050   {
00051     //Hess+=quadratic_prior::get_cHessian_contribution();
00052     int & vxs = (int&)(xs);
00053     quadratic_prior::get_cHessian_contribution(Hess,vxs);
00054   }
00055  // Here need hooks for sparse matrix structures
00056 
00057   dvar3_array & block_diagonal_vhessian=
00058     *pmin->lapprox->block_diagonal_vhessian;
00059   block_diagonal_vhessian.initialize();
00060   dvar3_array& block_diagonal_ch=
00061     *pmin->lapprox->block_diagonal_vch;
00062     //dvar3_array(*pmin->lapprox->block_diagonal_ch);
00063   int ii=xs+us+1;
00064   d3_array& bdH=(*pmin->lapprox->block_diagonal_hessian);
00065   int ic;
00066   for (ic=1;ic<=nsc;ic++)
00067   {
00068     int lus=lrea(ic);
00069     for (i=1;i<=lus;i++)
00070       for (j=1;j<=lus;j++)
00071         y(ii++)=bdH(ic)(i,j);
00072   }
00073 
00074   dvector g(1,nvar);
00075   gradcalc(0,g);
00076   gradient_structure::set_YES_DERIVATIVES();
00077   dvar_vector vy=dvar_vector(y);
00078   //initial_params::stddev_vscale(d,vy);
00079   ii=xs+us+1;
00080   if (initial_df1b2params::have_bounded_random_effects)
00081   {
00082     cerr << "can't do importance sampling with bounded random effects"
00083      " at present" << endl;
00084     ad_exit(1);
00085   }
00086   else
00087   {
00088     for (int ic=1;ic<=nsc;ic++)
00089     {
00090       int lus=lrea(ic);
00091       if (lus>0)
00092       {
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 
00106    int nsamp=pmin->lapprox->use_gauss_hermite;
00107    pmin->lapprox->in_gauss_hermite_phase=1;
00108    dvar_vector sample_value(1,nsamp);
00109    sample_value.initialize();
00110 
00111    dvar_vector tau(1,us);;
00112    // !!! This only works for one random efect in each separable call
00113    // at present.
00114    for (int is=1;is<=nsamp;is++)
00115    {
00116      int offset=0;
00117      pmin->lapprox->num_separable_calls=0;
00118      pmin->lapprox->gh->is=is;
00119      for (ic=1;ic<=nsc;ic++)
00120      {
00121        int lus=lrea(ic);
00122        // will need vector stuff here when more than one random effect
00123        if (lus>1)
00124        {
00125          cerr << "error not implemented" << endl;
00126          ad_exit(1);
00127        }
00128        if (lus>0)
00129        {
00130          tau(offset+1,offset+lus).shift(1)=block_diagonal_ch(ic)(1,1)*
00131            pmin->lapprox->gh->x(is);
00132          offset+=lus;
00133        }
00134      }
00135 
00136      // have to reorder the terms to match the block diagonal hessian
00137      imatrix & ls=*(pmin->lapprox->block_diagonal_re_list);
00138      int mmin=ls.indexmin();
00139      int mmax=ls.indexmax();
00140 
00141      int ii=1;
00142      int i;
00143      for (i=mmin;i<=mmax;i++)
00144      {
00145        int cmin=ls(i).indexmin();
00146        int cmax=ls(i).indexmax();
00147        for (int j=cmin;j<=cmax;j++)
00148        {
00149          vy(ls(i,j))+=tau(ii++);
00150        }
00151      }
00152      if (ii-1 != us)
00153      {
00154        cerr << "error in interface" << endl;
00155        ad_exit(1);
00156      }
00157      initial_params::reset(vy);    // get the values into the model
00158      ii=1;
00159      for (i=mmin;i<=mmax;i++)
00160      {
00161        int cmin=ls(i).indexmin();
00162        int cmax=ls(i).indexmax();
00163        for (int j=cmin;j<=cmax;j++)
00164        {
00165          vy(ls(i,j))-=tau(ii++);
00166        }
00167      }
00168 
00169      *objective_function_value::pobjfun=0.0;
00170      pmin->AD_uf_outer();
00171    }
00172 
00173    nsc=pmin->lapprox->num_separable_calls;
00174 
00175    dvariable vf=pmin->do_gauss_hermite_integration();
00176 
00177    int sgn=0;
00178    dvariable ld=0.0;
00179    if (ad_comm::no_ln_det_choleski_flag)
00180    {
00181      for (int ic=1;ic<=nsc;ic++)
00182      {
00183        if (allocated(block_diagonal_vhessian(ic)))
00184        {
00185          ld+=ln_det(block_diagonal_vhessian(ic),sgn);
00186        }
00187      }
00188      ld*=0.5;
00189    }
00190    else
00191    {
00192      for (int ic=1;ic<=nsc;ic++)
00193      {
00194        if (allocated(block_diagonal_vhessian(ic)))
00195        {
00196          ld+=ln_det_choleski(block_diagonal_vhessian(ic));
00197        }
00198      }
00199      ld*=0.5;
00200    }
00201 
00202    vf+=ld;
00203    //vf+=us*0.91893853320467241;
00204 
00205    double f=value(vf);
00206    gradcalc(nvar,g);
00207 
00208    // put uhat back into the model
00209    gradient_structure::set_NO_DERIVATIVES();
00210    vy(xs+1,xs+us).shift(1)=u0;
00211    initial_params::reset(vy);    // get the values into the model
00212    gradient_structure::set_YES_DERIVATIVES();
00213 
00214    pmin->lapprox->in_gauss_hermite_phase=0;
00215 
00216   ii=1;
00217   for (i=1;i<=xs;i++)
00218     xadjoint(i)=g(ii++);
00219   for (i=1;i<=us;i++)
00220     uadjoint(i)=g(ii++);
00221   for (ic=1;ic<=nsc;ic++)
00222   {
00223     int lus=lrea(ic);
00224     for (i=1;i<=lus;i++)
00225     {
00226       for (j=1;j<=lus;j++)
00227       {
00228         (*pmin->lapprox->block_diagonal_vhessianadjoint)(ic)(i,j)=g(ii++);
00229       }
00230     }
00231   }
00232   return f;
00233 }
00234 #endif