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 #include "ns3/basic-data-calculators.h"
24 
25 #include <cmath>
26 #include <limits>
27 #include <ostream>
28 #include <stdint.h>
29 
30 namespace ns3
31 {
32 
40 template <typename T = double>
41 class Average
42 {
43  public:
45  : m_size(0),
46  m_min(std::numeric_limits<T>::max()),
47  m_max(0)
48  {
49  }
50 
55  void Update(const T& x)
56  {
57  // Give the variance calculator the next value.
59 
60  m_min = std::min(x, m_min);
61  m_max = std::max(x, m_max);
62  m_size++;
63  }
64 
66  void Reset()
67  {
69 
70  m_size = 0;
72  m_max = 0;
73  }
74 
75  // Sample statistics
80  uint32_t Count() const
81  {
82  return m_size;
83  }
84 
89  T Min() const
90  {
91  return m_min;
92  }
93 
98  T Max() const
99  {
100  return m_max;
101  }
102 
107  double Avg() const
108  {
109  return m_varianceCalculator.getMean();
110  }
111 
116  double Mean() const
117  {
118  return Avg();
119  }
120 
125  double Var() const
126  {
128  }
129 
134  double Stddev() const
135  {
136  return std::sqrt(Var());
137  }
138 
154  double Error90() const
155  {
156  return 1.645 * std::sqrt(Var() / Count());
157  }
158 
169  double Error95() const
170  {
171  return 1.960 * std::sqrt(Var() / Count());
172  }
173 
185  double Error99() const
186  {
187  return 2.576 * std::sqrt(Var() / Count());
188  }
189 
192  private:
193  uint32_t m_size;
194  T m_min;
195  T m_max;
197 };
198 
205 template <typename T>
206 std::ostream&
207 operator<<(std::ostream& os, const Average<T>& x)
208 {
209  if (x.Count() != 0)
210  {
211  os << x.Avg() << " (" << x.Stddev() << ") [" << x.Min() << ", " << x.Max() << "]";
212  }
213  else
214  {
215  os << "NA"; // not available
216  }
217  return os;
218 }
219 } // namespace ns3
220 #endif /* AVERAGE_H */
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Simple average, min, max and std.
Definition: average.h:42
T m_max
Maximum value observed.
Definition: average.h:195
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition: average.h:154
double Avg() const
Sample average.
Definition: average.h:107
T Min() const
Sample minimum.
Definition: average.h:89
double Var() const
Sample unbiased nbiased estimate of variance.
Definition: average.h:125
T Max() const
Sample maximum.
Definition: average.h:98
uint32_t m_size
Number of sampled data.
Definition: average.h:193
T m_min
Minimum value observed.
Definition: average.h:194
void Reset()
Reset statistics.
Definition: average.h:66
void Update(const T &x)
Add new sample.
Definition: average.h:55
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition: average.h:169
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition: average.h:185
uint32_t Count() const
Sample size.
Definition: average.h:80
MinMaxAvgTotalCalculator< double > m_varianceCalculator
Variance calculator.
Definition: average.h:196
double Stddev() const
Sample standard deviation.
Definition: average.h:134
double Mean() const
Sample estimate of mean, alias to Avg.
Definition: average.h:116
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:129