24 Tracer tr(
"FindMaximum2::Fit");
25 enum State {Start, Restart, Continue, Interpolate, Extrapolate,
27 State TheState = Start;
28 Real z,w,x,x2,g,l1,l2,l3,d1,d2=0,d3;
30 int np = Theta.
Nrows();
34 Theta1 = Theta; HP = 0.0; g = 0.0;
44 tr.
ReName(
"FindMaximum2::Fit/Start");
45 Value(Theta1,
true, l1, oorg);
49 tr.
ReName(
"FindMaximum2::Fit/ReStart");
50 conv = NextPoint(H1, d1);
51 if (conv) { TheState = Convergence;
break; }
52 if (counter++ > n_it) { TheState = Fail;
break; }
55 H3 = H1 * z; K = (H3 - HP) * g; HP = H3;
57 if ( g == 0.0 ) K1 = 0.0;
else K1 = K * 0.2 + K1 * 0.6;
63 tr.
ReName(
"FindMaximum2::Fit/Continue");
64 Theta2 = Theta1 + H1 +
K;
65 Value(Theta2,
false, l2, oorg);
66 if (counter++ > n_it) { TheState = Fail;
break; }
69 H1 *= 0.5; K *= 0.25; d1 *= 0.5; g *= 2.0;
70 TheState = Continue;
break;
72 d2 = LastDerivative(H1 + K * 2.0);
75 tr.
ReName(
"FindMaximum2::Fit/Interpolate");
76 z = d1 + d2 - 3.0 * (l2 - l1);
78 if (w < 0.0) { TheState = Extrapolate;
break; }
80 if (1.5 * w + d1 < 0.0)
81 { TheState = Extrapolate;
break; }
82 if (d2 > 0.0 && l2 > l1 && w > 0.0)
83 { TheState = Extrapolate;
break; }
84 x = d1 / (w + d1); x2 = x * x; g /= x;
85 Theta3 = Theta1 + H1 * x + K * x2;
86 Value(Theta3,
true, l3, oorg);
87 if (counter++ > n_it) { TheState = Fail;
break; }
91 { x *= 0.5; x2 = x*x; g *= 2.0; d1 *= x; H1 *= x; K *= x2; }
94 x = 0.5 * (x-1.0); x2 = x*x; Theta1 = Theta2;
95 H1 = (H1 + K * 2.0) * x;
96 K *= x2; g = 0.0; d1 = x * d2; l1 = l2;
98 TheState = Continue;
break;
101 if (l3 >= l1 && l3 >= l2)
102 { Theta1 = Theta3; l1 = l3; TheState = Restart;
break; }
104 d3 = LastDerivative(H1 + K * 2.0);
106 { H1 *= x; K *= x2; Theta2 = Theta3; d1 *= x; d2 = d3*x; }
109 Theta1 = Theta2; Theta2 = Theta3;
110 x -= 1.0; x2 = x*x; g = 0.0; H1 = (H1 + K * 2.0) * x;
111 K *= x2; l1 = l2; l2 = l3; d1 = x*d2; d2 = x*d3;
112 if (d1 <= 0.0) { TheState = Start;
break; }
114 TheState = Interpolate;
break;
117 tr.
ReName(
"FindMaximum2::Fit/Extrapolate");
118 Theta1 = Theta2; g = 0.0; K *= 4.0; H1 = (H1 * 2.0 +
K);
119 d1 = 2.0 * d2; l1 = l2;
120 TheState = Continue;
break;
126 Theta = Theta1;
return;
136 Tracer tr(
"NonLinearLeastSquares::Value");
137 Y.resize(n_obs); X.resize(n_obs,n_param);
139 Pred.Set(Parameters);
140 if (!Pred.IsValid()) { oorg=
true;
return; }
141 for (
int i=1; i<=n_obs; i++)
144 X.Row(i) = Pred.Derivatives();
146 if (!Pred.IsValid()) { oorg=
true;
return; }
147 Y = *DataPointer - Y;
Real ssq = Y.SumSquare();
148 errorvar = ssq / (n_obs - n_param);
150 cout << setw(15) << setprecision(10) <<
" " << errorvar;
152 oorg =
false; v = -0.5 * ssq;
157 Tracer tr(
"NonLinearLeastSquares::NextPoint");
159 test = M.SumSquare();
160 cout <<
" " << setw(15) << setprecision(10)
161 << test <<
" " << Y.SumSquare() / (n_obs - n_param);
163 if (test < errorvar * criterion)
return true;
168 {
return (Derivs * H).AsScalar(); }
173 Tracer tr(
"NonLinearLeastSquares::Fit");
174 n_param = Parameters.
Nrows(); n_obs = Data.
Nrows();
177 cout <<
"\nConverged" << endl;
182 if (Covariance.Nrows()==0)
185 Covariance << UI * UI.
t() * errorvar;
187 for (
int i = 1; i<=n_param; i++) SE(i) = sqrt(SE(i));
192 { MakeCovariance(); SEX = SE.
AsColumn(); }
195 { MakeCovariance(); Corr << SE.
i() * Covariance * SE.
i(); }
200 for (
int i = 1; i<=n_obs; i++) Hat(i) = X.
Row(i).
SumSquare();
209 Tracer tr(
"MLE_D_FI::Value");
210 if (!LL.IsValid(Parameters,wg)) { oorg=
true;
return; }
211 v = LL.LogLikelihood();
212 if (!LL.IsValid()) { oorg=
true;
return; }
214 cout << setw(20) << setprecision(10) << v;
216 Derivs = LL.Derivatives();
221 Tracer tr(
"MLE_D_FI::NextPoint");
225 Adj = LT.
t().
i() * Adj1;
227 cout <<
" " << setw(20) << setprecision(10) <<
test;
228 return (test < Criterion);
232 {
return (Derivs.t() * H).AsScalar(); }
236 Tracer tr(
"MLE_D_FI::Fit");
238 cout <<
"\nConverged" << endl;
243 if (Covariance.Nrows()==0)
246 Covariance << LTI.
t() * LTI;
248 int n = Covariance.
Nrows();
249 for (
int i=1; i <= n; i++) SE(i) = sqrt(SE(i));
254 { MakeCovariance(); SEX = SE.
AsColumn(); }
257 { MakeCovariance(); Corr << SE.
i() * Covariance * SE.
i(); }
void GetCorrelations(SymmetricMatrix &)
Miscellaneous exception (details in character string).
Basic data element used in Config class.
bool NextPoint(ColumnVector &, Real &)
void Fit(const ColumnVector &, ColumnVector &)
Real SumSquare(const BaseMatrix &B)
void GetCorrelations(SymmetricMatrix &)
void GetStandardErrors(ColumnVector &)
TransposedMatrix t() const
void Fit(ColumnVector &, int)
void Fit(ColumnVector &Parameters)
ReturnMatrix Cholesky(const SymmetricMatrix &S)
void QRZ(Matrix &X, UpperTriangularMatrix &U)
bool NextPoint(ColumnVector &, Real &)
void Value(const ColumnVector &, bool, Real &, bool &)
GetSubMatrix Row(int f) const
void Value(const ColumnVector &, bool, Real &, bool &)
Real LastDerivative(const ColumnVector &)
Covergence failure exception.
ColedMatrix AsColumn() const
void GetStandardErrors(ColumnVector &)
Real LastDerivative(const ColumnVector &)
struct Data Data
Basic data element used in Config class.
void ReName(const char *)
void GetHatDiagonal(DiagonalMatrix &) const