A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
average.h
Go to the documentation of this file.
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 
39 template <typename T = double>
40 class Average
41 {
42 public:
44  : m_size (0), m_min (std::numeric_limits<T>::max ()), m_max (0)
45  {
46  }
47 
49  void Update (T const & x)
50  {
51  // Give the variance calculator the next value.
53 
54  m_min = std::min (x, m_min);
55  m_max = std::max (x, m_max);
56  m_size++;
57  }
59  void Reset ()
60  {
62 
63  m_size = 0;
64  m_min = std::numeric_limits<T>::max ();
65  m_max = 0;
66  }
67 
69  //\{
71  uint32_t Count () const { return m_size; }
73  T Min () const { return m_min; }
75  T Max () const { return m_max; }
77  double Avg () const { return m_varianceCalculator.getMean ();}
79  double Mean () const { return Avg (); }
81  double Var () const { return m_varianceCalculator.getVariance ();}
83  double Stddev () const { return std::sqrt (Var ()); }
84  //\}
85 
94  //\{
96  double Error90 () const { return 1.645 * std::sqrt (Var () / Count ()); }
98  double Error95 () const { return 1.960 * std::sqrt (Var () / Count ()); }
100  double Error99 () const { return 2.576 * std::sqrt (Var () / Count ()); }
101  //\}
102 
103 private:
104  uint32_t m_size;
105  T m_min;
106  T m_max;
108 };
109 
111 template <typename T>
112 std::ostream & operator<< (std::ostream & os, Average<T> const & x)
113 {
114  if (x.Count () != 0)
115  os << x.Avg () << " (" << x.Stddev () << ") [" << x.Min () << ", " << x.Max () << "]";
116  else
117  os << "NA"; // not available
118  return os;
119 }
120 }
121 #endif /* AVERAGE_H */
T Max() const
Maximum.
Definition: average.h:75
T m_min
Minimum value observed.
Definition: average.h:105
double Stddev() const
Standard deviation.
Definition: average.h:83
double Mean() const
Estimate of mean, alias to Avg.
Definition: average.h:79
double Avg() const
Sample average.
Definition: average.h:77
double getVariance() const
Returns the current variance.
uint32_t m_size
Number of sampled data.
Definition: average.h:104
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
T m_max
Maximum value observed.
Definition: average.h:106
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition: average.h:98
T Min() const
Minimum.
Definition: average.h:73
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition: average.h:100
void Reset()
Reset statistics.
Definition: average.h:59
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Simple average, min, max and std.
Definition: average.h:40
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition: average.h:96
MinMaxAvgTotalCalculator< double > m_varianceCalculator
Variance calculator.
Definition: average.h:107
double getMean() const
Returns the mean value.
uint32_t Count() const
Sample size.
Definition: average.h:71
double Var() const
Unbiased estimate of variance.
Definition: average.h:81
void Update(T const &x)
Add new sample.
Definition: average.h:49