OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
newmatnl.cpp
Go to the documentation of this file.
1 //$$ newmatnl.cpp Non-linear optimisation
2 
3 // Copyright (C) 1993,4,5,6: R B Davies
4 
5 
6 #define WANT_MATH
7 #define WANT_STREAM
8 
9 #include <cmath>
10 #include <iostream>
11 #include <iomanip>
12 #include <ossim/matrix/newmatap.h>
13 #include <ossim/matrix/newmatnl.h>
14 
15 #ifdef use_namespace
16 namespace NEWMAT {
17 #endif
18 
19 using namespace std;
20 
21 void FindMaximum2::Fit(ColumnVector& Theta, int n_it)
22 {
23  Tracer tr("FindMaximum2::Fit");
24  enum State {Start, Restart, Continue, Interpolate, Extrapolate,
25  Fail, Convergence};
26  State TheState = Start;
27  Real z,w,x,x2,g,l1,l2,l3,d1,d2=0,d3;
28  ColumnVector Theta1, Theta2, Theta3;
29  int np = Theta.Nrows();
30  ColumnVector H1(np), H3, HP(np), K, K1(np);
31  bool oorg, conv;
32  int counter = 0;
33  Theta1 = Theta; HP = 0.0; g = 0.0;
34 
35  // This is really a set of gotos and labels, but they do not work
36  // correctly in AT&T C++ and Sun 4.01 C++.
37 
38  for(;;)
39  {
40  switch (TheState)
41  {
42  case Start:
43  tr.ReName("FindMaximum2::Fit/Start");
44  Value(Theta1, true, l1, oorg);
45  if (oorg) Throw(ProgramException("invalid starting value\n"));
46 
47  case Restart:
48  tr.ReName("FindMaximum2::Fit/ReStart");
49  conv = NextPoint(H1, d1);
50  if (conv) { TheState = Convergence; break; }
51  if (counter++ > n_it) { TheState = Fail; break; }
52 
53  z = 1.0 / sqrt(d1);
54  H3 = H1 * z; K = (H3 - HP) * g; HP = H3;
55  g = 0.0; // de-activate to use curved projection
56  if (g==0.0) K1 = 0.0; else K1 = K * 0.2 + K1 * 0.6;
57  // (K - K1) * alpha + K1 * (1 - alpha)
58  // = K * alpha + K1 * (1 - 2 * alpha)
59  K = K1 * d1; g = z;
60 
61  case Continue:
62  tr.ReName("FindMaximum2::Fit/Continue");
63  Theta2 = Theta1 + H1 + K;
64  Value(Theta2, false, l2, oorg);
65  if (counter++ > n_it) { TheState = Fail; break; }
66  if (oorg)
67  {
68  H1 *= 0.5; K *= 0.25; d1 *= 0.5; g *= 2.0;
69  TheState = Continue; break;
70  }
71  d2 = LastDerivative(H1 + K * 2.0);
72 
73  case Interpolate:
74  tr.ReName("FindMaximum2::Fit/Interpolate");
75  z = d1 + d2 - 3.0 * (l2 - l1);
76  w = z * z - d1 * d2;
77  if (w < 0.0) { TheState = Extrapolate; break; }
78  w = z + sqrt(w);
79  if (1.5 * w + d1 < 0.0)
80  { TheState = Extrapolate; break; }
81  if (d2 > 0.0 && l2 > l1 && w > 0.0)
82  { TheState = Extrapolate; break; }
83  x = d1 / (w + d1); x2 = x * x; g /= x;
84  Theta3 = Theta1 + H1 * x + K * x2;
85  Value(Theta3, true, l3, oorg);
86  if (counter++ > n_it) { TheState = Fail; break; }
87  if (oorg)
88  {
89  if (x <= 1.0)
90  { x *= 0.5; x2 = x*x; g *= 2.0; d1 *= x; H1 *= x; K *= x2; }
91  else
92  {
93  x = 0.5 * (x-1.0); x2 = x*x; Theta1 = Theta2;
94  H1 = (H1 + K * 2.0) * x;
95  K *= x2; g = 0.0; d1 = x * d2; l1 = l2;
96  }
97  TheState = Continue; break;
98  }
99 
100  if (l3 >= l1 && l3 >= l2)
101  { Theta1 = Theta3; l1 = l3; TheState = Restart; break; }
102 
103  d3 = LastDerivative(H1 + K * 2.0);
104  if (l1 > l2)
105  { H1 *= x; K *= x2; Theta2 = Theta3; d1 *= x; d2 = d3*x; }
106  else
107  {
108  Theta1 = Theta2; Theta2 = Theta3;
109  x -= 1.0; x2 = x*x; g = 0.0; H1 = (H1 + K * 2.0) * x;
110  K *= x2; l1 = l2; l2 = l3; d1 = x*d2; d2 = x*d3;
111  if (d1 <= 0.0) { TheState = Start; break; }
112  }
113  TheState = Interpolate; break;
114 
115  case Extrapolate:
116  tr.ReName("FindMaximum2::Fit/Extrapolate");
117  Theta1 = Theta2; g = 0.0; K *= 4.0; H1 = (H1 * 2.0 + K);
118  d1 = 2.0 * d2; l1 = l2;
119  TheState = Continue; break;
120 
121  case Fail:
122  Throw(ConvergenceException(Theta));
123 
124  case Convergence:
125  Theta = Theta1; return;
126  }
127  }
128 }
129 
130 
131 
133  (const ColumnVector& Parameters, bool, Real& v, bool& oorg)
134 {
135  Tracer tr("NonLinearLeastSquares::Value");
136  Y.ReSize(n_obs); X.ReSize(n_obs,n_param);
137  // put the fitted values in Y, the derivatives in X.
138  Pred.Set(Parameters);
139  if (!Pred.IsValid()) { oorg=true; return; }
140  for (int i=1; i<=n_obs; i++)
141  {
142  Y(i) = Pred(i);
143  X.Row(i) = Pred.Derivatives();
144  }
145  if (!Pred.IsValid()) { oorg=true; return; } // check afterwards as well
146  Y = *DataPointer - Y; Real ssq = Y.SumSquare();
147  errorvar = ssq / (n_obs - n_param);
148  cout << endl;
149  cout << setw(15) << setprecision(10) << " " << errorvar;
150  Derivs = Y.t() * X; // get the derivative and stash it
151  oorg = false; v = -0.5 * ssq;
152 }
153 
155 {
156  Tracer tr("NonLinearLeastSquares::NextPoint");
157  QRZ(X, U); QRZ(X, Y, M); // do the QR decomposition
158  test = M.SumSquare();
159  cout << " " << setw(15) << setprecision(10)
160  << test << " " << Y.SumSquare() / (n_obs - n_param);
161  Adj = U.i() * M;
162  if (test < errorvar * criterion) return true;
163  else return false;
164 }
165 
167 { return (Derivs * H).AsScalar(); }
168 
170  ColumnVector& Parameters)
171 {
172  Tracer tr("NonLinearLeastSquares::Fit");
173  n_param = Parameters.Nrows(); n_obs = Data.Nrows();
174  DataPointer = &Data;
175  FindMaximum2::Fit(Parameters, Lim);
176  cout << "\nConverged" << endl;
177 }
178 
180 {
181  if (Covariance.Nrows()==0)
182  {
183  UpperTriangularMatrix UI = U.i();
184  Covariance << UI * UI.t() * errorvar;
185  SE << Covariance; // get diagonals
186  for (int i = 1; i<=n_param; i++) SE(i) = sqrt(SE(i));
187  }
188 }
189 
191  { MakeCovariance(); SEX = SE.AsColumn(); }
192 
194  { MakeCovariance(); Corr << SE.i() * Covariance * SE.i(); }
195 
197 {
198  Hat.ReSize(n_obs);
199  for (int i = 1; i<=n_obs; i++) Hat(i) = X.Row(i).SumSquare();
200 }
201 
202 
203 // the MLE_D_FI routines
204 
205 void MLE_D_FI::Value
206  (const ColumnVector& Parameters, bool wg, Real& v, bool& oorg)
207 {
208  Tracer tr("MLE_D_FI::Value");
209  if (!LL.IsValid(Parameters,wg)) { oorg=true; return; }
210  v = LL.LogLikelihood();
211  if (!LL.IsValid()) { oorg=true; return; } // check validity again
212  cout << endl;
213  cout << setw(20) << setprecision(10) << v;
214  oorg = false;
215  Derivs = LL.Derivatives(); // Get derivatives
216 }
217 
219 {
220  Tracer tr("MLE_D_FI::NextPoint");
221  SymmetricMatrix FI = LL.FI();
222  LT = Cholesky(FI);
223  ColumnVector Adj1 = LT.i() * Derivs;
224  Adj = LT.t().i() * Adj1;
225  test = SumSquare(Adj1);
226  cout << " " << setw(20) << setprecision(10) << test;
227  return (test < Criterion);
228 }
229 
231 { return (Derivs.t() * H).AsScalar(); }
232 
233 void MLE_D_FI::Fit(ColumnVector& Parameters)
234 {
235  Tracer tr("MLE_D_FI::Fit");
236  FindMaximum2::Fit(Parameters,Lim);
237  cout << "\nConverged" << endl;
238 }
239 
241 {
242  if (Covariance.Nrows()==0)
243  {
244  LowerTriangularMatrix LTI = LT.i();
245  Covariance << LTI.t() * LTI;
246  SE << Covariance; // get diagonal
247  int n = Covariance.Nrows();
248  for (int i=1; i <= n; i++) SE(i) = sqrt(SE(i));
249  }
250 }
251 
253 { MakeCovariance(); SEX = SE.AsColumn(); }
254 
256 { MakeCovariance(); Corr << SE.i() * Covariance * SE.i(); }
257 
258 
259 
260 #ifdef use_namespace
261 }
262 #endif
263 
ossim_uint32 x
double Real
Definition: include.h:57
void GetCorrelations(SymmetricMatrix &)
Definition: newmatnl.cpp:255
void GetHatDiagonal(DiagonalMatrix &) const
Definition: newmatnl.cpp:196
ColedMatrix AsColumn() const
Definition: newmat6.cpp:332
bool NextPoint(ColumnVector &, Real &)
Definition: newmatnl.cpp:154
InvertedMatrix i() const
Definition: newmat6.cpp:325
void Fit(const ColumnVector &, ColumnVector &)
Definition: newmatnl.cpp:169
void GetCorrelations(SymmetricMatrix &)
Definition: newmatnl.cpp:193
os2<< "> n<< " > nendobj n
void GetStandardErrors(ColumnVector &)
Definition: newmatnl.cpp:190
void Fit(ColumnVector &, int)
Definition: newmatnl.cpp:21
void Fit(ColumnVector &Parameters)
Definition: newmatnl.cpp:233
GetSubMatrix Row(int) const
Definition: submat.cpp:45
ReturnMatrix Cholesky(const SymmetricMatrix &)
Definition: cholesky.cpp:30
virtual Real SumSquare() const
Definition: newmat8.cpp:433
bool NextPoint(ColumnVector &, Real &)
Definition: newmatnl.cpp:218
void Value(const ColumnVector &, bool, Real &, bool &)
Definition: newmatnl.cpp:206
void ReSize(int)
Definition: newmat4.cpp:256
void Value(const ColumnVector &, bool, Real &, bool &)
Definition: newmatnl.cpp:133
Real LastDerivative(const ColumnVector &)
Definition: newmatnl.cpp:166
void MakeCovariance()
Definition: newmatnl.cpp:240
int Nrows() const
Definition: newmat.h:430
void GetStandardErrors(ColumnVector &)
Definition: newmatnl.cpp:252
void QRZ(Matrix &, UpperTriangularMatrix &)
Definition: hholder.cpp:111
TransposedMatrix t() const
Definition: newmat6.cpp:316
Real LastDerivative(const ColumnVector &)
Definition: newmatnl.cpp:230
void ReName(const char *)
Definition: myexcept.h:94
Real SumSquare(const BaseMatrix &B)
Definition: newmat.h:1755