Homepage Demos Overview Downloads Tutorials Reference
Credits

newmatnl.cpp

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

newmat11b
Generated Tue Oct 19 14:18:14 2004 by Doxygen 1.3.9.1