@@ -79,6 +79,8 @@ The various mathematical statistics are listed below:
7979| ` thirdQuartile() ` | 3rd quartile, is the value at which 75 percent of the data is below it |
8080| ` firstQuartile() ` | first quartile, is the value at which 25 percent of the data is below it |
8181| ` percentile() ` | value at any percentile (0–100) with linear interpolation |
82+ | ` rank() ` | rank each data point, with configurable tie handling |
83+ | ` percentileRank() ` | percentile position of a value within a dataset |
8284| ` pstdev() ` | Population standard deviation |
8385| ` stdev() ` | Sample standard deviation |
8486| ` sem() ` | Standard error of the mean (SEM) — measures precision of the sample mean |
@@ -335,6 +337,45 @@ $value = Stat::percentile([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 90);
335337// 91.0
336338```
337339
340+ #### Stat::rank( array $data, string $method = Stat::RANK_AVERAGE )
341+ Return 1-based ranks for each data point, preserving the original array keys.
342+
343+ The ` $method ` parameter controls how tied values are ranked:
344+ - ` Stat::RANK_AVERAGE ` (default): tied values receive the average rank.
345+ - ` Stat::RANK_MIN ` : tied values receive the lowest rank in the tied group.
346+ - ` Stat::RANK_MAX ` : tied values receive the highest rank in the tied group.
347+ - ` Stat::RANK_DENSE ` : tied values receive the same rank and ranks do not skip numbers.
348+ - ` Stat::RANK_ORDINAL ` : tied values are ranked by their sorted order, preserving input order inside ties.
349+
350+ ``` php
351+ use HiFolks\Statistics\Stat;
352+
353+ $ranks = Stat::rank([10, 20, 20, 30]);
354+ // [1, 2.5, 2.5, 4]
355+
356+ $ranks = Stat::rank([10, 20, 20, 30], Stat::RANK_DENSE);
357+ // [1, 2, 2, 3]
358+ ```
359+
360+ #### Stat::percentileRank( array $data, int|float $value, string $kind = Stat::PERCENTILE_RANK_WEAK, ?int $round = null )
361+ Return the percentile position of a value within a dataset.
362+
363+ The ` $kind ` parameter controls the calculation:
364+ - ` Stat::PERCENTILE_RANK_WEAK ` (default): percentage of values less than or equal to the value.
365+ - ` Stat::PERCENTILE_RANK_STRICT ` : percentage of values strictly less than the value.
366+ - ` Stat::PERCENTILE_RANK_MEAN ` : average of weak and strict percentile ranks.
367+ - ` Stat::PERCENTILE_RANK_RANK ` : average percentage rank for exact matches, falling back to mean when the value is absent.
368+
369+ ``` php
370+ use HiFolks\Statistics\Stat;
371+
372+ $rank = Stat::percentileRank([10, 20, 20, 30, 40], 20);
373+ // 60.0
374+
375+ $rank = Stat::percentileRank([10, 20, 20, 30, 40], 20, Stat::PERCENTILE_RANK_STRICT);
376+ // 20.0
377+ ```
378+
338379#### Stat::pstdev( array $data )
339380Return the ** Population** Standard Deviation, a measure of the amount of variation or dispersion of a set of values.
340381A low standard deviation indicates that the values tend to be close to the mean of the set, while a high standard deviation indicates that the values are spread out over a wider range.
0 commit comments