1 | /* |
2 | SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com> |
3 | SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org> |
4 | |
5 | SPDX-License-Identifier: GPL-2.0-or-later |
6 | */ |
7 | |
8 | #include "stats.h" |
9 | |
10 | //------------------------------------------------------------------------------ |
11 | // Name: KStats |
12 | // Desc: constructor |
13 | //------------------------------------------------------------------------------ |
14 | KStats::KStats() = default; |
15 | |
16 | //------------------------------------------------------------------------------ |
17 | // Name: ~KStats |
18 | // Desc: destructor |
19 | //------------------------------------------------------------------------------ |
20 | KStats::~KStats() = default; |
21 | |
22 | //------------------------------------------------------------------------------ |
23 | // Name: clearAll |
24 | // Desc: empties the data set |
25 | //------------------------------------------------------------------------------ |
26 | void KStats::clearAll() |
27 | { |
28 | data_.clear(); |
29 | } |
30 | |
31 | //------------------------------------------------------------------------------ |
32 | // Name: enterData |
33 | // Desc: adds an item to the data set |
34 | //------------------------------------------------------------------------------ |
35 | void KStats::enterData(const KNumber &data) |
36 | { |
37 | data_.push_back(data); |
38 | } |
39 | |
40 | //------------------------------------------------------------------------------ |
41 | // Name: clearLast |
42 | // Desc: removes the last item from the data set |
43 | //------------------------------------------------------------------------------ |
44 | void KStats::clearLast() |
45 | { |
46 | if (!data_.isEmpty()) { |
47 | data_.pop_back(); |
48 | } |
49 | } |
50 | |
51 | //------------------------------------------------------------------------------ |
52 | // Name: sum |
53 | // Desc: calculates the SUM of all values in the data set |
54 | //------------------------------------------------------------------------------ |
55 | KNumber KStats::sum() const |
56 | { |
57 | KNumber result = KNumber::Zero; |
58 | for (const KNumber &x : std::as_const(data_)) { |
59 | result += x; |
60 | } |
61 | |
62 | return result; |
63 | } |
64 | |
65 | //------------------------------------------------------------------------------ |
66 | // Name: median |
67 | // Desc: calculates the MEDIAN of all values in the data set |
68 | //------------------------------------------------------------------------------ |
69 | KNumber KStats::median() |
70 | { |
71 | KNumber result = KNumber::Zero; |
72 | size_t index; |
73 | |
74 | unsigned int bound = count(); |
75 | |
76 | if (bound == 0) { |
77 | error_flag_ = true; |
78 | return KNumber::Zero; |
79 | } |
80 | |
81 | if (bound == 1) |
82 | return data_.at(0); |
83 | |
84 | // need to copy data_-list, because sorting afterwards |
85 | QList<KNumber> tmp_data(data_); |
86 | std::sort(tmp_data.begin(), tmp_data.end()); |
87 | |
88 | if (bound & 1) { // odd |
89 | index = (bound - 1) / 2 + 1; |
90 | result = tmp_data.at(index - 1); |
91 | } else { // even |
92 | index = bound / 2; |
93 | result = ((tmp_data.at(index - 1)) + (tmp_data.at(index))) / KNumber(2); |
94 | } |
95 | |
96 | return result; |
97 | } |
98 | |
99 | //------------------------------------------------------------------------------ |
100 | // Name: std_kernel |
101 | // Desc: calculates the STD Kernel of all values in the data set |
102 | //------------------------------------------------------------------------------ |
103 | KNumber KStats::std_kernel() |
104 | { |
105 | KNumber result = KNumber::Zero; |
106 | const KNumber mean_value = mean(); |
107 | |
108 | if (mean_value.type() != KNumber::TYPE_ERROR) { |
109 | for (const KNumber &x : std::as_const(data_)) { |
110 | result += (x - mean_value) * (x - mean_value); |
111 | } |
112 | } |
113 | |
114 | return result; |
115 | } |
116 | |
117 | //------------------------------------------------------------------------------ |
118 | // Name: sum_of_squares |
119 | // Desc: calculates the SUM of all values in the data set (each squared) |
120 | //------------------------------------------------------------------------------ |
121 | KNumber KStats::sum_of_squares() const |
122 | { |
123 | KNumber result = KNumber::Zero; |
124 | |
125 | for (const KNumber &x : std::as_const(data_)) { |
126 | result += (x * x); |
127 | } |
128 | |
129 | return result; |
130 | } |
131 | |
132 | //------------------------------------------------------------------------------ |
133 | // Name: mean |
134 | // Desc: calculates the MEAN of all values in the data set |
135 | //------------------------------------------------------------------------------ |
136 | KNumber KStats::mean() |
137 | { |
138 | if (data_.isEmpty()) { |
139 | error_flag_ = true; |
140 | return KNumber::Zero; |
141 | } |
142 | |
143 | return (sum() / KNumber(count())); |
144 | } |
145 | |
146 | //------------------------------------------------------------------------------ |
147 | // Name: std |
148 | // Desc: calculates the STANDARD DEVIATION of all values in the data set |
149 | //------------------------------------------------------------------------------ |
150 | KNumber KStats::std() |
151 | { |
152 | if (data_.isEmpty()) { |
153 | error_flag_ = true; |
154 | return KNumber::Zero; |
155 | } |
156 | |
157 | return (std_kernel() / KNumber(count())).sqrt(); |
158 | } |
159 | |
160 | //------------------------------------------------------------------------------ |
161 | // Name: sample_std |
162 | // Desc: calculates the SAMPLE STANDARD DEVIATION of all values in the data set |
163 | //------------------------------------------------------------------------------ |
164 | KNumber KStats::sample_std() |
165 | { |
166 | KNumber result = KNumber::Zero; |
167 | |
168 | if (count() < 2) { |
169 | error_flag_ = true; |
170 | return KNumber::Zero; |
171 | } |
172 | |
173 | result = (std_kernel() / KNumber(count() - 1)).sqrt(); |
174 | |
175 | return result; |
176 | } |
177 | |
178 | //------------------------------------------------------------------------------ |
179 | // Name: count |
180 | // Desc: returns the amount of values in the data set |
181 | //------------------------------------------------------------------------------ |
182 | int KStats::count() const |
183 | { |
184 | return data_.size(); |
185 | } |
186 | |
187 | //------------------------------------------------------------------------------ |
188 | // Name: error |
189 | // Desc: returns the error state AND clears it |
190 | //------------------------------------------------------------------------------ |
191 | bool KStats::error() |
192 | { |
193 | bool value = error_flag_; |
194 | error_flag_ = false; |
195 | return value; |
196 | } |
197 | |