A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
13
#include "
basic-data-calculators.h
"
14
15
#include <cmath>
16
#include <limits>
17
#include <ostream>
18
#include <stdint.h>
19
20
namespace
ns3
21
{
22
23
/**
24
* \ingroup stats
25
*
26
* Simple average, min, max and std. deviation calculator
27
*
28
*/
29
30
template
<
typename
T =
double
>
31
class
Average
32
{
33
public
:
34
Average
()
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.
48
m_varianceCalculator
.
Update
(x);
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
{
58
m_varianceCalculator
.
Reset
();
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
*/
70
uint32_t
Count
()
const
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
{
99
return
m_varianceCalculator
.
getMean
();
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
{
117
return
m_varianceCalculator
.
getVariance
();
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.
186
MinMaxAvgTotalCalculator<double>
m_varianceCalculator
;
//!< Variance calculator.
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
*/
195
template
<
typename
T>
196
std::ostream&
197
operator<<
(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 */
basic-data-calculators.h
ns3::Average
Simple average, min, max and std.
Definition
average.h:32
ns3::Average::m_max
T m_max
Maximum value observed.
Definition
average.h:185
ns3::Average::Error90
double Error90() const
Margin of error of the mean for 90% confidence level.
Definition
average.h:144
ns3::Average::Avg
double Avg() const
Sample average.
Definition
average.h:97
ns3::Average::Min
T Min() const
Sample minimum.
Definition
average.h:79
ns3::Average::Var
double Var() const
Sample unbiased nbiased estimate of variance.
Definition
average.h:115
ns3::Average::Max
T Max() const
Sample maximum.
Definition
average.h:88
ns3::Average::m_size
uint32_t m_size
Number of sampled data.
Definition
average.h:183
ns3::Average::m_min
T m_min
Minimum value observed.
Definition
average.h:184
ns3::Average::Average
Average()
Definition
average.h:34
ns3::Average::Reset
void Reset()
Reset statistics.
Definition
average.h:56
ns3::Average::Update
void Update(const T &x)
Add new sample.
Definition
average.h:45
ns3::Average::Error95
double Error95() const
Margin of error of the mean for 95% confidence level.
Definition
average.h:159
ns3::Average::Error99
double Error99() const
Margin of error of the mean for 99% confidence level.
Definition
average.h:175
ns3::Average::Count
uint32_t Count() const
Sample size.
Definition
average.h:70
ns3::Average::m_varianceCalculator
MinMaxAvgTotalCalculator< double > m_varianceCalculator
Variance calculator.
Definition
average.h:186
ns3::Average::Stddev
double Stddev() const
Sample standard deviation.
Definition
average.h:124
ns3::Average::Mean
double Mean() const
Sample estimate of mean, alias to Avg.
Definition
average.h:106
ns3::MinMaxAvgTotalCalculator
Template class MinMaxAvgTotalCalculator.
Definition
basic-data-calculators.h:30
ns3::MinMaxAvgTotalCalculator::Reset
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
Definition
basic-data-calculators.h:257
ns3::MinMaxAvgTotalCalculator::getVariance
double getVariance() const override
Returns the current variance.
Definition
basic-data-calculators.h:115
ns3::MinMaxAvgTotalCalculator::getMean
double getMean() const override
Returns the mean value.
Definition
basic-data-calculators.h:97
ns3::MinMaxAvgTotalCalculator::Update
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Definition
basic-data-calculators.h:196
uint32_t
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition
angles.cc:148
std
STL namespace.
src
stats
model
average.h
Generated on Fri Nov 8 2024 13:59:06 for ns-3 by
1.11.0