00001
00002
00003
00004
00005
00006 #ifndef NEWMATNL_LIB
00007 #define NEWMATNL_LIB 0
00008
00009 #include "newmat.h"
00010
00011 #ifdef use_namespace
00012 namespace NEWMAT {
00013 #endif
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00107
00108
00109
00110
00111
00112
00113
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00178 class FindMaximum2 {
00179 virtual void Value(const ColumnVector&, bool, Real&, bool&) = 0;
00180 virtual bool NextPoint(ColumnVector&, Real&) = 0;
00181 virtual Real LastDerivative(const ColumnVector&) = 0;
00182 public:
00183 void Fit(ColumnVector&, int);
00184 };
00185
00189 class R1_Col_I_D {
00190
00191
00192
00193
00194
00195
00196
00197 protected:
00198 ColumnVector para;
00199
00200 public:
00201 virtual bool IsValid() { return true; }
00202
00203 virtual Real operator()(int i) = 0;
00204 virtual void Set(const ColumnVector& X) { para = X; }
00205
00206 bool IsValid(const ColumnVector& X)
00207 { Set(X); return IsValid(); }
00208
00209 Real operator()(int i, const ColumnVector& X)
00210 { Set(X); return operator()(i); }
00211
00212 virtual ReturnMatrix Derivatives() = 0;
00213
00214 };
00215
00219 class NonLinearLeastSquares : public FindMaximum2 {
00220
00221 void Value(const ColumnVector&, bool, Real&, bool&);
00222 bool NextPoint(ColumnVector&, Real&);
00223 Real LastDerivative(const ColumnVector&);
00224
00225 Matrix X;
00226 ColumnVector Y;
00227 UpperTriangularMatrix U;
00228 ColumnVector M;
00229 Real errorvar, criterion;
00230 int n_obs, n_param;
00231 const ColumnVector* DataPointer;
00232 RowVector Derivs;
00233 SymmetricMatrix Covariance;
00234 DiagonalMatrix SE;
00235 R1_Col_I_D& Pred;
00236 int Lim;
00237
00238 public:
00239 NonLinearLeastSquares(R1_Col_I_D& pred, int lim=1000, Real crit=0.0001)
00240 : Pred(pred), Lim(lim), criterion(crit) {}
00241 void Fit(const ColumnVector&, ColumnVector&);
00242 Real ResidualVariance() const { return errorvar; }
00243 void GetResiduals(ColumnVector& Z) const { Z = Y; }
00244 void GetStandardErrors(ColumnVector&);
00245 void GetCorrelations(SymmetricMatrix&);
00246 void GetHatDiagonal(DiagonalMatrix&) const;
00247
00248 private:
00249 void MakeCovariance();
00250 };
00251
00252
00253
00254
00255
00256
00257
00258
00265 class LL_D_FI {
00266 protected:
00267 ColumnVector para;
00268 bool wg;
00269
00270 public:
00271 virtual void Set(const ColumnVector& X) { para = X; }
00272
00273 virtual void WG(bool wgx) { wg = wgx; }
00274
00275
00276 virtual bool IsValid() { return true; }
00277
00278 bool IsValid(const ColumnVector& X, bool wgx=true)
00279 { Set(X); WG(wgx); return IsValid(); }
00280
00281 virtual Real LogLikelihood() = 0;
00282 Real LogLikelihood(const ColumnVector& X, bool wgx=true)
00283 { Set(X); WG(wgx); return LogLikelihood(); }
00284
00285 virtual ReturnMatrix Derivatives() = 0;
00286
00287 virtual ReturnMatrix FI() = 0;
00288 };
00289
00290
00291
00294 class MLE_D_FI : public FindMaximum2 {
00295
00296 void Value(const ColumnVector&, bool, Real&, bool&);
00297 bool NextPoint(ColumnVector&, Real&);
00298 Real LastDerivative(const ColumnVector&);
00299
00300
00301 LL_D_FI& LL;
00302 int Lim;
00303 Real Criterion;
00304 ColumnVector Derivs;
00305 LowerTriangularMatrix LT;
00306 SymmetricMatrix Covariance;
00307 DiagonalMatrix SE;
00308
00309 public:
00310 MLE_D_FI(LL_D_FI& ll, int lim=1000, Real criterion=0.0001)
00311 : LL(ll), Lim(lim), Criterion(criterion) {}
00312 void Fit(ColumnVector& Parameters);
00313 void GetStandardErrors(ColumnVector&);
00314 void GetCorrelations(SymmetricMatrix&);
00315
00316 private:
00317 void MakeCovariance();
00318 };
00319
00320 #ifdef use_namespace
00321 }
00322 #endif
00323 #endif