Line data Source code
1 : /**
2 : * Unit tests for the Statistics module's probabilistic distribution methods
3 : */
4 : #include <cmath>
5 : #include <cstdlib>
6 : #include <gtest/gtest.h>
7 : #include <iostream>
8 : #include <openGPMP/stats/probdist.hpp>
9 : #include <stdexcept>
10 : #include <vector>
11 :
12 : // Tolerance for floating-point comparisons
13 : const double TOLERANCE = 1e-2;
14 :
15 : // Unit test for quantile_dist method
16 4 : TEST(ProbDistTest, QuantileDist) {
17 : gpmp::stats::ProbDist probDist;
18 : // median probability
19 1 : double probability = 0.5;
20 : // median of standard normal distribution
21 1 : double expected_quantile = 0.0;
22 :
23 1 : double actual_quantile = probDist.quantile_dist(probability);
24 1 : EXPECT_NEAR(expected_quantile, actual_quantile, TOLERANCE);
25 1 : }
26 :
27 4 : TEST(ProbDistTest, QuantileDist_ProbabilityZero) {
28 : gpmp::stats::ProbDist probDist;
29 :
30 1 : double probability = 0.0;
31 1 : double actual_quantile = probDist.quantile_dist(probability);
32 1 : EXPECT_EQ(0.0, actual_quantile);
33 1 : }
34 :
35 : // Unit test for normal_PDF method
36 4 : TEST(ProbDistTest, NormalPDF) {
37 : gpmp::stats::ProbDist probDist;
38 : // value at mean
39 1 : double x = 0.0;
40 : // mean of normal distribution
41 1 : double mean = 0.0;
42 : // standard deviation of normal distribution
43 1 : double stddev = 1.0;
44 :
45 : // calculated expected value based on input parameters
46 1 : double expected_pdf = 0.398942;
47 :
48 1 : double actual_pdf = probDist.normal_PDF(x, mean, stddev);
49 1 : EXPECT_NEAR(expected_pdf, actual_pdf, TOLERANCE);
50 1 : }
51 :
52 4 : TEST(ProbDistTest, NormalCDF) {
53 : gpmp::stats::ProbDist probDist;
54 :
55 : // value at mean
56 1 : double x = 0.0;
57 : // mean of normal distribution
58 1 : double mean = 0.0;
59 : // standard deviation of normal distribution
60 1 : double stddev = 1.0;
61 :
62 : // cumulative distribution function (CDF) at mean
63 1 : double expected_cdf = 0.5;
64 :
65 1 : double actual_cdf = probDist.normal_CDF(x, mean, stddev);
66 1 : EXPECT_NEAR(expected_cdf, actual_cdf, TOLERANCE);
67 1 : }
68 :
69 : // Unit test for uniform_CDF method
70 4 : TEST(ProbDistTest, UniformCDF) {
71 : gpmp::stats::ProbDist probDist;
72 :
73 : // current rank
74 1 : size_t k = 2;
75 : // items
76 1 : size_t n = 5;
77 :
78 1 : double expected_cdf = 0.333;
79 :
80 1 : double actual_cdf = probDist.uniform_CDF(k, n);
81 :
82 1 : EXPECT_NEAR(expected_cdf, actual_cdf, TOLERANCE);
83 1 : }
84 :
85 : // Unit test for emp_CDF method
86 4 : TEST(ProbDistTest, EmpCDF) {
87 : gpmp::stats::ProbDist probDist;
88 :
89 : // Setup
90 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
91 1 : double x = 3.0;
92 :
93 : // Expected empirical cumulative distribution function (CDF) value
94 1 : double expected_cdf = 0.6;
95 :
96 1 : double actual_cdf = probDist.emp_CDF(data, x);
97 :
98 1 : EXPECT_NEAR(expected_cdf, actual_cdf, TOLERANCE);
99 1 : }
100 :
101 4 : TEST(ProbDistTest, EmpPMF) {
102 : gpmp::stats::ProbDist probDist;
103 :
104 1 : std::vector<double> data = {1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0};
105 1 : double x = 3.0;
106 :
107 : // expected probability mass function (PMF) value
108 1 : double expected_pmf = 3.0 / data.size();
109 :
110 1 : double actual_pmf = probDist.emp_PMF(data, x);
111 :
112 1 : EXPECT_NEAR(expected_pmf, actual_pmf, TOLERANCE);
113 1 : }
114 :
115 4 : TEST(ProbDistTest, InverseEmpCDF) {
116 : gpmp::stats::ProbDist probDist;
117 :
118 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
119 : // probability value
120 1 : double p = 0.5;
121 :
122 : // expected inverse empirical cumulative distribution function (CDF) value
123 1 : double expected_result = 3.0;
124 :
125 1 : double actual_result = probDist.inverse_emp_CDF(data, p);
126 :
127 1 : EXPECT_EQ(expected_result, actual_result);
128 1 : }
129 :
130 : // Unit test for mle method
131 4 : TEST(ProbDistTest, MLE) {
132 : gpmp::stats::ProbDist probDist;
133 :
134 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
135 :
136 : // expected maximum likelihood estimate (MLE)
137 1 : double expected_result = 3.0;
138 :
139 1 : double actual_result = probDist.mle(data);
140 1 : EXPECT_EQ(expected_result, actual_result);
141 1 : }
142 :
143 : // Unit test for mom method
144 4 : TEST(ProbDistTest, MOM) {
145 : gpmp::stats::ProbDist probDist;
146 :
147 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
148 :
149 : // method of moments xpected result
150 1 : double expected_result = 2;
151 :
152 1 : double actual_result = probDist.mom(data);
153 1 : EXPECT_NEAR(expected_result, actual_result, TOLERANCE);
154 1 : }
155 :
156 : // Unit test for mle_est method
157 4 : TEST(ProbDistTest, MLEEst) {
158 : gpmp::stats::ProbDist probDist;
159 :
160 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
161 :
162 : // maximum likelihood estimation
163 1 : double expected_result = 3.0;
164 :
165 1 : double actual_result = probDist.mle_est(data);
166 :
167 1 : EXPECT_NEAR(expected_result, actual_result, TOLERANCE);
168 1 : }
169 :
170 : // Unit test for mumv method
171 4 : TEST(ProbDistTest, MUMV) {
172 : gpmp::stats::ProbDist probDist;
173 :
174 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
175 :
176 : // mean of unordered multiset values (MUMV)
177 1 : double expected_result = 3.0;
178 :
179 1 : double actual_result = probDist.mumv(data);
180 1 : EXPECT_NEAR(expected_result, actual_result, TOLERANCE);
181 1 : }
182 :
183 4 : TEST(ProbDistTest, MedianUnbiased) {
184 : gpmp::stats::ProbDist probDist;
185 :
186 : // sample data (odd number of elements)
187 1 : std::vector<double> data_odd = {1.0, 2.0, 3.0, 4.0, 5.0};
188 :
189 : // sample data (even number of elements)
190 1 : std::vector<double> data_even = {1.0, 2.0, 3.0, 4.0};
191 :
192 1 : double expected_result_odd = 3.0;
193 1 : double expected_result_even = 2.5;
194 :
195 1 : double actual_result_odd = probDist.median_uniased(data_odd);
196 1 : double actual_result_even = probDist.median_uniased(data_even);
197 :
198 : // Assertions
199 1 : EXPECT_NEAR(expected_result_odd, actual_result_odd, TOLERANCE);
200 1 : EXPECT_NEAR(expected_result_even, actual_result_even, TOLERANCE);
201 1 : }
202 :
203 : // Unit test for ConfidenceInterval method
204 4 : TEST(ProbDistTest, ConfidenceInterval) {
205 : gpmp::stats::ProbDist probDist;
206 :
207 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
208 :
209 : // sig level
210 1 : double alpha = 0.05;
211 :
212 1 : auto interval = probDist.ConfidenceInterval(data, alpha);
213 :
214 1 : EXPECT_LT(interval.first, interval.second);
215 1 : }
216 :
217 4 : TEST(ProbDistTest, LikelihoodInterval) {
218 : gpmp::stats::ProbDist probDist;
219 :
220 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
221 : // sig level
222 1 : double alpha = 0.05;
223 :
224 1 : auto interval = probDist.LikelihoodInterval(data, alpha);
225 :
226 1 : EXPECT_LT(interval.first, interval.second);
227 1 : }
228 :
229 4 : TEST(ProbDistTest, PredictionInterval) {
230 : gpmp::stats::ProbDist probDist;
231 :
232 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
233 1 : double alpha = 0.05;
234 :
235 1 : auto interval = probDist.PredictionInterval(data, alpha);
236 :
237 1 : EXPECT_LT(interval.first, interval.second);
238 1 : }
239 :
240 4 : TEST(ProbDistTest, ToleranceInterval) {
241 : gpmp::stats::ProbDist probDist;
242 :
243 1 : std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
244 1 : double alpha = 0.05;
245 :
246 1 : auto interval = probDist.ToleranceInterval(data, alpha);
247 :
248 1 : EXPECT_LT(interval.first, interval.second);
249 1 : }
|