Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / statistics.h @ a3be1d55

History | View | Annotate | Download (2.42 KB)

1
//=========================================================================
2
//  STATISTICS.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Tamas Borbely
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#ifndef _STATISTICS_H_
19
#define _STATISTICS_H_
20

    
21
#include <float.h>
22
#include "scavedefs.h"
23
#include "scaveutils.h"
24

    
25
NAMESPACE_BEGIN
26

    
27
/**
28
 * Class for collecting statistical data of the result
29
 * used in several places in Scave.
30
 */
31
class SCAVE_API Statistics
32
{
33
    private:
34
        long _count;
35
        double _min;
36
        double _max;
37
        double _sum;
38
        double _sumSqr;
39

    
40
    public:
41
        Statistics() : _count(0), _min(POSITIVE_INFINITY), _max(NEGATIVE_INFINITY), _sum(0.0), _sumSqr(0.0) {}
42
        Statistics(long count, double min, double max, double sum, double sumSqr)
43
            :_count(count), _min(min), _max(max), _sum(sum), _sumSqr(sumSqr) {}
44

    
45
        long getCount() const { return _count; }
46
        double getMin() const { return _min; }
47
        double getMax() const { return _max; }
48
        double getSum() const { return _sum; }
49
        double getSumSqr() const { return _sumSqr; }
50
        double getMean() const { return _count == 0 ? NaN : _sum / _count; }
51
        double getStddev() const { return sqrt(getVariance()); }
52
        double getVariance() const;
53

    
54
        void collect(double value);
55
        void adjoin(const Statistics &other);
56
};
57

    
58
inline double Statistics::getVariance() const
59
{
60
    if (_count >= 1)
61
    {
62
        double var = (_sumSqr - _sum*_sum/_count)/(_count-1);
63
        return var < 0 ? 0 : var;
64
    }
65
    else
66
        return NaN;
67
}
68

    
69
inline void Statistics::collect(double value)
70
{
71
    _count++;
72
    _min = (_min < value ? _min : value);
73
    _max = (_max > value ? _max : value);
74
    _sum += value;
75
    _sumSqr += value * value;
76
}
77

    
78
inline void Statistics::adjoin(const Statistics &other)
79
{
80
    _count += other._count;
81
    _min = (_min < other._min ? _min : other._min);
82
    _max = (_max > other._max ? _max : other._max);
83
    _sum += other._sum;
84
    _sumSqr += other._sumSqr;
85
}
86

    
87
NAMESPACE_END
88

    
89

    
90
#endif