1#pragma once
2
3#include <array>
4#include <string>
5#include <map>
6
7/**
8 * About:
9 * This is implementation of Independent two-sample t-test
10 * Read about it on https://en.wikipedia.org/wiki/Student%27s_t-test (Equal or unequal sample sizes, equal variance)
11 *
12 * Usage:
13 * It's it used to assume with some level of confidence that two distributions don't differ.
14 * Values can be added with t_test.add(0/1, value) and after compared and reported with compareAndReport().
15 */
16class StudentTTest
17{
18private:
19 struct DistributionData
20 {
21 size_t size = 0;
22 double sum = 0;
23 double squares_sum = 0;
24
25 void add(double value)
26 {
27 ++size;
28 sum += value;
29 squares_sum += value * value;
30 }
31
32 double avg() const
33 {
34 return sum / size;
35 }
36
37 double var() const
38 {
39 return (squares_sum - (sum * sum / size)) / static_cast<double>(size - 1);
40 }
41
42 void clear()
43 {
44 size = 0;
45 sum = 0;
46 squares_sum = 0;
47 }
48 };
49
50 std::array<DistributionData, 2> data {};
51
52public:
53 void clear();
54
55 void add(size_t distribution, double value);
56
57 /// Confidence_level_index can be set in range [0, 5]. Corresponding values can be found above. TODO: Trash - no separation of concepts in code.
58 std::pair<bool, std::string> compareAndReport(size_t confidence_level_index = 5) const;
59};
60