A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
average.h
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Pavel Boyko <boyko@iitp.ru>
19  * Corrections and extensions: Timo Bingmann <tbns@idlebox.net>
20  */
21 
22 #ifndef AVERAGE_H
23 #define AVERAGE_H
24 #include <cmath>
25 #include <ostream>
26 #include <limits>
27 #include <stdint.h>
28 #include "ns3/basic-data-calculators.h"
29 
30 namespace ns3 {
31 
44 template <typename T = double>
45 class Average
46 {
47 public:
48  Average ()
49  : m_size (0), m_min (std::numeric_limits<T>::max ()), m_max (0)
50  {
51  }
52 
54  void Update (T const & x)
55  {
56  // Give the variance calculator the next value.
57  m_varianceCalculator.Update (x);
58 
59  m_min = std::min (x, m_min);
60  m_max = std::max (x, m_max);
61  m_size++;
62  }
64  void Reset ()
65  {
66  m_varianceCalculator.Reset ();
67 
68  m_size = 0;
69  m_min = std::numeric_limits<T>::max ();
70  m_max = 0;
71  }
72 
74  //\{
76  uint32_t Count () const { return m_size; }
78  T Min () const { return m_min; }
80  T Max () const { return m_max; }
82  double Avg () const { return m_varianceCalculator.getMean ();}
84  double Mean () const { return Avg (); }
86  double Var () const { return m_varianceCalculator.getVariance ();}
88  double Stddev () const { return std::sqrt (Var ()); }
89  //\}
90 
99  //\{
101  double Error90 () const { return 1.645 * std::sqrt (Var () / Count ()); }
103  double Error95 () const { return 1.960 * std::sqrt (Var () / Count ()); }
105  double Error99 () const { return 2.576 * std::sqrt (Var () / Count ()); }
106  //\}
107 
108 private:
109  uint32_t m_size;
110  T m_min, m_max;
111  MinMaxAvgTotalCalculator<double> m_varianceCalculator;
112 };
113 
115 template <typename T>
116 std::ostream & operator<< (std::ostream & os, Average<T> const & x)
117 {
118  if (x.Count () != 0)
119  os << x.Avg () << " (" << x.Stddev () << ") [" << x.Min () << ", " << x.Max () << "]";
120  else
121  os << "NA"; // not available
122  return os;
123 }
124 }
125 #endif /* AVERAGE_H */
T Max() const
Maximum.
Definition: average.h:80
double Stddev() const
Standard deviation.
Definition: average.h:88
double Mean() const
Estimate of mean, alias to Avg.
Definition: average.h:84
double Avg() const
Sample average.
Definition: average.h:82
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition: average.h:103
T Min() const
Minimum.
Definition: average.h:78
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition: average.h:105
void Reset()
Reset statistics.
Definition: average.h:64
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition: average.h:101
uint32_t Count() const
Sample size.
Definition: average.h:76
double Var() const
Unbiased estimate of variance.
Definition: average.h:86
void Update(T const &x)
Add new sample.
Definition: average.h:54