ltiSerialVectorStats.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _LTI_SERIALVECTORSTATS_H_
00034 #define _LTI_SERIALVECTORSTATS_H_
00035
00036 #include "ltiVector.h"
00037 #include "ltiMatrix.h"
00038 #include "ltiStatisticsFunctor.h"
00039 #include "ltiTypeInfo.h"
00040
00041 namespace lti {
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 template <class T>
00069 class serialVectorStats : public statisticsFunctor {
00070 public:
00071
00072
00073
00074 class parameters : public statisticsFunctor::parameters {
00075 public:
00076
00077
00078
00079
00080 enum eVarianceType {
00081 Empirical=0,
00082 Maxlikely=1
00083 };
00084
00085
00086
00087
00088 parameters() : statisticsFunctor::parameters() {
00089 type = Empirical;
00090 correlation=false;
00091 };
00092
00093
00094
00095
00096
00097 parameters(const parameters& other)
00098 : statisticsFunctor::parameters() {
00099 copy(other);
00100 };
00101
00102
00103
00104
00105 ~parameters() {};
00106
00107
00108
00109
00110 const char* getTypeName() const {
00111 return "serialVectorStats::parameters";
00112 };
00113
00114
00115
00116
00117
00118
00119 parameters& copy(const parameters& other) {
00120 # ifndef _LTI_MSC_6
00121
00122 statisticsFunctor::parameters::copy(other);
00123 # else
00124
00125
00126 statisticsFunctor::parameters&
00127 (statisticsFunctor::parameters::* p_copy)
00128 (const statisticsFunctor::parameters&) =
00129 statisticsFunctor::parameters::copy;
00130 (this->*p_copy)(other);
00131 # endif
00132
00133 type=other.type;
00134 correlation=other.correlation;
00135
00136 return *this;
00137 };
00138
00139
00140
00141
00142
00143
00144 parameters& operator=(const parameters& other) {
00145 return copy(other);
00146 };
00147
00148
00149
00150
00151 virtual functor::parameters* clone() const {
00152 return new parameters(*this);
00153 };
00154
00155 # ifndef _LTI_MSC_6
00156
00157
00158
00159
00160
00161
00162
00163
00164 virtual bool write(ioHandler& handler,const bool complete=true) const
00165 # else
00166
00167
00168
00169
00170 bool writeMS(ioHandler& handler,const bool complete=true) const
00171 # endif
00172 {
00173 bool b = true;
00174 if (complete) {
00175 b = handler.writeBegin();
00176 }
00177
00178 if (b) {
00179 if (type == Empirical) {
00180 lti::write(handler,"type","Empirical");
00181 } else {
00182 lti::write(handler,"type","Maxlikely");
00183 }
00184 lti::write(handler, "correlation", correlation);
00185 }
00186
00187 # ifndef _LTI_MSC_6
00188
00189
00190 b = b && statisticsFunctor::parameters::write(handler,false);
00191 # else
00192 bool (functor::parameters::* p_writeMS)(ioHandler&,
00193 const bool) const =
00194 functor::parameters::writeMS;
00195 b = b && (this->*p_writeMS)(handler,false);
00196 # endif
00197
00198 if (complete) {
00199 b = b && handler.writeEnd();
00200 }
00201
00202 return b;
00203 }
00204
00205 # ifdef _LTI_MSC_6
00206
00207
00208
00209
00210
00211
00212
00213
00214 virtual bool write(ioHandler& handler,
00215 const bool complete = true) const {
00216
00217
00218 return writeMS(handler,complete);
00219 }
00220 # endif
00221
00222 # ifndef _LTI_MSC_6
00223
00224
00225
00226
00227
00228
00229
00230 virtual bool read(ioHandler& handler,const bool complete = true)
00231 # else
00232
00233
00234
00235
00236 bool readMS(ioHandler& handler,const bool complete=true)
00237 # endif
00238 {
00239 bool b = true;
00240 if (complete) {
00241 b = handler.readBegin();
00242 }
00243
00244 if (b) {
00245 std::string str;
00246
00247 lti::read(handler,"type",str);
00248 if (str == "Empirical") {
00249 type = Empirical;
00250 } else {
00251 type = Maxlikely;
00252 }
00253 lti::read(handler, "correlation", correlation);
00254 }
00255
00256 # ifndef _LTI_MSC_6
00257
00258
00259 b = b && statisticsFunctor::parameters::read(handler,false);
00260 # else
00261 bool (functor::parameters::* p_readMS)(ioHandler&,const bool) =
00262 functor::parameters::readMS;
00263 b = b && (this->*p_readMS)(handler,false);
00264 # endif
00265
00266 if (complete) {
00267 b = b && handler.readEnd();
00268 }
00269
00270 return b;
00271 }
00272
00273 # ifdef _LTI_MSC_6
00274
00275
00276
00277
00278
00279
00280
00281 virtual bool read(ioHandler& handler,const bool complete=true) {
00282
00283
00284 return readMS(handler,complete);
00285 }
00286 # endif
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 eVarianceType type;
00303
00304
00305
00306
00307
00308
00309
00310 bool correlation;
00311 };
00312
00313 typedef typename typeInfo<T>::square_accumulation_type sqrType;
00314
00315
00316
00317
00318 serialVectorStats();
00319
00320
00321
00322
00323
00324 serialVectorStats(const serialVectorStats& other);
00325
00326
00327
00328
00329 virtual ~serialVectorStats();
00330
00331
00332
00333
00334 virtual const char* getTypeName() const;
00335
00336
00337
00338
00339
00340
00341 void apply(vector<T>& mean, vector<T>& variance) const;
00342
00343
00344
00345
00346
00347
00348
00349
00350 void apply(vector<T>& mean, matrix<T>& covariance) const;
00351
00352
00353
00354
00355
00356 void considerRows(const matrix<T> &element);
00357
00358
00359
00360
00361
00362 void consider(const vector<T> &element);
00363
00364
00365
00366
00367
00368 void consider(const vector<T> &element,const int& n);
00369
00370
00371
00372
00373 double getN() const;
00374
00375
00376
00377
00378 void setN(double newN);
00379
00380
00381
00382
00383 void getMean(vector<T> &mean) const;
00384
00385
00386
00387
00388 void getVariance(vector<T> &var) const;
00389
00390
00391
00392
00393 void getCovariance(matrix<T> &covar) const;
00394
00395
00396
00397
00398
00399 void getSumOfVectors(vector<T>& s) const;
00400
00401
00402
00403
00404
00405
00406 void getSumOfVecOuterProd(matrix<sqrType>& s) const;
00407
00408
00409
00410
00411
00412 const vector<T>& getSumOfVectors() const;
00413
00414
00415
00416
00417
00418
00419 const matrix<sqrType>& getSumOfVecOuterProd() const;
00420
00421
00422
00423
00424 void reset();
00425
00426
00427
00428
00429
00430
00431 serialVectorStats& copy(const serialVectorStats& other);
00432
00433
00434
00435
00436 serialVectorStats& operator=(const serialVectorStats& other);
00437
00438
00439
00440
00441 virtual functor* clone() const;
00442
00443
00444
00445
00446 const parameters& getParameters() const;
00447
00448
00449
00450
00451 virtual bool read(ioHandler &handler, const bool complete=true);
00452
00453
00454
00455
00456 virtual bool write(ioHandler &handler, const bool complete=true) const;
00457
00458 protected:
00459
00460
00461
00462
00463 void outer(const vector<T>& a,
00464 const vector<T>& b,
00465 matrix<sqrType>& dest) const;
00466
00467
00468
00469
00470
00471
00472 void outerAcc(const vector<T>& a,
00473 const vector<T>& b,
00474 matrix<sqrType>& dest) const;
00475
00476
00477
00478
00479
00480 void outerAcc(const vector<T>& a,
00481 const vector<T>& b,
00482 const int n,
00483 matrix<sqrType>& dest) const;
00484
00485
00486
00487
00488 void diagonal(const matrix<sqrType>& mat,vector<T>& diag) const;
00489
00490 private:
00491
00492
00493
00494 vector<T> sumX;
00495
00496
00497
00498
00499 matrix<sqrType> sumXXT;
00500
00501
00502
00503
00504 double nv;
00505
00506 };
00507
00508 template <class T>
00509 bool read(ioHandler& handler,
00510 serialVectorStats<T>& pca,
00511 const bool complete=true) {
00512 return pca.read(handler,complete);
00513 }
00514
00515
00516 template <class T>
00517 bool write(ioHandler& handler,
00518 const serialVectorStats<T>& pca,
00519 const bool complete=true) {
00520 return pca.write(handler,complete);
00521 }
00522
00523 }
00524
00525 #include "ltiSerialVectorStats_template.h"
00526
00527 #endif