39 template <
typename T =
int64_t>
42 static_assert(std::is_arithmetic<T>::value,
"Statistics requres an arithmetic type.");
54 : sampleSize(other.sampleSize), M1(other.M1), M2(other.M2), M3(other.M3), M4(other.M4), min(other.min), max(other.max)
70 combined.sampleSize = this->sampleSize + other.sampleSize;
72 const auto delta = other.M1 - this->M1;
73 const auto delta2 = delta * delta;
74 const auto delta3 = delta * delta2;
75 const auto delta4 = delta2 * delta2;
77 combined.M1 = (this->sampleSize * this->M1 + other.sampleSize * other.M1) / combined.sampleSize;
79 combined.M2 = this->M2 + other.M2 + delta2 * this->sampleSize * other.sampleSize / combined.sampleSize;
83 + delta3 * this->sampleSize * other.sampleSize * (this->sampleSize - other.sampleSize) / (combined.sampleSize * combined.sampleSize);
85 combined.M3 += 3.0 * delta * (this->sampleSize * other.M2 - other.sampleSize * this->M2) / combined.sampleSize;
87 combined.M4 = this->M4 + other.M4
88 + delta4 * this->sampleSize * other.sampleSize
89 * (this->sampleSize * this->sampleSize - this->sampleSize * other.sampleSize + other.sampleSize * other.sampleSize)
90 / (combined.sampleSize * combined.sampleSize * combined.sampleSize);
92 combined.M4 += 6.0 * delta2 * (this->sampleSize * this->sampleSize * other.M2 + other.sampleSize * other.sampleSize * this->M2)
93 / (combined.sampleSize * combined.sampleSize)
94 + 4.0 * delta * (this->sampleSize * other.M3 - other.sampleSize * this->M3) / combined.sampleSize;
96 combined.min = std::min(this->min, other.min);
97 combined.max = std::max(this->max, other.max);
104 const auto combined = *
this + other;
111 this->sampleSize = other.sampleSize;
116 this->min = other.min;
117 this->max = other.max;
127 this->sampleSize = 0;
132 this->min = std::numeric_limits<decltype(this->min)>::max();
133 this->max = std::numeric_limits<decltype(this->max)>::min();
141 const auto n1 = this->sampleSize;
144 const auto delta = x - this->M1;
145 const auto delta_n = delta /
static_cast<decltype(delta)
>(this->sampleSize);
146 const auto delta_n2 = delta_n * delta_n;
147 const auto term1 = delta * delta_n * n1;
150 this->M4 += term1 * delta_n2 * (this->sampleSize * this->sampleSize - 3 * this->sampleSize + 3) + 6 * delta_n2 * this->M2
151 - 4 * delta_n * this->M3;
152 this->M3 += term1 * delta_n * (this->sampleSize - 2) - 3 * delta_n * this->M2;
155 this->min = std::min(this->min, x);
156 this->max = std::max(this->max, x);
159 size_t getSize()
const 161 return this->sampleSize;
164 double getMean()
const 169 double getVariance()
const 171 if(this->sampleSize > 1)
173 return this->M2 / (this->sampleSize - 1);
179 double getStandardDeviation()
const 181 return std::sqrt(this->getVariance());
184 double getSkewness()
const 186 if(this->sampleSize > 2)
188 return sqrt(this->sampleSize) * this->M3 / pow(this->M2, 1.5);
194 double getKurtosis()
const 196 if(this->sampleSize > 3)
200 return this->sampleSize * this->M4 / (this->M2 * this->M2) - 3.0;
214 const auto sd = this->getStandardDeviation();
218 return (this->getMean() - static_cast<double>(this->getMin())) / sd;
235 size_t sampleSize{0};
240 T min{std::numeric_limits<T>::max()};
241 T max{std::numeric_limits<T>::min()};
double getZScore() const
Computed as (mean - hypothesis)/standard_deviation.
Definition: Statistics.h:212
Definition: Statistics.h:40
Statistics()=default
Default constructor.
void addSample(T x)
Adds a statistical sample.
Definition: Statistics.h:139
void reset()
Resets all accumulated statistics.
Definition: Statistics.h:125