-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
RobustZScore.php
306 lines (269 loc) · 7.44 KB
/
RobustZScore.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace Rubix\ML\AnomalyDetectors;
use Rubix\ML\Learner;
use Rubix\ML\DataType;
use Rubix\ML\Estimator;
use Rubix\ML\Persistable;
use Rubix\ML\Helpers\CPU;
use Rubix\ML\EstimatorType;
use Rubix\ML\Helpers\Stats;
use Rubix\ML\Helpers\Params;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Traits\AutotrackRevisions;
use Rubix\ML\Specifications\DatasetIsNotEmpty;
use Rubix\ML\Specifications\SpecificationChain;
use Rubix\ML\Specifications\DatasetHasDimensionality;
use Rubix\ML\Specifications\SamplesAreCompatibleWithEstimator;
use Rubix\ML\Exceptions\InvalidArgumentException;
use Rubix\ML\Exceptions\RuntimeException;
use function count;
use function abs;
use function max;
use function array_map;
/**
* Robust Z-Score
*
* A statistical anomaly detector that uses modified Z-Scores which are robust to preexisting
* outliers in the training set. The modified Z-Score uses the median and median absolute
* deviation (MAD) unlike the mean and standard deviation of a standard Z-Score - which are
* more sensitive to outliers. Anomalies are flagged if their final weighted Z-Score exceeds a
* user-defined threshold.
*
* > **Note:** A beta value of 1 means the estimator only considers the maximum absolute Z-Score,
* whereas a setting of 0 indicates that only the average Z-Score factors into the final score.
*
* References:
* [1] B. Iglewicz et al. (1993). How to Detect and Handle Outliers.
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class RobustZScore implements Estimator, Learner, Scoring, Persistable
{
use AutotrackRevisions;
/**
* The expected value of the MAD as n asymptotes.
*
* @var float
*/
protected const ETA = 0.6745;
/**
* The minimum z score to be flagged as an anomaly.
*
* @var float
*/
protected float $threshold;
/**
* The weight of the maximum per sample z score in the overall anomaly score.
*
* @var float
*/
protected float $beta;
/**
* The amount of epsilon smoothing added to the median absolute deviation (MAD) of each feature.
*
* @var float
*/
protected float $smoothing;
/**
* The median of each feature column in the training set.
*
* @var float[]
*/
protected array $medians = [
//
];
/**
* The median absolute deviation of each feature column.
*
* @var float[]
*/
protected array $mads = [
//
];
/**
* @param float $threshold
* @param float $beta
* @param float $smoothing
* @throws InvalidArgumentException
*/
public function __construct(float $threshold = 3.5, float $beta = 0.5, float $smoothing = 1e-9)
{
if ($threshold <= 0.0) {
throw new InvalidArgumentException('Threshold must be'
. " greater than 0, $threshold given.");
}
if ($beta < 0.0 or $beta > 1.0) {
throw new InvalidArgumentException('Beta must be'
. " between 0 and 1, $beta given.");
}
if ($smoothing <= 0.0) {
throw new InvalidArgumentException('Smoothing must be'
. " greater than 0, $smoothing given.");
}
$this->threshold = $threshold;
$this->beta = $beta;
$this->smoothing = $smoothing;
}
/**
* Return the estimator type.
*
* @internal
*
* @return EstimatorType
*/
public function type() : EstimatorType
{
return EstimatorType::anomalyDetector();
}
/**
* Return the data types that the estimator is compatible with.
*
* @internal
*
* @return list<\Rubix\ML\DataType>
*/
public function compatibility() : array
{
return [
DataType::continuous(),
];
}
/**
* Return the settings of the hyper-parameters in an associative array.
*
* @internal
*
* @return mixed[]
*/
public function params() : array
{
return [
'threshold' => $this->threshold,
'beta' => $this->beta,
'smoothing' => $this->smoothing,
];
}
/**
* Has the learner been trained?
*
* @return bool
*/
public function trained() : bool
{
return $this->medians and $this->mads;
}
/**
* Return the array of computed feature column medians.
*
* @return float[]|null
*/
public function medians() : ?array
{
return $this->medians;
}
/**
* Return the array of computed feature column median absolute deviations.
*
* @return float[]|null
*/
public function mads() : ?array
{
return $this->mads;
}
/**
* Train the learner with a dataset.
*
* @param Dataset $dataset
*/
public function train(Dataset $dataset) : void
{
SpecificationChain::with([
new DatasetIsNotEmpty($dataset),
new SamplesAreCompatibleWithEstimator($dataset, $this),
])->check();
$this->medians = $this->mads = [];
foreach ($dataset->features() as $column => $values) {
[$median, $mad] = Stats::medianMad($values);
$this->medians[$column] = $median;
$this->mads[$column] = $mad;
}
$epsilon = max($this->smoothing * max($this->mads), CPU::epsilon());
foreach ($this->mads as &$mad) {
$mad += $epsilon;
}
}
/**
* Make predictions from a dataset.
*
* @param Dataset $dataset
* @throws RuntimeException
* @return list<int>
*/
public function predict(Dataset $dataset) : array
{
if (!$this->medians or !$this->mads) {
throw new RuntimeException('Estimator has not been trained.');
}
return array_map([$this, 'predictSample'], $dataset->samples());
}
/**
* Predict a single sample and return the result.
*
* @internal
*
* @param list<int|float> $sample
* @return int
*/
public function predictSample(array $sample) : int
{
return $this->zHat($sample) > $this->threshold ? 1 : 0;
}
/**
* Return the anomaly scores assigned to the samples in a dataset.
*
* @param Dataset $dataset
* @throws RuntimeException
* @return list<float>
*/
public function score(Dataset $dataset) : array
{
if (!$this->medians or !$this->mads) {
throw new RuntimeException('Estimator has not been trained.');
}
DatasetHasDimensionality::with($dataset, count($this->medians))->check();
return array_map([$this, 'zHat'], $dataset->samples());
}
/**
* Calculate the modified z score for a given sample.
*
* @param list<int|float> $sample
* @return float
*/
protected function zHat(array $sample) : float
{
$scores = [];
foreach ($sample as $column => $value) {
$scores[] = abs(
(self::ETA
* ($value - $this->medians[$column]))
/ $this->mads[$column]
);
}
$zHat = (1.0 - $this->beta) * Stats::mean($scores)
+ $this->beta * max($scores);
return $zHat;
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Robust Z Score (' . Params::stringify($this->params()) . ')';
}
}