-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathDistribution.php
More file actions
217 lines (193 loc) · 5.36 KB
/
Copy pathDistribution.php
File metadata and controls
217 lines (193 loc) · 5.36 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PhpBench\Math;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
use ArrayIterator;
use ReturnTypeWillChange;
use BadMethodCallException;
use ArrayAccess;
use IteratorAggregate;
/**
* Represents a population of samples.
*
* Lazily Provides summary statistics, also traversable.
*
* @implements IteratorAggregate<string, float|int>
* @implements ArrayAccess<string, float|int>
*
* @phpstan-type Stats array{min: float|int, max: float|int, sum: float|int, stdev: float|int, mean: float|int, mode: float|int, variance: float|int, rstdev: float|int }
* @phpstan-type Closures array{min: callable():(float|int), max: callable():(float|int), sum: callable():(float|int), stdev: callable():(float|int), mean: callable():(float|int), mode: callable():(float|int), variance: callable():(float|int), rstdev: callable():(float|int) }
*/
class Distribution implements IteratorAggregate, ArrayAccess
{
/** @var Closures */
private array $closures;
/**
* @param array<float|int> $samples
* @param array{min?: float|int, max?: float|int, sum?: float|int, stdev?: float|int, mean?: float|int, mode?: float|int, variance?: float|int, rstdev?: float|int } $stats
*/
public function __construct(private readonly array $samples, private array $stats = [])
{
if (count($samples) < 1) {
throw new LogicException(
'Cannot create a distribution with zero samples.'
);
}
$this->closures = [
'min' => fn () => min($this->samples),
'max' => fn () => max($this->samples),
'sum' => fn () => array_sum($this->samples),
'stdev' => fn () => Statistics::stdev($this->samples),
'mean' => fn () => Statistics::mean($this->samples),
'mode' => fn () => Statistics::kdeMode($this->samples),
'variance' => fn () => Statistics::variance($this->samples),
'rstdev' => function () {
$mean = $this->getMean();
return $mean ? $this->getStdev() / $mean * 100 : 0;
},
];
if ($diff = array_diff(array_keys($this->stats), array_keys($this->closures))) {
throw new RuntimeException(sprintf(
'Unknown pre-computed stat(s) encountered: "%s"',
implode('", "', $diff)
));
}
}
/**
* @return float|int
*/
public function getMin()
{
return $this->getStat('min');
}
/**
* @return float|int
*/
public function getMax()
{
return $this->getStat('max');
}
/**
* @return float|int
*/
public function getSum()
{
return $this->getStat('sum');
}
/**
* @return float|int
*/
public function getStdev()
{
return $this->getStat('stdev');
}
/**
* @return float|int
*/
public function getMean()
{
return $this->getStat('mean');
}
/**
* @return float|int
*/
public function getMode()
{
return $this->getStat('mode');
}
/**
* @return float|int
*/
public function getRstdev()
{
return $this->getStat('rstdev');
}
/**
* @return float|int
*/
public function getVariance()
{
return $this->getStat('variance');
}
/**
* @return ArrayIterator<string, float|int>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->getStats());
}
/**
* @return Stats
*/
public function getStats(): array
{
return [
'min' => $this->getMin(),
'max' => $this->getMax(),
'sum' => $this->getSum(),
'stdev' => $this->getStdev(),
'mean' => $this->getMean(),
'mode' => $this->getMode(),
'variance' => $this->getVariance(),
'rstdev' => $this->getRstdev(),
];
}
/**
* @param key-of<Closures> $name
*/
private function getStat(string $name): float|int
{
if (!isset($this->closures[$name])) {
throw new InvalidArgumentException(sprintf(
'Unknown stat "%s", known stats: "%s"',
$name,
implode('", "', array_keys($this->closures))
));
}
return $this->stats[$name] ??= $this->closures[$name]();
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetExists($offset): bool
{
return isset($this->closures[$offset]);
}
/**
* {@inheritdoc}
*
* @param key-of<Closures> $offset
*/
#[ReturnTypeWillChange]
public function offsetGet(mixed $offset)
{
return $this->getStat($offset);
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value): void
{
throw new BadMethodCallException('Distribution is read-only');
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset): void
{
throw new BadMethodCallException('Distribution is read-only');
}
}