A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
average.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Pavel Boyko <boyko@iitp.ru>
7 * Corrections and extensions: Timo Bingmann <tbns@idlebox.net>
8 */
9
10#ifndef AVERAGE_H
11#define AVERAGE_H
12
14
15#include <cmath>
16#include <limits>
17#include <ostream>
18#include <stdint.h>
19
20namespace ns3
21{
22
23/**
24 * \ingroup stats
25 *
26 * Simple average, min, max and std. deviation calculator
27 *
28 */
29
30template <typename T = double>
32{
33 public:
35 : m_size(0),
36 m_min(std::numeric_limits<T>::max()),
37 m_max(0)
38 {
39 }
40
41 /**
42 * Add new sample
43 * \param x The sample
44 */
45 void Update(const T& x)
46 {
47 // Give the variance calculator the next value.
49
50 m_min = std::min(x, m_min);
51 m_max = std::max(x, m_max);
52 m_size++;
53 }
54
55 /// Reset statistics
56 void Reset()
57 {
59
60 m_size = 0;
61 m_min = std::numeric_limits<T>::max();
62 m_max = 0;
63 }
64
65 // Sample statistics
66 /**
67 * Sample size
68 * \return the sample size
69 */
71 {
72 return m_size;
73 }
74
75 /**
76 * Sample minimum
77 * \return the minimum of the sample
78 */
79 T Min() const
80 {
81 return m_min;
82 }
83
84 /**
85 * Sample maximum
86 * \return the maximum of the sample
87 */
88 T Max() const
89 {
90 return m_max;
91 }
92
93 /**
94 * Sample average
95 * \return the average of the sample
96 */
97 double Avg() const
98 {
100 }
101
102 /**
103 * Sample estimate of mean, alias to Avg
104 * \return the average of the sample
105 */
106 double Mean() const
107 {
108 return Avg();
109 }
110
111 /**
112 * Sample unbiased nbiased estimate of variance
113 * \return the unbiased nbiased estimate of variance
114 */
115 double Var() const
116 {
118 }
119
120 /**
121 * Sample standard deviation
122 * \return the standard deviation
123 */
124 double Stddev() const
125 {
126 return std::sqrt(Var());
127 }
128
129 /**
130 * \name Error of the mean estimates
131 *
132 * @{
133 */
134 /**
135 * \brief Margin of error of the mean for 90% confidence level
136 *
137 * Note that estimates are valid for
138 * - uncorrelated measurements,
139 * - normal distribution and
140 * - large enough sample size.
141 *
142 * \returns Margin of error of the mean for 90% confidence level
143 */
144 double Error90() const
145 {
146 return 1.645 * std::sqrt(Var() / Count());
147 }
148
149 /**
150 * \brief Margin of error of the mean for 95% confidence level
151 *
152 * Note that estimates are valid for
153 * - uncorrelated measurements,
154 * - normal distribution and
155 * - large enough sample size.
156 *
157 * \returns Margin of error of the mean for 95% confidence level
158 */
159 double Error95() const
160 {
161 return 1.960 * std::sqrt(Var() / Count());
162 }
163
164 /**
165 * \brief Margin of error of the mean for 99% confidence level
166 *
167 * Note that estimates are valid for
168 * - uncorrelated measurements,
169 * - normal distribution and
170 * - large enough sample size.
171 *
172 * \returns Margin of error of the mean for 99% confidence level
173 *
174 */
175 double Error99() const
176 {
177 return 2.576 * std::sqrt(Var() / Count());
178 }
179
180 /**@}*/
181
182 private:
183 uint32_t m_size; //!< Number of sampled data.
184 T m_min; //!< Minimum value observed.
185 T m_max; //!< Maximum value observed.
187};
188
189/**
190 * Print avg (err) [min, max]
191 * \param os The output stream
192 * \param x The Average value to print
193 * \return the output stream.
194 */
195template <typename T>
196std::ostream&
197operator<<(std::ostream& os, const Average<T>& x)
198{
199 if (x.Count() != 0)
200 {
201 os << x.Avg() << " (" << x.Stddev() << ") [" << x.Min() << ", " << x.Max() << "]";
202 }
203 else
204 {
205 os << "NA"; // not available
206 }
207 return os;
208}
209} // namespace ns3
210#endif /* AVERAGE_H */
Simple average, min, max and std.
Definition average.h:32
T m_max
Maximum value observed.
Definition average.h:185
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition average.h:144
double Avg() const
Sample average.
Definition average.h:97
T Min() const
Sample minimum.
Definition average.h:79
double Var() const
Sample unbiased nbiased estimate of variance.
Definition average.h:115
T Max() const
Sample maximum.
Definition average.h:88
uint32_t m_size
Number of sampled data.
Definition average.h:183
T m_min
Minimum value observed.
Definition average.h:184
void Reset()
Reset statistics.
Definition average.h:56
void Update(const T &x)
Add new sample.
Definition average.h:45
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition average.h:159
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition average.h:175
uint32_t Count() const
Sample size.
Definition average.h:70
MinMaxAvgTotalCalculator< double > m_varianceCalculator
Variance calculator.
Definition average.h:186
double Stddev() const
Sample standard deviation.
Definition average.h:124
double Mean() const
Sample estimate of mean, alias to Avg.
Definition average.h:106
Template class MinMaxAvgTotalCalculator.
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:148
STL namespace.