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 * 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
25
26#include <cmath>
27#include <limits>
28#include <ostream>
29#include <stdint.h>
30
31namespace ns3
32{
33
34/**
35 * \ingroup stats
36 *
37 * Simple average, min, max and std. deviation calculator
38 *
39 */
40
41template <typename T = double>
43{
44 public:
46 : m_size(0),
47 m_min(std::numeric_limits<T>::max()),
48 m_max(0)
49 {
50 }
51
52 /**
53 * Add new sample
54 * \param x The sample
55 */
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
66 /// Reset statistics
67 void Reset()
68 {
70
71 m_size = 0;
72 m_min = std::numeric_limits<T>::max();
73 m_max = 0;
74 }
75
76 // Sample statistics
77 /**
78 * Sample size
79 * \return the sample size
80 */
82 {
83 return m_size;
84 }
85
86 /**
87 * Sample minimum
88 * \return the minimum of the sample
89 */
90 T Min() const
91 {
92 return m_min;
93 }
94
95 /**
96 * Sample maximum
97 * \return the maximum of the sample
98 */
99 T Max() const
100 {
101 return m_max;
102 }
103
104 /**
105 * Sample average
106 * \return the average of the sample
107 */
108 double Avg() const
109 {
111 }
112
113 /**
114 * Sample estimate of mean, alias to Avg
115 * \return the average of the sample
116 */
117 double Mean() const
118 {
119 return Avg();
120 }
121
122 /**
123 * Sample unbiased nbiased estimate of variance
124 * \return the unbiased nbiased estimate of variance
125 */
126 double Var() const
127 {
129 }
130
131 /**
132 * Sample standard deviation
133 * \return the standard deviation
134 */
135 double Stddev() const
136 {
137 return std::sqrt(Var());
138 }
139
140 /**
141 * \name Error of the mean estimates
142 *
143 * @{
144 */
145 /**
146 * \brief Margin of error of the mean for 90% confidence level
147 *
148 * Note that estimates are valid for
149 * - uncorrelated measurements,
150 * - normal distribution and
151 * - large enough sample size.
152 *
153 * \returns Margin of error of the mean for 90% confidence level
154 */
155 double Error90() const
156 {
157 return 1.645 * std::sqrt(Var() / Count());
158 }
159
160 /**
161 * \brief Margin of error of the mean for 95% confidence level
162 *
163 * Note that estimates are valid for
164 * - uncorrelated measurements,
165 * - normal distribution and
166 * - large enough sample size.
167 *
168 * \returns Margin of error of the mean for 95% confidence level
169 */
170 double Error95() const
171 {
172 return 1.960 * std::sqrt(Var() / Count());
173 }
174
175 /**
176 * \brief Margin of error of the mean for 99% confidence level
177 *
178 * Note that estimates are valid for
179 * - uncorrelated measurements,
180 * - normal distribution and
181 * - large enough sample size.
182 *
183 * \returns Margin of error of the mean for 99% confidence level
184 *
185 */
186 double Error99() const
187 {
188 return 2.576 * std::sqrt(Var() / Count());
189 }
190
191 /**@}*/
192
193 private:
194 uint32_t m_size; //!< Number of sampled data.
195 T m_min; //!< Minimum value observed.
196 T m_max; //!< Maximum value observed.
198};
199
200/**
201 * Print avg (err) [min, max]
202 * \param os The output stream
203 * \param x The Average value to print
204 * \return the output stream.
205 */
206template <typename T>
207std::ostream&
208operator<<(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 */
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
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:159
STL namespace.