38 #if defined(USE_DD_STUFF)
39 # if defined(_MSC_VER)
40 extern "C" _export
void dd_newton_raphson(
int n,
double * v,
double * diag,
41 double * ldiag,
double * yy);
43 extern "C" void dd_newton_raphson(
int n,
double * v,
double * diag,
44 double * ldiag,
double * yy);
54 int& no_converge_flag)
78 cerr <<
"Illegal value for hesstype here" <<
endl;
97 output_stream <<
"Newton raphson " << ii <<
" ";
111 if (ierr && no_converge_flag ==0)
133 #if defined(USE_DD_STUFF)
138 cerr <<
"high precision Newton Raphson not implemented" <<
endl;
143 if (f_from_1< pfmin->lapprox->fmc1.fbest)
152 cout <<
"error not used" <<
endl;
195 # if defined(USE_ATLAS)
221 cerr <<
"matrix not pos definite in sparse choleski" <<
endl;
231 ofstream ofs3(
"inner-eigvectors");
232 ofs3 <<
"eigenvalues and eigenvectors " <<
endl;
235 ofs3 <<
"eigenvectors" <<
endl;
239 ofs3 << setw(4) << i <<
" "
240 <<
setshowpoint() << setw(14) << setprecision(10) << v(i)
261 double maxg_old=maxg;
277 for (
int i=1;i<=
usize;i++)
284 for (
int i=1;i<=
usize;i++)
300 if (no_converge_flag)
360 int no_converge_flag=0;
372 f_from_1=inner_optimization_banded(
x,pfmin,
376 if (sparse_hessian_flag==0)
378 for (i=1;i<=xsize;i++) { y(i)=
x(i); }
379 for (i=1;i<=usize;i++) { y(i+xsize)=uhat(i); }
383 for (i=1;i<=xsize;i++) {
value(y(i))=
x(i); }
384 for (i=1;i<=usize;i++) {
value(y(i+xsize))=uhat(i); }
390 do_newton_raphson_banded(pfmin,f_from_1,no_converge_flag);
394 do_newton_raphson_state_space(pfmin,f_from_1,no_converge_flag);
400 if (sparse_hessian_flag==0)
402 for (i=1;i<=usize;i++) { y(i+xsize)=uhat(i); }
406 for (i=1;i<=usize;i++) {
value(y(i+xsize))=uhat(i); }
412 while(no_converge_flag);
417 hs_symbolic & ssymb=*(pmin->lapprox->sparse_symbolic2);
420 do_newton_raphson_banded(pfmin,f_from_1,no_converge_flag);
424 if (pmin->lapprox->sparse_hessian_flag==0)
428 cerr <<
"Block diagonal Hessian is unallocated" <<
endl;
429 cerr <<
" Try -shess command line option, perhaps." <<
endl;
442 f+=0.5*
ln_det(*(pmin->lapprox->sparse_triplet2),ssymb);
447 xadjoint.initialize();
448 uadjoint.initialize();
453 else if (hesstype==4)
456 block_diagonal_flag=2;
457 used_flags.initialize();
464 sparse_triplet2->initialize();
474 if (num_importance_samples==0)
479 *bHessadjoint,pfmin);
481 else if (hesstype==4)
489 cerr <<
"Error in hesstype" <<
endl;
500 *bHessadjoint,pfmin);
502 else if (hesstype==4)
504 if (pmin->lapprox->sparse_hessian_flag==0)
513 cerr <<
"Error in hesstype" <<
endl;
530 else if (hesstype==4)
532 if (sparse_hessian_flag==0)
538 sparse_triplet2->initialize();
543 cerr <<
"Illegal value for hesstype" <<
endl;
547 block_diagonal_flag=3;
548 local_dtemp.initialize();
553 sparse_count_adjoint=0;
560 if (pHess_non_quadprior_part)
562 if (pHess_non_quadprior_part->indexmax() != Hess.indexmax())
564 delete pHess_non_quadprior_part;
565 pHess_non_quadprior_part=0;
568 if (!pHess_non_quadprior_part)
570 pHess_non_quadprior_part=
new dmatrix(1,usize,1,usize);
571 if (!pHess_non_quadprior_part)
573 cerr <<
"Error allocating memory for Hesssian part" <<
endl;
577 (*pHess_non_quadprior_part)=Hess;
580 block_diagonal_flag=0;
587 block_diagonal_flag=0;
589 assert(nvar <= INT_MAX);
614 dvector sscale=scale(1,Dux(1).indexmax());
615 for (i=1;i<=usize;i++)
619 local_dtemp=
elem_prod(local_dtemp,sscale);
629 for (i=1;i<=xsize;i++)
631 xadjoint(i)+=local_dtemp(i);
642 if (bHess_pd_flag==0)
644 xadjoint -=
solve(*bHess,uadjoint)*Dux;
647 else if (hesstype==4)
649 if (sparse_hessian_flag)
651 if (!sparse_triplet2 || !sparse_symbolic2)
653 throw std::bad_alloc();
658 dvector tmp=
solve(*sparse_triplet2,uadjoint,*sparse_symbolic2)*Dux;
664 xadjoint -=
solve(Hess,uadjoint)*Dux;
668 if (bHess_pd_flag==0)
break;
696 int num_fixed_effects=0;
702 ivector lfe_index(1, (
int)funnel_init_var::num_active_parameters);
705 for (
int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
707 int listi1 = *(plisti->
get_v() + 1);
710 lre_index(++num_local_re)=i;
714 lfe_index(++num_fixed_effects)=i;
719 if (num_local_re > 0)
724 for (
int i=1;i<=num_local_re;i++)
726 int lrei=lre_index(i);
727 int i1=list(lrei,1)-
xsize;
729 for (
int j=1;j<=num_local_re;j++)
731 int lrej=lre_index(j);
732 int j1=list(lrej,1)-
xsize;
734 if (i1>=j1) (*bHess)(i1,j1)+=locy(i2).u_bar[j2-1];
741 int* plre_indexi = lre_index.
get_v() + 1;
742 for (
int i=1;i<=num_local_re;i++)
744 int lrei = *plre_indexi;
747 int i2 = *(plisti->
get_v() + 2);
750 int* plre_indexj = lre_index.
get_v() + 1;
751 for (
int j=1;j<=num_local_re;j++)
753 int lrej = *plre_indexj;
756 int j2 = *(plistj->
get_v() + 2);
758 *(pHessi1->
get_v() + j1) += locy(i2).u_bar[j2-1];
767 for (
int i=1;i<=num_local_re;i++)
769 int lrei=lre_index(i);
770 int i1=list(lrei,1)-
xsize;
772 for (
int j=1;j<=num_local_re;j++)
774 int lrej=lre_index(j);
775 int j1=list(lrej,1)-
xsize;
782 +=locy(i2).u_bar[j2-1];
789 cerr <<
"illegal value for hesstype" <<
endl;
793 for (
int i=1;i<=num_local_re;i++)
795 int lrei=lre_index(i);
811 funnel_init_var::num_active_parameters=0;
849 (*re_objective_function_value::pobjfun)=0;
912 ivector lfe_index(1,(
int)funnel_init_var::num_active_parameters);
915 for (
int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
930 int* plfe_indexj = lfe_index.
get_v() + 1;
931 for (
int j=1;j<=xs;j++)
933 int lfe_indexj = lfe_index(j);
934 int* plistlfe_indexj = list(lfe_indexj).get_v();
935 int j1 = *(plistlfe_indexj + 1);
936 int j2 = *(plistlfe_indexj + 2);
947 for (
int i=1;i<=us;i++)
949 int i1=list(lre_index(i),1)-
xsize;
950 int i2=list(lre_index(i),2);
951 for (
int j=1;j<=us;j++)
953 int j1=list(lre_index(j),1)-
xsize;
954 int j2=list(lre_index(j),2);
955 if (i1>=j1) (*bHess)(i1,j1)+=locy(i2).u_bar[j2-1];
963 for (
int i=1;i<=us;i++)
965 int i1=list(lre_index(i),1)-
xsize;
966 int i2=list(lre_index(i),2);
967 for (
int j=1;j<=us;j++)
969 int j1=list(lre_index(j),1)-
xsize;
970 int j2=list(lre_index(j),2);
971 Hess(i1,j1)+=locy(i2).u_bar[j2-1];
977 for (
int i=1;i<=us;i++)
979 int i1=list(lre_index(i),1)-
xsize;
980 int i2=list(lre_index(i),2);
981 for (
int j=1;j<=us;j++)
983 int j1=list(lre_index(j),1)-
xsize;
984 int j2=list(lre_index(j),2);
990 +=locy(i2).u_bar[j2-1];
997 for (
int i=1;i<=us;i++)
999 int i1=list(lre_index(i),1)-
xsize;
1000 int i2=list(lre_index(i),2);
1004 for (
int i=1;i<=us;i++)
1006 int i1=list(lre_index(i),1)-
xsize;
1007 int i2=list(lre_index(i),2);
1008 for (
int j=1;j<=xs;j++)
1010 int j1=list(lfe_index(j),1);
1011 int j2=list(lfe_index(j),2);
1012 Dux(i1,j1)+=locy(i2).u_bar[j2-1];
1024 funnel_init_var::num_active_parameters=0;
1041 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
1042 const int xs = [](
unsigned int size)->
int
1044 assert(size <= INT_MAX);
1045 return static_cast<int>(size);
1047 const int us = [](
unsigned int size)->
int
1049 assert(size <= INT_MAX);
1050 return static_cast<int>(size);
1053 const int xs =
static_cast<int>(x.
size());
1054 const int us =
static_cast<int>(u0.
size());
1057 int nvar = xs + us + ((bw + 1) * (2 * us - bw))/2;
1070 for (
int i=1;i<=us;i++)
1072 int jmin=
admax(1,i-bw+1);
1073 for (
int j=jmin;j<=i;j++)
1081 for (
int i=1;i<=us;i++)
1083 int jmin=
admax(1,i-bw+1);
1084 for (
int j=jmin;j<=i;j++)
1085 vHess(i,j)=vy(ii++);
1106 ofs << ev << endl << endl << evecs <<
endl;
1113 cout <<
"Choleski failed" <<
endl;
1118 const double ltp=0.5*
log(2.0*
PI);
1125 for (
int i=1;i<=xs;i++)
1126 xadjoint(i)=g(ii++);
1127 for (
int i=1;i<=us;i++)
1128 uadjoint(i)=g(ii++);
1129 for (
int i=1;i<=us;i++)
1131 int jmin=
admax(1,i-bw+1);
1132 for (
int j=jmin;j<=i;j++)
1133 bHessadjoint(i,j)=g(ii++);
1156 double newfbest = 0.0;
1160 double lastval=oldfbest;
1177 for (
int i=mmin;i<=mmax;i++)
1193 if (have_value && newval>newfbest)
1203 if (newval>lastval && !have_value)
1211 if (newval<newfbest)
1228 cerr <<
"can't improve function value in trust region calculations"
static int straight_through_flag
Description not yet available.
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
laplace_approximation_calculator * lapprox
void initialize(void)
Description not yet available.
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
dcompressed_triplet * sparse_triplet2
static void set_yes_derivatives(void)
dvector banded_calculations(const dvector &_x, const double &_f, function_minimizer *pfmin)
Description not yet available.
void do_newton_raphson_banded(function_minimizer *pmin, double, int &)
Description not yet available.
df1b2_gradlist * f1b2gradlist
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
void set_dependent_variable(const df1b2variable &_x)
Description not yet available.
static void set_no_derivatives(void)
double calculate_importance_sample(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
function_minimizer * pmin
Description not yet available.
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Description not yet available.
static void set_active_random_effects(void)
dvector atlas_solve_spd(const dmatrix &M, const dvector &x)
static void get_Lxu_contribution(dmatrix &)
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Vector of double precision numbers.
int indexmin() const
Get minimum valid index.
static int separable_flag
static void get_cgradient_contribution(dvector, int)
Description not yet available.
void val(const adstring &s, int &v, int &code)
static int sparse_hessian_flag
static int num_inactive_vars
double calculate_importance_sample_shess(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
hs_symbolic * sparse_symbolic2
dvector evaluate_function_with_quadprior(const dvector &x, int usize, function_minimizer *pfmin)
Description not yet available.
static void get_cHessian_contribution_from_vHessian(dmatrix, int)
Description not yet available.
df1_two_variable fabs(const df1_two_variable &x)
double calculate_laplace_approximation(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
static int where_are_we_flag
void initialize(void)
Description not yet available.
static dvariable reset(const dvar_vector &x)
void do_separable_stuff_laplace_approximation_banded(df1b2variable &)
Description not yet available.
double gmax
maximum gradient
Description not yet available.
void gradcalc(int nvar, const dvector &g)
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
banded_symmetric_dmatrix * bHess
void deallocate(void)
If no other copies exist, free df1b2variable::ptr.
void initialize(void)
Description not yet available.
double evaluate_function(const dvector &x, function_minimizer *pfmin)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
void check_for_need_to_reallocate(int ip)
Does Nothing.
prescientific setscientific(void)
Description not yet available.
static laplace_approximation_calculator * lapprox
dvector get_uhat_quasi_newton(const dvector &x, function_minimizer *pfmin)
Description not yet available.
prnstream & endl(prnstream &)
dmatrix eigenvectors(const banded_symmetric_dmatrix &_SS, const dvector &_e)
Description not yet available.
Description not yet available.
Array of integers(int) with indexes from index_min to indexmax.
dvector solve_trans(const lower_triangular_dmatrix &M, const dvector &y)
Description not yet available.
static void set_active_only_random_effects(void)
double inner_optimization_banded(dvector &x, function_minimizer *pfmin, int &no_converge_flag)
Description not yet available.
void initialize(void)
Description not yet available.
static unsigned int num_active_parameters
int indexmax() const
Get maximum valid index.
preshowpoint setshowpoint(void)
Description not yet available.
static objective_function_value * pobjfun
Description not yet available.
static void xinit(const dvector &x)
Description not yet available.
Description not yet available.
dvector get_newton_raphson_info_banded(function_minimizer *pmin)
Description not yet available.
void initialize(void)
Initialze all elements of dvector to zero.
void do_separable_stuff_newton_raphson_banded(df1b2variable &)
Description not yet available.
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
dvector banded_calculations_trust_region_approach(const dvector &_uhat, function_minimizer *pmin)
Description not yet available.
unsigned int size() const
Get number of elements in array.
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument ("string").
std::ostream & get_output_stream()
void initialize(void)
Description not yet available.
dmatrix make_dmatrix(dcompressed_triplet &M)
Description not yet available.
Description not yet available.
virtual void AD_uf_outer()
double ln_det_choleski(const banded_symmetric_dmatrix &MM, int &ierr)
static int set_index(void)
dvector get_uhat_quasi_newton_qd(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Description not yet available.
Description not yet available.
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
virtual void user_function()
static void get_cHessian_contribution(dmatrix, int)
Description not yet available.
void safe_allocate(int ncl, int ncu)
Description not yet available.
static int get_num_quadratic_prior(void)
dvector value(const df1_one_vector &v)
static int stddev_scale(const dvector &d, const dvector &x)
static unsigned int num_vars
static init_df1b2vector * py
dvector get_uhat_lm_newton(const dvector &x, function_minimizer *pfmin)
Description not yet available.
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static int get_num_quadratic_prior(void)
void reset(void)
Description not yet available.
Fundamental data type for reverse mode automatic differentiation.
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dmatrix choleski_decomp_positive(const dmatrix &MM, double bound)
Description not yet available.
Description not yet available.
void df1b2_gradcalc1(void)
Description not yet available.
static void set_inactive_only_random_effects(void)
int bandwidth(void) const
static int in_qp_calculations