00001
00002
00003
00004
00005
00006 #ifndef WANT_MATH
00007 #define WANT_MATH
00008 #endif
00009
00010 #ifndef WANT_STREAM
00011 #define WANT_STREAM
00012 #endif
00013
00014 #include "newmatap.h"
00015 #include "newmatnl.h"
00016 #include <iomanip>
00017
00018 using namespace std;
00019
00020 #ifdef use_namespace
00021 namespace NEWMAT {
00022 #endif
00023
00024 void FindMaximum2::Fit(ColumnVector& Theta, int n_it)
00025 {
00026 Tracer tr("FindMaximum2::Fit");
00027 enum State {Start, Restart, Continue, Interpolate, Extrapolate,
00028 Fail, Convergence};
00029 State TheState = Start;
00030 Real z,w,x,x2,g,l1,l2,l3,d1,d2=0,d3;
00031 ColumnVector Theta1, Theta2, Theta3;
00032 int np = Theta.Nrows();
00033 ColumnVector H1(np), H3, HP(np), K, K1(np);
00034 bool oorg, conv;
00035 int counter = 0;
00036 Theta1 = Theta; HP = 0.0; g = 0.0;
00037
00038
00039
00040
00041 for(;;)
00042 {
00043 switch (TheState)
00044 {
00045 case Start:
00046 tr.ReName("FindMaximum2::Fit/Start");
00047 Value(Theta1, true, l1, oorg);
00048 if (oorg) Throw(ProgramException("invalid starting value\n"));
00049
00050 case Restart:
00051 tr.ReName("FindMaximum2::Fit/ReStart");
00052 conv = NextPoint(H1, d1);
00053 if (conv) { TheState = Convergence; break; }
00054 if (counter++ > n_it) { TheState = Fail; break; }
00055
00056 z = 1.0 / sqrt(d1);
00057 H3 = H1 * z; K = (H3 - HP) * g; HP = H3;
00058 g = 0.0;
00059 if (g==0.0) K1 = 0.0; else K1 = K * 0.2 + K1 * 0.6;
00060
00061
00062 K = K1 * d1; g = z;
00063
00064 case Continue:
00065 tr.ReName("FindMaximum2::Fit/Continue");
00066 Theta2 = Theta1 + H1 + K;
00067 Value(Theta2, false, l2, oorg);
00068 if (counter++ > n_it) { TheState = Fail; break; }
00069 if (oorg)
00070 {
00071 H1 *= 0.5; K *= 0.25; d1 *= 0.5; g *= 2.0;
00072 TheState = Continue; break;
00073 }
00074 d2 = LastDerivative(H1 + K * 2.0);
00075
00076 case Interpolate:
00077 tr.ReName("FindMaximum2::Fit/Interpolate");
00078 z = d1 + d2 - 3.0 * (l2 - l1);
00079 w = z * z - d1 * d2;
00080 if (w < 0.0) { TheState = Extrapolate; break; }
00081 w = z + sqrt(w);
00082 if (1.5 * w + d1 < 0.0)
00083 { TheState = Extrapolate; break; }
00084 if (d2 > 0.0 && l2 > l1 && w > 0.0)
00085 { TheState = Extrapolate; break; }
00086 x = d1 / (w + d1); x2 = x * x; g /= x;
00087 Theta3 = Theta1 + H1 * x + K * x2;
00088 Value(Theta3, true, l3, oorg);
00089 if (counter++ > n_it) { TheState = Fail; break; }
00090 if (oorg)
00091 {
00092 if (x <= 1.0)
00093 { x *= 0.5; x2 = x*x; g *= 2.0; d1 *= x; H1 *= x; K *= x2; }
00094 else
00095 {
00096 x = 0.5 * (x-1.0); x2 = x*x; Theta1 = Theta2;
00097 H1 = (H1 + K * 2.0) * x;
00098 K *= x2; g = 0.0; d1 = x * d2; l1 = l2;
00099 }
00100 TheState = Continue; break;
00101 }
00102
00103 if (l3 >= l1 && l3 >= l2)
00104 { Theta1 = Theta3; l1 = l3; TheState = Restart; break; }
00105
00106 d3 = LastDerivative(H1 + K * 2.0);
00107 if (l1 > l2)
00108 { H1 *= x; K *= x2; Theta2 = Theta3; d1 *= x; d2 = d3*x; }
00109 else
00110 {
00111 Theta1 = Theta2; Theta2 = Theta3;
00112 x -= 1.0; x2 = x*x; g = 0.0; H1 = (H1 + K * 2.0) * x;
00113 K *= x2; l1 = l2; l2 = l3; d1 = x*d2; d2 = x*d3;
00114 if (d1 <= 0.0) { TheState = Start; break; }
00115 }
00116 TheState = Interpolate; break;
00117
00118 case Extrapolate:
00119 tr.ReName("FindMaximum2::Fit/Extrapolate");
00120 Theta1 = Theta2; g = 0.0; K *= 4.0; H1 = (H1 * 2.0 + K);
00121 d1 = 2.0 * d2; l1 = l2;
00122 TheState = Continue; break;
00123
00124 case Fail:
00125 Throw(ConvergenceException(Theta));
00126
00127 case Convergence:
00128 Theta = Theta1; return;
00129 }
00130 }
00131 }
00132
00133
00134
00135 void NonLinearLeastSquares::Value
00136 (const ColumnVector& Parameters, bool, Real& v, bool& oorg)
00137 {
00138 Tracer tr("NonLinearLeastSquares::Value");
00139 Y.ReSize(n_obs); X.ReSize(n_obs,n_param);
00140
00141 Pred.Set(Parameters);
00142 if (!Pred.IsValid()) { oorg=true; return; }
00143 for (int i=1; i<=n_obs; i++)
00144 {
00145 Y(i) = Pred(i);
00146 X.Row(i) = Pred.Derivatives();
00147 }
00148 if (!Pred.IsValid()) { oorg=true; return; }
00149 Y = *DataPointer - Y; Real ssq = Y.SumSquare();
00150 errorvar = ssq / (n_obs - n_param);
00151 cout << endl;
00152 cout << setw(15) << setprecision(10) << " " << errorvar;
00153 Derivs = Y.t() * X;
00154 oorg = false; v = -0.5 * ssq;
00155 }
00156
00157 bool NonLinearLeastSquares::NextPoint(ColumnVector& Adj, Real& test)
00158 {
00159 Tracer tr("NonLinearLeastSquares::NextPoint");
00160 QRZ(X, U); QRZ(X, Y, M);
00161 test = M.SumSquare();
00162 cout << " " << setw(15) << setprecision(10)
00163 << test << " " << Y.SumSquare() / (n_obs - n_param);
00164 Adj = U.i() * M;
00165 if (test < errorvar * criterion) return true;
00166 else return false;
00167 }
00168
00169 Real NonLinearLeastSquares::LastDerivative(const ColumnVector& H)
00170 { return (Derivs * H).AsScalar(); }
00171
00172 void NonLinearLeastSquares::Fit(const ColumnVector& Data,
00173 ColumnVector& Parameters)
00174 {
00175 Tracer tr("NonLinearLeastSquares::Fit");
00176 n_param = Parameters.Nrows(); n_obs = Data.Nrows();
00177 DataPointer = &Data;
00178 FindMaximum2::Fit(Parameters, Lim);
00179 cout << "\nConverged" << endl;
00180 }
00181
00182 void NonLinearLeastSquares::MakeCovariance()
00183 {
00184 if (Covariance.Nrows()==0)
00185 {
00186 UpperTriangularMatrix UI = U.i();
00187 Covariance << UI * UI.t() * errorvar;
00188 SE << Covariance;
00189 for (int i = 1; i<=n_param; i++) SE(i) = sqrt(SE(i));
00190 }
00191 }
00192
00193 void NonLinearLeastSquares::GetStandardErrors(ColumnVector& SEX)
00194 { MakeCovariance(); SEX = SE.AsColumn(); }
00195
00196 void NonLinearLeastSquares::GetCorrelations(SymmetricMatrix& Corr)
00197 { MakeCovariance(); Corr << SE.i() * Covariance * SE.i(); }
00198
00199 void NonLinearLeastSquares::GetHatDiagonal(DiagonalMatrix& Hat) const
00200 {
00201 Hat.ReSize(n_obs);
00202 for (int i = 1; i<=n_obs; i++) Hat(i) = X.Row(i).SumSquare();
00203 }
00204
00205
00206
00207
00208 void MLE_D_FI::Value
00209 (const ColumnVector& Parameters, bool wg, Real& v, bool& oorg)
00210 {
00211 Tracer tr("MLE_D_FI::Value");
00212 if (!LL.IsValid(Parameters,wg)) { oorg=true; return; }
00213 v = LL.LogLikelihood();
00214 if (!LL.IsValid()) { oorg=true; return; }
00215 cout << endl;
00216 cout << setw(20) << setprecision(10) << v;
00217 oorg = false;
00218 Derivs = LL.Derivatives();
00219 }
00220
00221 bool MLE_D_FI::NextPoint(ColumnVector& Adj, Real& test)
00222 {
00223 Tracer tr("MLE_D_FI::NextPoint");
00224 SymmetricMatrix FI = LL.FI();
00225 LT = Cholesky(FI);
00226 ColumnVector Adj1 = LT.i() * Derivs;
00227 Adj = LT.t().i() * Adj1;
00228 test = SumSquare(Adj1);
00229 cout << " " << setw(20) << setprecision(10) << test;
00230 return (test < Criterion);
00231 }
00232
00233 Real MLE_D_FI::LastDerivative(const ColumnVector& H)
00234 { return (Derivs.t() * H).AsScalar(); }
00235
00236 void MLE_D_FI::Fit(ColumnVector& Parameters)
00237 {
00238 Tracer tr("MLE_D_FI::Fit");
00239 FindMaximum2::Fit(Parameters,Lim);
00240 cout << "\nConverged" << endl;
00241 }
00242
00243 void MLE_D_FI::MakeCovariance()
00244 {
00245 if (Covariance.Nrows()==0)
00246 {
00247 LowerTriangularMatrix LTI = LT.i();
00248 Covariance << LTI.t() * LTI;
00249 SE << Covariance;
00250 int n = Covariance.Nrows();
00251 for (int i=1; i <= n; i++) SE(i) = sqrt(SE(i));
00252 }
00253 }
00254
00255 void MLE_D_FI::GetStandardErrors(ColumnVector& SEX)
00256 { MakeCovariance(); SEX = SE.AsColumn(); }
00257
00258 void MLE_D_FI::GetCorrelations(SymmetricMatrix& Corr)
00259 { MakeCovariance(); Corr << SE.i() * Covariance * SE.i(); }
00260
00261
00262
00263 #ifdef use_namespace
00264 }
00265 #endif
00266