ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nnewmod2.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
7 #include <admodel.h>
8 
10  const int underflow_flag, const dvector& xscale,
11  const double& _ln_det_proj_jac)
12 {
13  double& ln_det_proj_jac=(double&) _ln_det_proj_jac;
14  int ibreak=-1;
15  int sgn=0;
16  double lndet=0.0;
17  //char ch;
18  if (!underflow_flag)
19  {
20  uistream ifs("admodel.hes");
21  if (!ifs)
22  {
23  cerr << "Error opening file admodel.hes" << endl;
24  }
25  int nvar = 0;
26  ifs >> nvar;
27  //dmatrix S(1,nvar,1,nvar);
28  if (nvar > 0)
29  {
30  if (nvar != initial_params::nvarcalc())
31  {
32  cout << "the number of independent variables is wrong in admodel.hes"
33  << endl;
34  }
35  dmatrix p(1,nvar,1,nvar);
36  dmatrix p1(1,nvar-1,1,nvar);
37  dmatrix h(1,nvar,1,nvar);
38  dvector ss(1,nvar);
39  ifs >> h;
40  if (!ifs)
41  {
42  cerr << "Error reading the hessian from file admodel.hes" << endl;
43  }
44  dvector n=g/norm(g);
45  // project the standard basis vectors onto the tangent space
46  int i;
47  for (i=1;i<=nvar;i++)
48  {
49  p(i)=-n(i)*n;
50  p(i,i)+=1;
51  }
52 
53  for (i=1;i<=nvar;i++)
54  {
55  ss(i)=norm(p(i));
56  }
57  double minsize = min(ss);
58 
59  for (i=1;i<=nvar;i++)
60  {
61  if (ss(i) == minsize)
62  {
63  ibreak = i;
64  break;
65  }
66  p1(i)=p(i);
67  }
68 
69  int ii;
70  for (ii=i+1;ii<=nvar;ii++)
71  {
72  p1(ii-1)=p(ii);
73  }
74 
75  dmatrix tmpS(1,nvar-1,1,nvar-1);
76 
77  //for (ii=1;ii<=nvar-1;ii++)
78  //{
79  //for (i=1;i<=nvar;i++)
80  //{
81  //p1(ii,i)*=xscale(i);
82  //}
83  //}
84 
85  for (i=1;i<=nvar-1;i++)
86  {
87  tmpS(i,i)=p1(i)*p1(i);
88  for (int j=1;j<i;j++)
89  {
90  tmpS(i,j)=p1(i)*p1(j);
91  tmpS(j,i)=tmpS(i,j);
92  }
93  }
94  ln_det_proj_jac=ln_det(tmpS,sgn);
95 
96  // reset the p1 basis
97  for (i=1;i<=nvar;i++)
98  {
99  if (i==ibreak) break;
100  p1(i)=p(i);
101  }
102 
103  for (ii=i+1;ii<=nvar;ii++)
104  {
105  p1(ii-1)=p(ii);
106  }
107 
108  for (i=1;i<=nvar;i++)
109  {
110  for (int j=1;j<i;j++)
111  {
112  double tmp=(h(i,j)+h(j,i))/2.;
113  h(i,j)=tmp;
114  h(j,i)=tmp;
115  }
116  }
117 
118  // move to "model space"
119  for (i=1;i<=nvar;i++)
120  {
121  for (int j=1;j<=nvar;j++)
122  {
123  h(i,j)/=(xscale(i)*xscale(j));
124  }
125  }
126 
127  for (i=1;i<nvar;i++)
128  {
129  dvector tmp = h*p1(i);
130  tmpS(i,i)=tmp*p1(i);
131  for (int j=1;j<i;j++)
132  {
133  tmpS(i,j)=tmp*p1(j);
134  tmpS(j,i)=tmpS(i,j);
135  }
136  }
137  lndet=ln_det(tmpS,sgn);
138  }
139  if (sgn <= 0)
140  {
141  cerr << "Error restricted Hessian is not positive definite" << endl;
142  }
143  }
144  else
145  {
146  lndet=-50.0;
147  }
148  return lndet;
149 }
double projected_hess_determinant(const dvector &g, const int underflow_flag, const dvector &xscale, const double &ln_det_proj_jac)
Definition: nnewmod2.cpp:9
Vector of double precision numbers.
Definition: dvector.h:50
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
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
prnstream & endl(prnstream &)
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
Description not yet available.
Definition: fvar.hpp:2819
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
Description not yet available.
Definition: fvar.hpp:3516