cstddev.h

00001 //==========================================================================
00002 //  CSTDDEV.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cStdDev: basic statistics (mean, stddev, min, max, etc)
00009 //    cWeightedStdDev: weighted version
00010 //
00011 //==========================================================================
00012 
00013 /*--------------------------------------------------------------*
00014   Copyright (C) 1992-2008 Andras Varga
00015   Copyright (C) 2006-2008 OpenSim Ltd.
00016 
00017   This file is distributed WITHOUT ANY WARRANTY. See the file
00018   `license' for details on this and other legal matters.
00019 *--------------------------------------------------------------*/
00020 
00021 #ifndef __CSTDDEV_H
00022 #define __CSTDDEV_H
00023 
00024 #include <stdio.h>
00025 #include "cstatistic.h"
00026 
00027 NAMESPACE_BEGIN
00028 
00034 class SIM_API cStdDev : public cStatistic
00035 {
00036   protected:
00037     long num_vals;
00038     double min_vals, max_vals;
00039     double sum_vals, sqrsum_vals;
00040 
00041   private:
00042     void copy(const cStdDev& other);
00043 
00044   protected:
00045     void doMerge(const cStatistic *other);
00046 
00047   public:
00050 
00054     cStdDev(const cStdDev& r) : cStatistic(r) {copy(r);}
00055 
00059     explicit cStdDev(const char *name=NULL);
00060 
00064     virtual ~cStdDev() {}
00065 
00069     cStdDev& operator=(const cStdDev& res);
00071 
00074 
00079     virtual cStdDev *dup() const  {return new cStdDev(*this);}
00080 
00085     virtual std::string info() const;
00086 
00091     virtual std::string detailedInfo() const;
00092 
00098     virtual void parsimPack(cCommBuffer *buffer);
00099 
00105     virtual void parsimUnpack(cCommBuffer *buffer);
00107 
00110 
00114     virtual void collect(double value);
00115 
00119     virtual void collect(SimTime value) {collect(value.dbl());}
00120 
00127     virtual void merge(const cStatistic *other);
00128 
00132     virtual bool isWeighted() const  {return false;}
00133 
00137     virtual long getCount() const  {return num_vals;}
00138 
00142     virtual double getSum() const  {return sum_vals;}
00143 
00147     virtual double getSqrSum() const  {return sqrsum_vals;}
00148 
00152     virtual double getMin() const;
00153 
00157     virtual double getMax() const;
00158 
00162     virtual double getMean() const;
00163 
00168     virtual double getStddev() const;
00169 
00174     virtual double getVariance() const;
00175 
00179     virtual double getWeights() const  {return getCount();}
00180 
00185     virtual double getWeightedSum() const  {return getSum();}
00186 
00191     virtual double getSqrSumWeights() const  {return getCount();}
00192 
00197     virtual double getWeightedSqrSum() const  {return getSqrSum();}
00198 
00203     virtual double random() const;
00204 
00208     virtual void clearResult();
00209 
00213     virtual void saveToFile(FILE *) const;
00214 
00219     virtual void loadFromFile(FILE *);
00221 };
00222 
00223 
00224 
00231 class SIM_API cWeightedStdDev : public cStdDev
00232 {
00233   protected:
00234     double sum_weights;
00235     double sum_weighted_vals;
00236     double sum_squared_weights;
00237     double sum_weights_squared_vals;
00238 
00239   private:
00240     void copy(const cWeightedStdDev& other);
00241 
00242   public:
00245 
00249     cWeightedStdDev(const cWeightedStdDev& r) : cStdDev(r) {copy(r);}
00250 
00254     explicit cWeightedStdDev(const char *name=NULL) : cStdDev(name)  {sum_weights=sum_weighted_vals=sum_squared_weights=sum_weights_squared_vals=0.0;}
00255 
00259     virtual ~cWeightedStdDev() {}
00260 
00264     cWeightedStdDev& operator=(const cWeightedStdDev& res);
00266 
00269 
00274     virtual cWeightedStdDev *dup() const  {return new cWeightedStdDev(*this);}
00275 
00280     virtual std::string info() const;
00281 
00287     virtual void parsimPack(cCommBuffer *buffer);
00288 
00294     virtual void parsimUnpack(cCommBuffer *buffer);
00296 
00299 
00303     virtual void collect(double value)  {collect2(value,1.0);}
00304 
00308     virtual void collect(SimTime value) {collect(value.dbl());}
00309 
00313     virtual bool isWeighted() const  {return true;}
00314 
00318     virtual void collect2(double value, double weight);
00319 
00323     virtual void collect2(SimTime value, double weight) {collect2(value.dbl(), weight);}
00324 
00328     virtual void collect2(double value, SimTime weight) {collect2(value, weight.dbl());}
00329 
00333     virtual void collect2(SimTime value, SimTime weight) {collect2(value.dbl(), weight.dbl());}
00334 
00340     virtual void merge(const cStatistic *other);
00341 
00345     virtual void clearResult();
00346 
00351     virtual double getMean() const;
00352 
00357     virtual double getVariance() const;
00358 
00362     virtual double getWeights() const  {return sum_weights;}
00363 
00367     virtual double getWeightedSum() const  {return sum_weighted_vals;}
00368 
00372     virtual double getSqrSumWeights() const  {return sum_squared_weights;}
00373 
00377     virtual double getWeightedSqrSum() const  {return sum_weights_squared_vals;}
00378 
00382     virtual void saveToFile(FILE *) const;
00383 
00387     virtual void loadFromFile(FILE *);
00389 };
00390 
00391 NAMESPACE_END
00392 
00393 #endif
00394 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3