-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboPercentile.php
111 lines (91 loc) · 3.42 KB
/
ComboPercentile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/
declare(strict_types=1);
namespace JBZoo\CsvBlueprint\Rules\Aggregate;
use JBZoo\CsvBlueprint\Rules\AbstractRule;
use JBZoo\CsvBlueprint\Utils;
use MathPHP\Statistics\Descriptive;
use function JBZoo\Utils\float;
final class ComboPercentile extends AbstractAggregateRuleCombo
{
public const INPUT_TYPE = AbstractRule::INPUT_TYPE_FLOATS;
protected const NAME = 'percentile';
private const ARGS = 2;
private const PERC = 0;
private const VAL = 1;
public function getHelpMeta(): array
{
return [
[
'Compute the P-th percentile of a list of numbers.',
'Linear interpolation between closest ranks method - Second variant, '
. 'C = 1 P-th percentile (0 <= P <= 100) of a list of N ordered values '
. '(sorted from least to greatest).',
'Similar method used in NumPy and Excel.',
'See: https://en.wikipedia.org/wiki/Percentile#'
. 'Second_variant.2C_.7F.27.22.60UNIQ--postMath-00000043-QINU.60.22.27.7F',
'Example: `[ 95.5, 1.234 ]` The 95.5th percentile in the column must be "1.234" (float).',
],
[
self::MIN => ['[ 95.0, 1.0 ]', 'x >= 1.0'],
self::GREATER => ['[ 95.0, 2.0 ]', 'x > 2.0'],
self::NOT => ['[ 95.0, 5.0 ]', 'x != 5.0'],
self::EQ => ['[ 95.0, 7.0 ]', 'x == 7.0'],
self::LESS => ['[ 95.0, 8.0 ]', 'x < 8.0'],
self::MAX => ['[ 95.0, 9.0 ]', 'x <= 9.0'],
],
];
}
public static function analyzeColumnValues(array $columnValues): array|bool|float|int|string
{
$result = self::calcValue($columnValues, ['p' => 95.0]);
if ($result === null) {
return false;
}
return [95.0, $result];
}
protected function getExpected(): float
{
return float($this->getParams()[self::VAL]);
}
protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}
$percentile = (float)$this->getParams()[self::PERC];
return self::calcValue($colValues, ['p' => $percentile]);
}
protected static function calcValue(array $columnValues, ?array $options = null): null|float|int
{
$columnValues = Utils::analyzeGuard($columnValues, self::INPUT_TYPE);
if ($columnValues === null) {
return null;
}
if (!isset($options['p'])) {
throw new Exception('The rule expects the "p" option');
}
return Descriptive::percentile($columnValues, $options['p']);
}
private function getParams(): array
{
$params = $this->getOptionAsArray();
if (\count($params) !== self::ARGS) {
throw new Exception(
'The rule expects exactly two params: '
. 'the first is percentile (P is beet 0.0 and 100.0), the second is the expected value (float)',
);
}
return $params;
}
}