ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
newmodm2.cpp
Go to the documentation of this file.
1 
8 #include <admodel.h>
9 #include <df1b2fun.h>
10 #include <adrndeff.h>
11 
12 #ifdef ISZERO
13  #undef ISZERO
14 #endif
15 #define ISZERO(d) ((d)==0.0)
16 
18  const int underflow_flag)
19 {
20  double lndet=0.0;
21  //double ld1=0.0;
22  //double ld2=0.0;
23  //char ch;
24  if (!underflow_flag)
25  {
26  int sgn=0;
27  adstring tmpstring="admodel.hes";
28  if (ad_comm::wd_flag)
29  tmpstring = ad_comm::adprogram_name + ".hes";
30  uistream ifs((char*)tmpstring);
31 
32  if (!ifs)
33  {
34  cerr << "Error opening file admodel.hes" << endl;
35  }
36  int nvar = 0;
37  ifs >> nvar;
38  dmatrix S(1,nvar,1,nvar);
39  if (nvar > 0)
40  {
41  if (nvar != initial_params::nvarcalc())
42  {
43  cout << "the number of independent variables is wrong in admodel.hes"
44  << endl;
45  }
46  dmatrix p(1,nvar,1,nvar);
47  //dmatrix p1(1,nvar-1,1,nvar);
48  dmatrix h(1,nvar,1,nvar);
49  //dmatrix gram(1,nvar-1,1,nvar-1);
50  dvector ss(1,nvar);
51  ifs >> h;
52  if (!ifs)
53  {
54  cerr << "Error reading the hessian from file admodel.hes" << endl;
55  }
56  dvector n=g/norm(g);
57  // project the standard basis vectors onto the tangent space
58  int i;
59  for (i=1;i<=nvar;i++)
60  {
61  p(i)=-n(i)*n;
62  p(i,i)+=1;
63  }
64  //cout << "p*n" << endl;
65  //cout << p*n << endl;
66  //cin >> ch;
67 
68  for (i=1;i<=nvar;i++)
69  {
70  ss(i)=norm(p(i));
71  }
72 
73  /*
74  double minsize=min(ss);
75  for (i=1;i<=nvar;i++)
76  {
77  if (ss(i)==minsize) break;
78  p1(i)=p(i);
79  }
80 
81  for (int ii=i+1;ii<=nvar;ii++)
82  {
83  p1(ii-1)=p(ii);
84  }
85  */
86 
87  for (i=1;i<=nvar;i++)
88  {
89  for (int j=1;j<i;j++)
90  {
91  double tmp=(h(i,j)+h(j,i))/2.;
92  h(i,j)=tmp;
93  h(j,i)=tmp;
94  }
95  }
96 
97  //cout << "A" << endl;
98  /*
99  for (i=1;i<nvar;i++)
100  {
101  dvector tmp = h*p1(i);
102  // cout << "aA" << endl;
103  for (int j=1;j<nvar;j++)
104  {
105  gram(i,j)=tmp*p1(j);
106  }
107  }
108  cout << "B" << endl;
109  ld1=ln_det(gram,sgn);
110  cout << "CX" << endl;
111  for (i=1;i<nvar;i++)
112  {
113  for (int j=1;j<nvar;j++)
114  {
115  gram(i,j)=p1(i)*p1(j);
116  }
117  if (norm(p1(i))==0) cout <<"norm p1 =0 " << endl;
118  }
119  // cout << "D" << endl;
120 
121  ld2=ln_det(gram,sgn);
122  // cout << "E" << endl;
123  */
124 
125  // cout << "eigs of h" << endl;
126  // cout << sort(eigenvalues(h)) << endl << endl;
127 
128  // cout << "F" << endl;
129  for (i=1;i<=nvar;i++)
130  {
131  dvector tmp=h*p(i);
132  for (int j=1;j<=i;j++)
133  {
134  S(i,j)=tmp*p(j)+n(i)*n(j);
135  }
136  }
137  // cout << "G" << endl;
138 
139  for (i=1;i<=nvar;i++)
140  {
141  for (int j=1;j<i;j++)
142  {
143  S(j,i)=S(i,j);
144  }
145  }
146  }
147  // cout << "eigs of S" << endl;
148  // cout << sort(eigenvalues(S)) << endl << endl;
149  lndet=ln_det(S,sgn);
150  // cout << "old det = " << lndet << endl;
151  // lndet=ld1-ld2;
152  // cout << "new det = " << lndet << " " << ld1 << " " << ld2 << endl;
153  //char ch;
154  //cin >> ch;
155  //double lndet2=0.;
156  if (sgn <= 0)
157  {
158  cerr << "Error restricted Hessian is not positive definite" << endl;
159  }
160  {
161  // cout << "doing S eigenvalues" << endl;
162  // dvector eig=eigenvalues(S);
163  // cout << "finished S eigenvalues" << endl;
164  //lndet2=sum(log(eig));
165  // cout << sort(eig) << endl << endl;
166  }
167  }
168  else
169  {
170  lndet=-50.0;
171  }
172  return lndet;
173 }
174 
176  const dvector& fg, const dvector& g)
177  {
178  independent_variables x(1,nvar);
179  // get the initial values into the x vector
181  //double f = 0.0;
182  dvariable vf=0.0;
185  if (lapprox)
186  {
187  if (lapprox->hesstype==2)
188  {
190  }
191  }
192  userfunction();
194  double f = value(vf);
195  gradcalc(nvar, g);
196 
197  vf=0.0;
200  if (lapprox)
201  {
202  if (lapprox->hesstype==2)
203  {
205  }
206  }
207  userfunction();
209  f = value(vf);
210  gradcalc(nvar, fg);
211 
213  double div=norm(g)*norm(fg);
214 
215  if (div==0.0)
216  cout << "0" << endl;
217  else
218  cout << g*fg/(norm(g)*norm(fg)) << endl;
219  }
220 
221 void function_minimizer::prof_minimize(int iprof, double sigma,
222  double new_value, const double& _fprof, const int underflow_flag,
223  double global_min, const double& _penalties, const double& _final_weight)
224  {
225  double& penalties=(double&) _penalties;
226  double& fprof=(double&) _fprof;
227  double& final_weight=(double&) _final_weight;
228  if (!underflow_flag)
229  {
230  int max_profile_phases=3;
231  int profile_phase=1;
233  while (profile_phase <= max_profile_phases)
234  {
235  {
236  int nvar=initial_params::nvarcalc(); // get the number of active
237  // parameters
238  dvector g(1,nvar);
239  independent_variables x(1,nvar);
240  // get the initial values into the x vector
242  fmm fmc(nvar);
243  fmc.maxfn= maxfn;
244  fmc.iprint= iprint;
245  fmc.crit = crit;
246  fmc.imax = imax;
247  fmc.dfn= dfn;
250  double f=0.0;
252  // set convergence criterion for this phase
253  if (!(!convergence_criteria))
254  {
257  fmc.crit=convergence_criteria(ind);
258  }
260  {
263  fmc.maxfn=int(maximum_function_evaluations(ind));
264  }
265  int itnsave=0;
266  //double weight=pow(50.0,profile_phase)/(sigma*sigma);
267  double weight = pow(120.0,profile_phase);
268  if (!ISZERO(sigma))
269  {
270  weight /= (sigma*sigma);
271  }
272  final_weight=weight;
273  while (fmc.ireturn>=0)
274  {
275  fmc.fmin(f,x,g);
276  double diff =
277  new_value-value(likeprof_params::likeprofptr[iprof]->variable());
278  if (fmc.itn>itnsave && diff < pow(.1,iprof)*sigma)
279  {
280  fmc.ifn=fmc.imax;
281  }
282  if (fmc.ireturn>0)
283  {
284  dvariable vf=0.0;
287  userfunction();
289  vf+=weight*square(new_value-tv);
291  f = value(vf);
292  gradcalc(nvar, g);
293  }
294  }
296  iexit=fmc.iexit;
297  ihflag=fmc.ihflag;
298  ihang=fmc.ihang;
300  quit_flag=fmc.quit_flag;
303  userfunction();
304  double tv=value(likeprof_params::likeprofptr[iprof]->variable());
306  penalties=weight*(new_value-tv)*(new_value-tv);
307  fprof+=penalties;
308  if (quit_flag=='Q') break;
309  if (!quit_flag || quit_flag == 'N')
310  {
311  profile_phase++;
312  }
313  }
314  }
315  }
316  else
317  {
318  fprof=global_min+20.0;
319  }
320  }
static likeprof_params * likeprofptr[500]
Definition: admodel.h:2257
double projected_hess_determinant(const dvector &g, const int underflow_flag, const dvector &xscale, const double &ln_det_proj_jac)
Definition: nnewmod2.cpp:9
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
double dfn
Definition: fvar.hpp:3187
double crit
Definition: fvar.hpp:3184
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int maxfn_flag
Definition: fvar.hpp:3194
void prof_minimize(int iprof, double sigma, double new_value, const double &fprof, const int underflow_flag, double global_min, const double &penalties, const double &final_weight)
Definition: newmodm2.cpp:221
long maxfn
Definition: fvar.hpp:3182
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
void fmin(const double &f, const dvector &x, const dvector &g)
Function fmin contains Quasi-Newton function minimizer with inexact line search using Wolfe condition...
Definition: newfmin.cpp:254
ADMB variable vector.
Definition: fvar.hpp:2172
static adstring adprogram_name
Definition: fvar.hpp:8860
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
virtual dvariable variable(void)=0
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
static int nvarcalc()
Definition: model.cpp:152
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
long imax
Definition: fvar.hpp:3186
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
static int current_phase
Definition: admodel.h:842
long ihang
Definition: fvar.hpp:3192
int scroll_flag
Definition: fvar.hpp:3193
static dvector maximum_function_evaluations
Definition: admodel.h:1917
static int max_number_phases
Definition: admodel.h:841
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
long ifn
Definition: fvar.hpp:3188
static void xinit(const dvector &x)
Definition: model.cpp:226
Functions and variables for function minimizer.
Definition: fvar.hpp:3290
Description not yet available.
Definition: fvar.hpp:2819
int quit_flag
Definition: fvar.hpp:3195
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
void get_particular_grad(int iprof, int nvar, const dvector &fg, const dvector &g)
Definition: newmodm2.cpp:175
long ihflag
Definition: fvar.hpp:3191
#define ISZERO(d)
Definition: newmodm2.cpp:15
double min_improve
Definition: fvar.hpp:3196
Description not yet available.
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
Description not yet available.
Definition: fvar.hpp:3516
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double min_improve
Definition: admodel.h:1900
virtual void userfunction(void)=0
long iprint
Definition: fvar.hpp:3183
long int itn
Definition: fvar.hpp:3301
static unsigned int wd_flag
Definition: fvar.hpp:8864
static dvector convergence_criteria
Definition: admodel.h:1915
int ireturn
Definition: fvar.hpp:3197
long iexit
Definition: fvar.hpp:3189
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
df1b2variable div(const df1b2variable &x, const df1b2variable &y)
Description not yet available.
Definition: df32fun.cpp:1282
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17