nl_ex.cc
Go to the documentation of this file.
00001 // This is an example of a non-linear least squares fit. The example
00002 // is from "Nonlinear estimation" by Gavin Ross (Springer,1990), p 63.
00003 // There are better ways of doing the fit in this case so this
00004 // example is just an example.
00005 
00006 // The model is E(y) = a + b exp(-kx) and there are 6 data points.
00007 
00008 #define WANT_STREAM
00009 #define WANT_MATH
00010 #include "newmatnl.h"
00011 #include "newmatio.h"
00012 
00013 #ifdef use_namespace
00014 using namespace RBD_LIBRARIES;
00015 #endif
00016 
00017 
00018 // first define the class describing the predictor function
00019 
00020 class Model_3pe : public R1_Col_I_D
00021 {
00022    ColumnVector x_values;         // the values of "x"
00023    RowVector deriv;               // values of derivatives
00024 public:
00025    Model_3pe(const ColumnVector& X_Values)
00026       : x_values(X_Values) { deriv.ReSize(3); }
00027                                                                                          // load X data
00028    Real operator()(int);
00029    bool IsValid() { return para(3)>0; }
00030                                   // require "k" > 0
00031    ReturnMatrix Derivatives() { return deriv; }
00032 };
00033 
00034 Real Model_3pe::operator()(int i)
00035 {
00036    Real a = para(1); Real b = para(2); Real k = para(3);
00037    Real xvi = x_values(i);
00038    Real e = exp(-k * xvi);
00039    deriv(1) = 1.0;                    // calculate derivatives
00040    deriv(2) = e;
00041    deriv(3) = - b * e * xvi;
00042    return a + b * e;                  // function value
00043 }
00044 
00045 int main()
00046 {
00047    {
00048       // Get the data
00049       ColumnVector X(6);
00050       ColumnVector Y(6);
00051       X << 1   << 2   <<  3   <<  4   <<  6   <<  8;
00052       Y << 3.2 << 7.9 << 11.1 << 14.5 << 16.7 << 18.3;
00053 
00054 
00055       // Do the fit
00056       Model_3pe model(X);                // the model object
00057       NonLinearLeastSquares NLLS(model); // the non-linear least squares
00058                                          // object
00059       ColumnVector Para(3);              // for the parameters
00060       Para << 9 << -6 << .5;             // trial values of parameters
00061       cout << "Fitting parameters\n";
00062       NLLS.Fit(Y,Para);                  // do the fit
00063 
00064       // Inspect the results
00065       ColumnVector SE;                   // for the standard errors
00066       NLLS.GetStandardErrors(SE);
00067       cout << "\n\nEstimates and standard errors\n" <<
00068          setw(10) << setprecision(2) << (Para | SE) << endl;
00069       Real ResidualSD = sqrt(NLLS.ResidualVariance());
00070       cout << "\nResidual s.d. = " << setw(10) << setprecision(2) <<
00071          ResidualSD << endl;
00072       SymmetricMatrix Correlations;
00073       NLLS.GetCorrelations(Correlations);
00074       cout << "\nCorrelationMatrix\n" <<
00075          setw(10) << setprecision(2) << Correlations << endl;
00076       ColumnVector Residuals;
00077       NLLS.GetResiduals(Residuals);
00078       DiagonalMatrix Hat;
00079       NLLS.GetHatDiagonal(Hat);
00080       cout << "\nX, Y, Residual, Hat\n" << setw(10) << setprecision(2) <<
00081          (X | Y | Residuals | Hat.AsColumn()) << endl;
00082       // recover var/cov matrix
00083       SymmetricMatrix D;
00084       D << SE.AsDiagonal() * Correlations * SE.AsDiagonal();
00085       cout << "\nVar/cov\n" << setw(14) << setprecision(4) << D << endl;
00086    }
00087 
00088 #ifdef DO_FREE_CHECK
00089    FreeCheck::Status();
00090 #endif
00091  
00092    return 0;
00093 }


rl_agent
Author(s): Todd Hester
autogenerated on Thu Jun 6 2019 22:00:13