23 Tracer tr(
"FindMaximum2::Fit");
24 enum State {Start, Restart, Continue, Interpolate, Extrapolate,
26 State TheState = Start;
27 Real z,w,
x,x2,g,l1,l2,l3,d1,d2=0,d3;
29 int np = Theta.
Nrows();
33 Theta1 = Theta; HP = 0.0; g = 0.0;
43 tr.
ReName(
"FindMaximum2::Fit/Start");
44 Value(Theta1,
true, l1, oorg);
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; }
54 H3 = H1 * z; K = (H3 - HP) * g; HP = H3;
56 if (g==0.0) K1 = 0.0;
else K1 = K * 0.2 + K1 * 0.6;
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; }
68 H1 *= 0.5; K *= 0.25; d1 *= 0.5; g *= 2.0;
69 TheState = Continue;
break;
71 d2 = LastDerivative(H1 + K * 2.0);
74 tr.
ReName(
"FindMaximum2::Fit/Interpolate");
75 z = d1 + d2 - 3.0 * (l2 - l1);
77 if (w < 0.0) { TheState = Extrapolate;
break; }
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; }
90 {
x *= 0.5; x2 =
x*
x; g *= 2.0; d1 *=
x; H1 *=
x; K *= x2; }
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;
97 TheState = Continue;
break;
100 if (l3 >= l1 && l3 >= l2)
101 { Theta1 = Theta3; l1 = l3; TheState = Restart;
break; }
103 d3 = LastDerivative(H1 + K * 2.0);
105 { H1 *=
x; K *= x2; Theta2 = Theta3; d1 *=
x; d2 = d3*
x; }
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; }
113 TheState = Interpolate;
break;
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;
125 Theta = Theta1;
return;
135 Tracer tr(
"NonLinearLeastSquares::Value");
136 Y.ReSize(n_obs); X.ReSize(n_obs,n_param);
138 Pred.Set(Parameters);
139 if (!Pred.IsValid()) { oorg=
true;
return; }
140 for (
int i=1; i<=n_obs; i++)
143 X.Row(i) = Pred.Derivatives();
145 if (!Pred.IsValid()) { oorg=
true;
return; }
146 Y = *DataPointer - Y;
Real ssq = Y.SumSquare();
147 errorvar = ssq / (n_obs - n_param);
149 cout << setw(15) << setprecision(10) <<
" " << errorvar;
151 oorg =
false; v = -0.5 * ssq;
156 Tracer tr(
"NonLinearLeastSquares::NextPoint");
158 test = M.SumSquare();
159 cout <<
" " << setw(15) << setprecision(10)
160 << test <<
" " << Y.SumSquare() / (n_obs - n_param);
162 if (test < errorvar * criterion)
return true;
167 {
return (Derivs * H).AsScalar(); }
172 Tracer tr(
"NonLinearLeastSquares::Fit");
173 n_param = Parameters.
Nrows(); n_obs = Data.
Nrows();
176 cout <<
"\nConverged" << endl;
181 if (Covariance.Nrows()==0)
184 Covariance << UI * UI.
t() * errorvar;
186 for (
int i = 1; i<=n_param; i++) SE(i) = sqrt(SE(i));
191 { MakeCovariance(); SEX = SE.
AsColumn(); }
194 { MakeCovariance(); Corr << SE.
i() * Covariance * SE.
i(); }
199 for (
int i = 1; i<=n_obs; i++) Hat(i) = X.
Row(i).
SumSquare();
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; }
213 cout << setw(20) << setprecision(10) << v;
215 Derivs = LL.Derivatives();
220 Tracer tr(
"MLE_D_FI::NextPoint");
224 Adj = LT.
t().
i() * Adj1;
226 cout <<
" " << setw(20) << setprecision(10) << test;
227 return (test < Criterion);
231 {
return (Derivs.t() * H).AsScalar(); }
235 Tracer tr(
"MLE_D_FI::Fit");
237 cout <<
"\nConverged" << endl;
242 if (Covariance.Nrows()==0)
245 Covariance << LTI.
t() * LTI;
247 int n = Covariance.Nrows();
248 for (
int i=1; i <=
n; i++) SE(i) = sqrt(SE(i));
253 { MakeCovariance(); SEX = SE.
AsColumn(); }
256 { MakeCovariance(); Corr << SE.
i() * Covariance * SE.
i(); }
void GetCorrelations(SymmetricMatrix &)
void GetHatDiagonal(DiagonalMatrix &) const
ColedMatrix AsColumn() const
bool NextPoint(ColumnVector &, Real &)
void Fit(const ColumnVector &, ColumnVector &)
void GetCorrelations(SymmetricMatrix &)
os2<< "> n<< " > nendobj n
void GetStandardErrors(ColumnVector &)
void Fit(ColumnVector &, int)
void Fit(ColumnVector &Parameters)
GetSubMatrix Row(int) const
ReturnMatrix Cholesky(const SymmetricMatrix &)
virtual Real SumSquare() const
bool NextPoint(ColumnVector &, Real &)
void Value(const ColumnVector &, bool, Real &, bool &)
void Value(const ColumnVector &, bool, Real &, bool &)
Real LastDerivative(const ColumnVector &)
void GetStandardErrors(ColumnVector &)
void QRZ(Matrix &, UpperTriangularMatrix &)
TransposedMatrix t() const
Real LastDerivative(const ColumnVector &)
void ReName(const char *)
Real SumSquare(const BaseMatrix &B)