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
18namespace statistics {
19
26template <typename T>
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
71template <typename T>
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
114
118void test_function(const float *test_data, const int number_of_samples) {
119 float mean = 0.f, variance = 0.f;
120
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
158int 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
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}
friend std::istream & operator>>(std::istream &input, stats_computer1 &stat)
friend std::istream & operator>>(std::istream &input, stats_computer2 &stat)
int main()
Main function.
Statistical algorithms.
void test_function(const float *test_data, const int number_of_samples)