File indexing completed on 2024-04-21 05:50:08

0001 /*
0002     SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
0003     SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "stats.h"
0009 
0010 //------------------------------------------------------------------------------
0011 // Name: KStats
0012 // Desc: constructor
0013 //------------------------------------------------------------------------------
0014 KStats::KStats() = default;
0015 
0016 //------------------------------------------------------------------------------
0017 // Name: ~KStats
0018 // Desc: destructor
0019 //------------------------------------------------------------------------------
0020 KStats::~KStats() = default;
0021 
0022 //------------------------------------------------------------------------------
0023 // Name: clearAll
0024 // Desc: empties the data set
0025 //------------------------------------------------------------------------------
0026 void KStats::clearAll()
0027 {
0028     data_.clear();
0029 }
0030 
0031 //------------------------------------------------------------------------------
0032 // Name: enterData
0033 // Desc: adds an item to the data set
0034 //------------------------------------------------------------------------------
0035 void KStats::enterData(const KNumber &data)
0036 {
0037     data_.push_back(data);
0038 }
0039 
0040 //------------------------------------------------------------------------------
0041 // Name: clearLast
0042 // Desc: removes the last item from the data set
0043 //------------------------------------------------------------------------------
0044 void KStats::clearLast()
0045 {
0046     if (!data_.isEmpty()) {
0047         data_.pop_back();
0048     }
0049 }
0050 
0051 //------------------------------------------------------------------------------
0052 // Name: sum
0053 // Desc: calculates the SUM of all values in the data set
0054 //------------------------------------------------------------------------------
0055 KNumber KStats::sum() const
0056 {
0057     KNumber result = KNumber::Zero;
0058     for (const KNumber &x : std::as_const(data_)) {
0059         result += x;
0060     }
0061 
0062     return result;
0063 }
0064 
0065 //------------------------------------------------------------------------------
0066 // Name: median
0067 // Desc: calculates the MEDIAN of all values in the data set
0068 //------------------------------------------------------------------------------
0069 KNumber KStats::median()
0070 {
0071     KNumber result = KNumber::Zero;
0072     size_t index;
0073 
0074     unsigned int bound = count();
0075 
0076     if (bound == 0) {
0077         error_flag_ = true;
0078         return KNumber::Zero;
0079     }
0080 
0081     if (bound == 1)
0082         return data_.at(0);
0083 
0084     // need to copy data_-list, because sorting afterwards
0085     QList<KNumber> tmp_data(data_);
0086     std::sort(tmp_data.begin(), tmp_data.end());
0087 
0088     if (bound & 1) { // odd
0089         index = (bound - 1) / 2 + 1;
0090         result = tmp_data.at(index - 1);
0091     } else { // even
0092         index = bound / 2;
0093         result = ((tmp_data.at(index - 1)) + (tmp_data.at(index))) / KNumber(2);
0094     }
0095 
0096     return result;
0097 }
0098 
0099 //------------------------------------------------------------------------------
0100 // Name: std_kernel
0101 // Desc: calculates the STD Kernel of all values in the data set
0102 //------------------------------------------------------------------------------
0103 KNumber KStats::std_kernel()
0104 {
0105     KNumber result = KNumber::Zero;
0106     const KNumber mean_value = mean();
0107 
0108     if (mean_value.type() != KNumber::TYPE_ERROR) {
0109         for (const KNumber &x : std::as_const(data_)) {
0110             result += (x - mean_value) * (x - mean_value);
0111         }
0112     }
0113 
0114     return result;
0115 }
0116 
0117 //------------------------------------------------------------------------------
0118 // Name: sum_of_squares
0119 // Desc: calculates the SUM of all values in the data set (each squared)
0120 //------------------------------------------------------------------------------
0121 KNumber KStats::sum_of_squares() const
0122 {
0123     KNumber result = KNumber::Zero;
0124 
0125     for (const KNumber &x : std::as_const(data_)) {
0126         result += (x * x);
0127     }
0128 
0129     return result;
0130 }
0131 
0132 //------------------------------------------------------------------------------
0133 // Name: mean
0134 // Desc: calculates the MEAN of all values in the data set
0135 //------------------------------------------------------------------------------
0136 KNumber KStats::mean()
0137 {
0138     if (data_.isEmpty()) {
0139         error_flag_ = true;
0140         return KNumber::Zero;
0141     }
0142 
0143     return (sum() / KNumber(count()));
0144 }
0145 
0146 //------------------------------------------------------------------------------
0147 // Name: std
0148 // Desc: calculates the STANDARD DEVIATION of all values in the data set
0149 //------------------------------------------------------------------------------
0150 KNumber KStats::std()
0151 {
0152     if (data_.isEmpty()) {
0153         error_flag_ = true;
0154         return KNumber::Zero;
0155     }
0156 
0157     return (std_kernel() / KNumber(count())).sqrt();
0158 }
0159 
0160 //------------------------------------------------------------------------------
0161 // Name: sample_std
0162 // Desc: calculates the SAMPLE STANDARD DEVIATION of all values in the data set
0163 //------------------------------------------------------------------------------
0164 KNumber KStats::sample_std()
0165 {
0166     KNumber result = KNumber::Zero;
0167 
0168     if (count() < 2) {
0169         error_flag_ = true;
0170         return KNumber::Zero;
0171     }
0172 
0173     result = (std_kernel() / KNumber(count() - 1)).sqrt();
0174 
0175     return result;
0176 }
0177 
0178 //------------------------------------------------------------------------------
0179 // Name: count
0180 // Desc: returns the amount of values in the data set
0181 //------------------------------------------------------------------------------
0182 int KStats::count() const
0183 {
0184     return data_.size();
0185 }
0186 
0187 //------------------------------------------------------------------------------
0188 // Name: error
0189 // Desc: returns the error state AND clears it
0190 //------------------------------------------------------------------------------
0191 bool KStats::error()
0192 {
0193     bool value = error_flag_;
0194     error_flag_ = false;
0195     return value;
0196 }