Go to the documentation of this file.00001
00002
00003
00004
00005
00006
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
00019
00020 class Model_3pe : public R1_Col_I_D
00021 {
00022 ColumnVector x_values;
00023 RowVector deriv;
00024 public:
00025 Model_3pe(const ColumnVector& X_Values)
00026 : x_values(X_Values) { deriv.ReSize(3); }
00027
00028 Real operator()(int);
00029 bool IsValid() { return para(3)>0; }
00030
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;
00040 deriv(2) = e;
00041 deriv(3) = - b * e * xvi;
00042 return a + b * e;
00043 }
00044
00045 int main()
00046 {
00047 {
00048
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
00056 Model_3pe model(X);
00057 NonLinearLeastSquares NLLS(model);
00058
00059 ColumnVector Para(3);
00060 Para << 9 << -6 << .5;
00061 cout << "Fitting parameters\n";
00062 NLLS.Fit(Y,Para);
00063
00064
00065 ColumnVector SE;
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
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 }