@@ -29,6 +29,11 @@ public function testMean(): void
2929 isSame (1.0 , Stats::mean ([1 ]));
3030 isSame (1.0 , Stats::mean ([1 , 1 ]));
3131 isSame (2.0 , Stats::mean ([1 , 3 ]));
32+ isSame (2.0 , Stats::mean (['1 ' , 3 ]));
33+ isSame (2.25 , Stats::mean (['1.5 ' , 3 ]));
34+
35+ $ data = [72 , 57 , 66 , 92 , 32 , 17 , 146 ];
36+ isSame (68.857142857 , Stats::mean ($ data ));
3237 }
3338
3439 public function testStdDev (): void
@@ -81,10 +86,120 @@ public function testHistogram(): void
8186
8287 public function testRenderAverage (): void
8388 {
84- isSame ('1.500±0.500 ' , Stats::renderAverage ([1 , 2 , 1 , 2 ]));
85- isSame ('1.5±0.5 ' , Stats::renderAverage ([1 , 2 , 1 , 2 ], 1 ));
86- isSame ('1.50±0.50 ' , Stats::renderAverage ([1 , 2 , 1 , 2 ], 2 ));
87- isSame ('2±1 ' , Stats::renderAverage ([1 , 2 , 1 , 2 ], 0 ));
88- isSame ('2±1 ' , Stats::renderAverage ([1 , 2 , 1 , 2 ], -1 ));
89+ $ data = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
90+ isSame ('5.500±2.872 ' , Stats::renderAverage ($ data ));
91+ isSame ('5.5±2.9 ' , Stats::renderAverage ($ data , 1 ));
92+ isSame ('5.50±2.87 ' , Stats::renderAverage ($ data , 2 ));
93+ isSame ('6±3 ' , Stats::renderAverage ($ data , 0 ));
94+ isSame ('6±3 ' , Stats::renderAverage ($ data , -1 ));
95+
96+ $ data = [72 , 57 , 66 , 92 , 32 , 17 , 146 ];
97+ isSame ('68.857±39.084 ' , Stats::renderAverage ($ data ));
98+ isSame ('68.9±39.1 ' , Stats::renderAverage ($ data , 1 ));
99+ isSame ('68.86±39.08 ' , Stats::renderAverage ($ data , 2 ));
100+ isSame ('69±39 ' , Stats::renderAverage ($ data , 0 ));
101+ isSame ('69±39 ' , Stats::renderAverage ($ data , -1 ));
102+ }
103+
104+ public function testRenderMedian (): void
105+ {
106+ $ data = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
107+ isSame ('5.500±2.872 ' , Stats::renderMedian ($ data ));
108+ isSame ('5.5±2.9 ' , Stats::renderMedian ($ data , 1 ));
109+ isSame ('5.50±2.87 ' , Stats::renderMedian ($ data , 2 ));
110+ isSame ('6±3 ' , Stats::renderMedian ($ data , 0 ));
111+ isSame ('6±3 ' , Stats::renderMedian ($ data , -1 ));
112+
113+ $ data = [72 , 57 , 66 , 92 , 32 , 17 , 146 ];
114+ isSame ('66.000±39.084 ' , Stats::renderMedian ($ data ));
115+ isSame ('66.0±39.1 ' , Stats::renderMedian ($ data , 1 ));
116+ isSame ('66.00±39.08 ' , Stats::renderMedian ($ data , 2 ));
117+ isSame ('66±39 ' , Stats::renderMedian ($ data , 0 ));
118+ isSame ('66±39 ' , Stats::renderMedian ($ data , -1 ));
119+ }
120+
121+ public function testPercentile (): void
122+ {
123+ $ data = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
124+ isSame (1.0 , Stats::percentile ($ data , 0 ));
125+ isSame (1.09 , Stats::percentile ($ data , 1 ));
126+ isSame (1.9 , Stats::percentile ($ data , 10 ));
127+ isSame (2.8 , Stats::percentile ($ data , 20 ));
128+ isSame (3.7 , Stats::percentile ($ data , 30 ));
129+ isSame (4.6 , Stats::percentile ($ data , 40 ));
130+ isSame (5.5 , Stats::percentile ($ data , 50 ));
131+ isSame (6.4 , Stats::percentile ($ data , 60 ));
132+ isSame (7.3 , Stats::percentile ($ data , 70 ));
133+ isSame (8.2 , Stats::percentile ($ data , 80 ));
134+ isSame (9.1 , Stats::percentile ($ data , 90 ));
135+ isSame (9.55 , Stats::percentile ($ data ));
136+ isSame (9.91 , Stats::percentile ($ data , 99 ));
137+ isSame (9.9991 , Stats::percentile ($ data , 99.99 ));
138+ isSame (10.0 , Stats::percentile ($ data , 100 ));
139+
140+ $ data = [72 , 57 , 66 , 92 , 32 , 17 , 146 ];
141+ isSame (17.0 , Stats::percentile ($ data , 0 ));
142+ isSame (17.9 , Stats::percentile ($ data , 1 ));
143+ isSame (26.0 , Stats::percentile ($ data , 10 ));
144+ isSame (37.0 , Stats::percentile ($ data , 20 ));
145+ isSame (52.0 , Stats::percentile ($ data , 30 ));
146+ isSame (60.6 , Stats::percentile ($ data , 40 ));
147+ isSame (66.0 , Stats::percentile ($ data , 50 ));
148+ isSame (69.6 , Stats::percentile ($ data , 60 ));
149+ isSame (76.0 , Stats::percentile ($ data , 70 ));
150+ isSame (88.0 , Stats::percentile ($ data , 80 ));
151+ isSame (113.6 , Stats::percentile ($ data , 90 ));
152+ isSame (129.8 , Stats::percentile ($ data ));
153+ isSame (142.76 , Stats::percentile ($ data , 99 ));
154+ isSame (145.9676 , Stats::percentile ($ data , 99.99 ));
155+ isSame (146.0 , Stats::percentile ($ data , 100 ));
156+
157+ isSame (0.0 , Stats::percentile ([], 0 ));
158+ isSame (0.0 , Stats::percentile ([], 90 ));
159+ isSame (0.0 , Stats::percentile ([0 ], 0 ));
160+ isSame (0.0 , Stats::percentile ([0 ], 90 ));
161+ isSame (1.0 , Stats::percentile ([1 ], 90 ));
162+
163+ isSame (0.0 , Stats::percentile (['qwerty ' ], 50 ));
164+ isSame (5.5 , Stats::percentile (['1 ' , '2 ' , '3 ' , '4 ' , '5 ' , '6 ' , '7 ' , '8 ' , '9 ' , '10 ' ], 50 ));
165+ isSame (5.5 , Stats::percentile (['1.0 ' , '2.0 ' , '3.0 ' , '4.0 ' , '5.0 ' , '6.0 ' , '7.0 ' , '8.0 ' , '9.0 ' , '10.0 ' ], 50 ));
166+ isSame (
167+ 5.5 ,
168+ Stats::percentile ([
169+ 11 => '1.0 ' ,
170+ 12 => '2.0 ' ,
171+ 13 => '3.0 ' ,
172+ 14 => '4.0 ' ,
173+ 15 => '5.0 ' ,
174+ 16 => '6.0 ' ,
175+ 17 => '7.0 ' ,
176+ 18 => '8.0 ' ,
177+ 19 => '9.0 ' ,
178+ 20 => '10.0 ' ,
179+ ], 50 ),
180+ );
181+ }
182+
183+ public function testPercentileWithInvalidPercent1 (): void
184+ {
185+ $ this ->expectException (\JBZoo \Utils \Exception::class);
186+ $ this ->expectExceptionMessage ('Percentile should be between 0 and 100, 146 given ' );
187+ Stats::percentile ([1 , 2 , 3 ], 146 );
188+ }
189+
190+ public function testPercentileWithInvalidPercent2 (): void
191+ {
192+ $ this ->expectException (\JBZoo \Utils \Exception::class);
193+ $ this ->expectExceptionMessage ('Percentile should be between 0 and 100, -146 given ' );
194+ Stats::percentile ([1 , 2 , 3 ], -146 );
195+ }
196+
197+ public function testMedian (): void
198+ {
199+ isSame (0.0 , Stats::median ([]));
200+ isSame (1.0 , Stats::median ([1 ]));
201+ isSame (1.5 , Stats::median ([1 , 2 ]));
202+ isSame (5.5 , Stats::median ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]));
203+ isSame (5.5 , Stats::median ([1 , 1 , 1 , 1 , 5 , 6 , 7 , 8 , 9 , 10 ]));
89204 }
90205}
0 commit comments