TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
realtime_stats.cpp
Go to the documentation of this file.
1
10
#include <cassert>
11
#include <cmath>
12
#include <iostream>
13
18
namespace
statistics
{
19
26
template
<
typename
T>
27
class
stats_computer1
{
28
public
:
32
void
new_val
(T x) {
33
if
(n == 0)
34
K = x;
35
n++;
36
T tmp = x - K;
37
Ex += tmp;
38
Ex2 +=
static_cast<
double
>
(tmp) * tmp;
39
}
40
42
double
mean
()
const
{
return
K + Ex / n; }
43
45
double
variance
()
const
{
return
(Ex2 - (Ex * Ex) / n) / (n - 1); }
46
48
double
std
()
const
{
return
std::sqrt(this->
variance
()); }
49
53
friend
std::istream &
operator>>
(std::istream &input,
54
stats_computer1
&stat) {
55
T val;
56
input >> val;
57
stat.
new_val
(val);
58
return
input;
59
}
60
61
private
:
62
unsigned
int
n = 0;
63
double
Ex, Ex2;
64
T K;
65
};
66
71
template
<
typename
T>
72
class
stats_computer2
{
73
public
:
77
void
new_val
(T x) {
78
n++;
79
double
delta = x - mu;
80
mu += delta / n;
81
double
delta2 = x - mu;
82
M += delta * delta2;
83
}
84
86
double
mean
()
const
{
return
mu; }
87
89
double
variance
()
const
{
return
M / n; }
90
92
double
std
()
const
{
return
std::sqrt(this->
variance
()); }
93
97
friend
std::istream &
operator>>
(std::istream &input,
98
stats_computer2
&stat) {
99
T val;
100
input >> val;
101
stat.
new_val
(val);
102
return
input;
103
}
104
105
private
:
106
unsigned
int
n = 0;
107
double
mu = 0, var = 0, M = 0;
108
};
109
110
}
// namespace statistics
111
112
using
statistics::stats_computer1
;
113
using
statistics::stats_computer2
;
114
118
void
test_function
(
const
float
*test_data,
const
int
number_of_samples) {
119
float
mean = 0.f, variance = 0.f;
120
121
stats_computer1<float>
stats01;
122
stats_computer2<float>
stats02;
123
124
for
(
int
i = 0; i < number_of_samples; i++) {
125
stats01.
new_val
(test_data[i]);
126
stats02.
new_val
(test_data[i]);
127
mean += test_data[i];
128
}
129
130
mean /= number_of_samples;
131
132
for
(
int
i = 0; i < number_of_samples; i++) {
133
float
temp = test_data[i] - mean;
134
variance += temp * temp;
135
}
136
variance /= number_of_samples;
137
138
std::cout <<
"<<<<<<<< Test Function >>>>>>>>"
<< std::endl
139
<<
"Expected: Mean: "
<< mean <<
"\t Variance: "
<< variance
140
<< std::endl;
141
std::cout <<
"\tMethod 1:"
142
<<
"\tMean: "
<< stats01.
mean
()
143
<<
"\t Variance: "
<< stats01.
variance
()
144
<<
"\t Std: "
<< stats01.
std
() << std::endl;
145
std::cout <<
"\tMethod 2:"
146
<<
"\tMean: "
<< stats02.
mean
()
147
<<
"\t Variance: "
<< stats02.
variance
()
148
<<
"\t Std: "
<< stats02.
std
() << std::endl;
149
150
assert(std::abs(stats01.
mean
() - mean) < 0.01);
151
assert(std::abs(stats02.
mean
() - mean) < 0.01);
152
assert(std::abs(stats02.
variance
() - variance) < 0.01);
153
154
std::cout <<
"(Tests passed)"
<< std::endl;
155
}
156
158
int
main
(
int
argc,
char
**argv) {
159
const
float
test_data1[] = {3, 4, 5, -1.4, -3.6, 1.9, 1.};
160
test_function
(test_data1,
sizeof
(test_data1) /
sizeof
(test_data1[0]));
161
162
std::cout
163
<<
"Enter data. Any non-numeric data will terminate the data input."
164
<< std::endl;
165
166
stats_computer1<float>
stats1;
167
stats_computer2<float>
stats2;
168
169
while
(1) {
170
double
val;
171
std::cout <<
"Enter number: "
;
172
std::cin >> val;
173
174
// check for failure to read input. Happens for
175
// non-numeric data
176
if
(std::cin.fail())
177
break
;
178
179
stats1.
new_val
(val);
180
stats2.
new_val
(val);
181
182
std::cout <<
"\tMethod 1:"
183
<<
"\tMean: "
<< stats1.
mean
()
184
<<
"\t Variance: "
<< stats1.
variance
()
185
<<
"\t Std: "
<< stats1.
std
() << std::endl;
186
std::cout <<
"\tMethod 2:"
187
<<
"\tMean: "
<< stats2.
mean
()
188
<<
"\t Variance: "
<< stats2.
variance
()
189
<<
"\t Std: "
<< stats2.
std
() << std::endl;
190
}
191
192
return
0;
193
}
statistics::stats_computer1
Definition
realtime_stats.cpp:27
statistics::stats_computer1::variance
double variance() const
Definition
realtime_stats.cpp:45
statistics::stats_computer1::operator>>
friend std::istream & operator>>(std::istream &input, stats_computer1 &stat)
Definition
realtime_stats.cpp:53
statistics::stats_computer1::mean
double mean() const
Definition
realtime_stats.cpp:42
statistics::stats_computer1::new_val
void new_val(T x)
Definition
realtime_stats.cpp:32
statistics::stats_computer1::std
double std() const
Definition
realtime_stats.cpp:48
statistics::stats_computer2
Definition
realtime_stats.cpp:72
statistics::stats_computer2::mean
double mean() const
Definition
realtime_stats.cpp:86
statistics::stats_computer2::operator>>
friend std::istream & operator>>(std::istream &input, stats_computer2 &stat)
Definition
realtime_stats.cpp:97
statistics::stats_computer2::std
double std() const
Definition
realtime_stats.cpp:92
statistics::stats_computer2::new_val
void new_val(T x)
Definition
realtime_stats.cpp:77
statistics::stats_computer2::variance
double variance() const
Definition
realtime_stats.cpp:89
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
statistics
Statistical algorithms.
test_function
void test_function(const float *test_data, const int number_of_samples)
Definition
realtime_stats.cpp:118
math
realtime_stats.cpp
Generated by
1.12.0