Statistics
| Branch: | Revision:

root / src / scave / stddev.cc @ e1750c09

History | View | Annotate | Download (2.27 KB)

1
//=========================================================================
2
//  STDDEV.CC - 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
#include <math.h>
19
#include "scaveutils.h"
20
#include "channel.h"
21
#include "stddev.h"
22

    
23
USING_NAMESPACE
24

    
25

    
26
StddevNode::StddevNode()
27
    : num_vals(0), min_vals(NaN), max_vals(NaN), sum_vals(0.0), sqrsum_vals(0.0)
28
{
29
}
30

    
31
StddevNode::~StddevNode()
32
{
33
}
34

    
35
bool StddevNode::isReady() const
36
{
37
    return in()->length()>0;
38
}
39

    
40
void StddevNode::process()
41
{
42
    int n = in()->length();
43
    for (int i=0; i<n; i++)
44
    {
45
        Datum a;
46
        in()->read(&a,1);
47
        collect(a.y);
48
    }
49
}
50

    
51
bool StddevNode::isFinished() const
52
{
53
    return in()->eof();
54
}
55

    
56
void StddevNode::collect(double val)
57
{
58
    if (++num_vals <= 0)
59
        throw opp_runtime_error("StddevNode: observation count overflow");
60

    
61
    sum_vals += val;
62
    sqrsum_vals += val*val;
63

    
64
    if (num_vals>1)
65
    {
66
        if (val<min_vals)
67
            min_vals = val;
68
        else if (val>max_vals)
69
            max_vals = val;
70
    }
71
    else
72
    {
73
        min_vals = max_vals = val;
74
    }
75
}
76

    
77
double StddevNode::getVariance() const
78
{
79
    if (num_vals<=1)
80
        return 0.0;
81
    else
82
    {
83
        double devsqr = (sqrsum_vals - sum_vals*sum_vals/num_vals)/(num_vals-1);
84
        if (devsqr<=0)
85
            return 0.0;
86
        else
87
            return devsqr;
88
    }
89
}
90

    
91
double StddevNode::getStddev() const
92
{
93
    return sqrt( getVariance() );
94
}
95

    
96

    
97
//------
98

    
99
const char *StddevNodeType::getDescription() const
100
{
101
    return "Collects basic statistics like min, max, mean, stddev.";
102
}
103

    
104
void StddevNodeType::getAttributes(StringMap& attrs) const
105
{
106
}
107

    
108
Node *StddevNodeType::create(DataflowManager *mgr, StringMap& attrs) const
109
{
110
    checkAttrNames(attrs);
111

    
112
    Node *node = new StddevNode();
113
    node->setNodeType(this);
114
    mgr->addNode(node);
115
    return node;
116
}
117

    
118