-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatistics.h
109 lines (92 loc) · 2.87 KB
/
statistics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2015-2019 Free Software Foundation, Inc.
// Copyright (C) 2020-2022 Edward M. Smith-Rowland
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or (at
// your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// statistics.h
#ifndef STATISTICS_H
#define STATISTICS_H 1
#include <emsr/complex_util.h>
/**
* Incremental computation of statistics.
*
* @todo Make an insertion operator (operator<<) that takes an object
* decomposable into two parts. The first part will be the value
* and the second part will be the weight. Then _M_count or something
* will need to be of type _Tp rather than unsigned integral.
*/
template<typename _Tp>
struct _Statistics
{
_Statistics&
operator<<(_Tp __diff)
{
++_M_count;
auto __old_mean = _M_mean;
_M_mean = (_M_type(__diff) + _M_type(_M_count - 1) * _M_mean)
/ _M_type(_M_count);
auto __del_mean = _M_mean - __old_mean;
auto __del_diff = _M_type(__diff) - _M_mean;
if (_M_count > 1)
_M_variance = (_M_type(_M_count - 2) * _M_variance * _M_variance
+ _M_type(_M_count - 1) * __del_mean * __del_mean
+ __del_diff * __del_diff) / _M_type(_M_count - 1);
if (__diff < _M_min)
{
_M_min = __diff;
_M_min_index = _M_count - 1;
}
if (__diff > _M_max)
{
_M_max = __diff;
_M_max_index = _M_count - 1;
}
return *this;
}
static constexpr bool _M_is_complex = emsr::is_complex_v<_Tp>;
using _M_type = std::conditional_t<emsr::is_complex_v<_Tp>,
std::complex<long double>, long double>;
_Tp
count() const
{ return _Tp(_M_count); }
_Tp
mean() const
{ return _Tp(_M_mean); }
_Tp
variance() const
{ return _Tp(_M_variance); }
_Tp
std_deviation() const
{ return _Tp(std::sqrt(_M_variance)); }
_Tp
min() const
{ return _Tp(_M_min); }
std::size_t
min_index() const
{ return _M_min_index; }
_Tp
max() const
{ return _Tp(_M_max); }
std::size_t
max_index() const
{ return _M_max_index; }
std::size_t _M_count = 0;
std::size_t _M_min_index = -1;
std::size_t _M_max_index = -1;
_M_type _M_mean = 0;
_M_type _M_variance = 0;
_M_type _M_min = std::numeric_limits<long double>::max();
_M_type _M_max = std::numeric_limits<long double>::lowest();
};
#endif // STATISTICS_H