A Discrete-Event Network Simulator
API
average.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Pavel Boyko <boyko@iitp.ru>
18  * Corrections and extensions: Timo Bingmann <tbns@idlebox.net>
19  */
20 
21 #ifndef AVERAGE_H
22 #define AVERAGE_H
23 
24 #include "basic-data-calculators.h"
25 
26 #include <cmath>
27 #include <limits>
28 #include <ostream>
29 #include <stdint.h>
30 
31 namespace ns3
32 {
33 
41 template <typename T = double>
42 class Average
43 {
44  public:
46  : m_size(0),
47  m_min(std::numeric_limits<T>::max()),
48  m_max(0)
49  {
50  }
51 
56  void Update(const T& x)
57  {
58  // Give the variance calculator the next value.
60 
61  m_min = std::min(x, m_min);
62  m_max = std::max(x, m_max);
63  m_size++;
64  }
65 
67  void Reset()
68  {
70 
71  m_size = 0;
73  m_max = 0;
74  }
75 
76  // Sample statistics
81  uint32_t Count() const
82  {
83  return m_size;
84  }
85 
90  T Min() const
91  {
92  return m_min;
93  }
94 
99  T Max() const
100  {
101  return m_max;
102  }
103 
108  double Avg() const
109  {
110  return m_varianceCalculator.getMean();
111  }
112 
117  double Mean() const
118  {
119  return Avg();
120  }
121 
126  double Var() const
127  {
129  }
130 
135  double Stddev() const
136  {
137  return std::sqrt(Var());
138  }
139 
155  double Error90() const
156  {
157  return 1.645 * std::sqrt(Var() / Count());
158  }
159 
170  double Error95() const
171  {
172  return 1.960 * std::sqrt(Var() / Count());
173  }
174 
186  double Error99() const
187  {
188  return 2.576 * std::sqrt(Var() / Count());
189  }
190 
193  private:
194  uint32_t m_size;
195  T m_min;
196  T m_max;
198 };
199 
206 template <typename T>
207 std::ostream&
208 operator<<(std::ostream& os, const Average<T>& x)
209 {
210  if (x.Count() != 0)
211  {
212  os << x.Avg() << " (" << x.Stddev() << ") [" << x.Min() << ", " << x.Max() << "]";
213  }
214  else
215  {
216  os << "NA"; // not available
217  }
218  return os;
219 }
220 } // namespace ns3
221 #endif /* AVERAGE_H */
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
Simple average, min, max and std.
Definition: average.h:43
T m_max
Maximum value observed.
Definition: average.h:196
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition: average.h:155
double Avg() const
Sample average.
Definition: average.h:108
T Min() const
Sample minimum.
Definition: average.h:90
double Var() const
Sample unbiased nbiased estimate of variance.
Definition: average.h:126
T Max() const
Sample maximum.
Definition: average.h:99
uint32_t m_size
Number of sampled data.
Definition: average.h:194
T m_min
Minimum value observed.
Definition: average.h:195
void Reset()
Reset statistics.
Definition: average.h:67
void Update(const T &x)
Add new sample.
Definition: average.h:56
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition: average.h:170
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition: average.h:186
uint32_t Count() const
Sample size.
Definition: average.h:81
MinMaxAvgTotalCalculator< double > m_varianceCalculator
Variance calculator.
Definition: average.h:197
double Stddev() const
Sample standard deviation.
Definition: average.h:135
double Mean() const
Sample estimate of mean, alias to Avg.
Definition: average.h:117
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
double getVariance() const override
Returns the current variance.
double getMean() const override
Returns the mean value.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159