-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathMonadicAnomalyzer.m
377 lines (261 loc) · 14.8 KB
/
MonadicAnomalyzer.m
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
(*
Monadic Anomalyzer Mathematica package
Copyright (C) 2019 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
antononcube @ gmail . com,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2019 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* :Title: MonadicAnomalyzer *)
(* :Context: MonadicAnomalyzer` *)
(* :Author: Anton Antonov *)
(* :Date: 2019-10-25 *)
(* :Package Version: 0.1 *)
(* :Mathematica Version: 12 *)
(* :Copyright: (c) 2019 Anton Antonov *)
(* :Keywords: Quantile Regression, Anomaly, Outlier, Outlier detection, Unit tests *)
(* :Discussion: *)
(**************************************************************)
(* Importing packages (if needed) *)
(**************************************************************)
If[Length[DownValues[MonadicQuantileRegression`QRMonUnit]] == 0,
Echo["MonadicQuantileRegression.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"]
];
(**************************************************************)
(* Package definition *)
(**************************************************************)
BeginPackage["MonadicAnomalyzer`"];
QRMonAnomalyze::usage = "QRMonAnomalyze[opts] adds or places anomalies into the data of the monad.";
QRMonPlaceOutliers::usage = "QRMonPlaceOutliers[n, opts] adds or places n outliers into the data of the monad. \
QRMonPlaceOutliers[ {nb, nt}, opts] adds or places nb bottom outliers and nt top outliers into the data of the monad.";
QRMonComponentsPartition::usage = "QRMonComponentsPartition[n, opts] partitions the data of the monad into n components.";
Begin["`Private`"];
Needs["MonadicQuantileRegression`"];
(**************************************************************)
(* Anomalyze *)
(**************************************************************)
Clear[QRMonAnomalyze];
SyntaxInformation[QRMonAnomalyze] = { "ArgumentsPattern" -> { _., _., OptionsPattern[] } };
Options[QRMonAnomalyze] = { "NumberOfOutliers" -> Automatic, "NumberOfComponents" -> Automatic };
QRMonAnomalyze[$QRMonFailure] := $QRMonFailure;
QRMonAnomalyze[xs_, context_Association] := QRMonAnomalyze[][xs, context];
QRMonAnomalyze[][xs_, context_Association] := QRMonAnomalyze[ Automatic, Automatic ][xs, context];
QRMonAnomalyze[ opts : OptionsPattern[] ][xs_, context_Association] := QRMonAnomalyze[ Automatic, Automatic, opts ][xs, context];
QRMonAnomalyze[ opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{numberOfOutliers, numberOfComponents},
numberOfOutliers = OptionValue[ QRMonAnomalyze, "NumberOfOutliers" ];
numberOfComponents = OptionValue[ QRMonAnomalyze, "NumberOfComponents" ];
If[ !( IntegerQ[numberOfOutliers] && numberOfOutliers > 0 || TrueQ[ numberOfComponents === Automatic ] ),
Echo["The value of the option \"NumberOfOutliers\" is expected to be a positive integer.", "QRMonAnomalyze:"];
Return[$QRMonFailure];
];
If[ !( IntegerQ[numberOfComponents] && numberOfComponents > 0 || TrueQ[ numberOfComponents === Automatic ] ),
Echo["The value of the option \"NumberOfComponents\" is expected to be a positive integer.", "QRMonAnomalyze:"];
Return[$QRMonFailure];
];
];
QRMonAnomalyze[___][xs_, context_Association] :=
Block[{},
Echo["No arguments are expected;" <>
"the expected option",
"QRMonAnomalyze:"
];
$QRMonFailure
];
(**************************************************************)
(* PlaceOutliers *)
(**************************************************************)
Clear[QRMonPlaceOutliers];
SyntaxInformation[QRMonPlaceOutliers] = { "ArgumentsPattern" -> { _., _., OptionsPattern[] } };
Options[QRMonPlaceOutliers] =
{
"NumberOfBottomOutliers" -> Automatic, "NumberOfTopOutliers" -> Automatic,
"Factor" -> Automatic, "Offset" -> Automatic
};
QRMonPlaceOutliers[$QRMonFailure] := $QRMonFailure;
QRMonPlaceOutliers[xs_, context_Association] := QRMonPlaceOutliers[][xs, context];
QRMonPlaceOutliers[][xs_, context_Association] := QRMonPlaceOutliers[ {5, 5}, Options[QRMonPlaceOutliers] ][xs, context];
QRMonPlaceOutliers[ opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{ nPointsBottom, nPointsTop},
nPointsBottom = OptionValue[ QRMonPlaceOutliers, "NumberOfBottomOutliers" ];
nPointsTop = OptionValue[ QRMonPlaceOutliers, "NumberOfBottomTop" ];
If[ !( IntegerQ[nPointsBottom] && nPointsBottom >= 0 ),
Echo["The value of the option \"NumberOfBottomOutliers\" is expected to be a non-negative integer.", "QRMonPlaceOutliers:"];
Return[$QRMonFailure];
];
If[ !( IntegerQ[nPointsTop] && nPointsTop >= 0 ),
Echo["The value of the option \"NumberOfBottomTop\" is expected to be a non-negative integer.", "QRMonPlaceOutliers:"];
Return[$QRMonFailure];
];
QRMonPlaceOutliers[ {nPointsBottom, nPointsTop}, opts ][xs, context]
];
QRMonPlaceOutliers[ n_?IntegerQ, opts : OptionsPattern[] ][xs_, context_Association] :=
QRMonPlaceOutliers[ {n - Floor[n/2], Floor[n/2]}, opts][xs, context];
QRMonPlaceOutliers[ {nBottom_?IntegerQ, nTop_?IntegerQ}, opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{factor, offset, res, pointsBottom, pointsTop, pointsBottom2, pointsTop2, data, qFunc},
factor = OptionValue[ QRMonPlaceOutliers, "Factor" ];
offset = OptionValue[ QRMonPlaceOutliers, "Offset" ];
If[ !( NumericQ[factor] && factor > 0 || TrueQ[factor == Automatic] ),
Echo["The value of the option \"Factor\" is expected to be a positive real or Automatic.", "QRMonPlaceOutliers:"];
Return[$QRMonFailure];
];
If[ !( NumericQ[offset] && offset >= 0 || TrueQ[offset == Automatic]),
Echo["The value of the option \"Offset\" is expected to be a non-negative real or Automatic.", "QRMonPlaceOutliers:"];
Return[$QRMonFailure];
];
data = Fold[ QRMonBind, QRMonUnit[xs, context], { QRMonGetData, QRMonTakeValue }];
If[ TrueQ[data === $QRMonFailure], Return[$QRMonFailure] ];
res = Fold[ QRMonBind, QRMonUnit[xs, context], { QRMonOutliers, QRMonTakeValue }];
pointsBottom = RandomSample[ res["bottomOutliers"], UpTo[nBottom] ];
pointsTop = RandomSample[ res["topOutliers"], UpTo[nTop] ];
If[ TrueQ[factor === Automatic], factor = 1. ];
Which[
KeyExistsQ[ context["regressionFunctions"], 0.5 ],
If[ TrueQ[offset === Automatic], offset = 0. ];
qFunc = context["regressionFunctions"][0.5],
KeyExistsQ[ context["regressionFunctions"], "mean" ],
If[ TrueQ[offset === Automatic], offset = 0. ];
qFunc = context["regressionFunctions"]["mean"],
True,
If[ TrueQ[offset === Automatic], offset = Abs[ Subtract @@ MinMax[ data[[All,2]] ] ] ];
qFunc = 0&
];
pointsBottom2 = Map[ { #[[1]], qFunc[ #[[1]] ] - offset - factor * Abs[ #[[2]] - qFunc[ #[[1]] ] ] }&, pointsBottom];
pointsTop2 = Map[ { #[[1]], qFunc[ #[[1]] ] + offset + factor * Abs[ #[[2]] - qFunc[ #[[1]] ] ] }&, pointsTop];
data = data /. Join[ Thread[ pointsBottom -> pointsBottom2 ], Thread[ pointsTop -> pointsTop2 ] ];
QRMonUnit[ <| "bottomOutliers" -> pointsBottom2, "topOutliers" -> pointsTop2 |>, Join[ context, <| "data" -> data |> ] ]
] /; nBottom >= 0 && nTop >= 0;
QRMonPlaceOutliers[___][xs_, context_Association] :=
Block[{},
Echo[
"QRMonPlaceOutliers[n, opts] adds or places n outliers into the data of the monad. "<>
"QRMonPlaceOutliers[ {nb, nt}, opts] adds or places nb bottom outliers and nt top outliers into the data of the monad.",
"QRMonPlaceOutliers:"
];
$QRMonFailure
];
(**************************************************************)
(* Components partition *)
(**************************************************************)
Clear[QRMonComponentsPartition];
SyntaxInformation[QRMonComponentsPartition] = { "ArgumentsPattern" -> { _., OptionsPattern[] } };
Options[QRMonComponentsPartition] =
{
"NumberOfComponents" -> Automatic,
"FactorRange" -> Automatic, "OffsetRange" -> Automatic, "AlternateSigns" -> True
};
QRMonComponentsPartition[$QRMonFailure] := $QRMonFailure;
QRMonComponentsPartition[xs_, context_Association] := QRMonComponentsPartition[][xs, context];
QRMonComponentsPartition[][xs_, context_Association] := QRMonComponentsPartition[ 5, Options[QRMonPlaceOutliers] ][xs, context];
QRMonComponentsPartition[ opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{ nComponents },
nComponents = OptionValue[ QRMonComponentsPartition, "NumberOfComponents" ];
If[ !( IntegerQ[nComponents] && nComponents > 0 || TrueQ[ nComponents === Automatic ] ),
Echo["The value of the option \"nComponents\" is expected to be a positive integer or Automatic.", "QRMonComponentsPartition:"];
Return[$QRMonFailure];
];
If[ TrueQ[nComponents === Automatic], nComponents = 5 ];
QRMonComponentsPartition[ nComponents, opts ][xs, context]
];
QRMonComponentsPartition[ nComponents_?IntegerQ, opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{factorRange, offsetRange, alternateSigns, data, factors, offsets},
factorRange = OptionValue[ QRMonComponentsPartition, "FactorRange" ];
offsetRange = OptionValue[ QRMonComponentsPartition, "OffsetRange" ];
alternateSigns = TrueQ[ OptionValue[ QRMonComponentsPartition, "AlternateSigns" ] ];
If[ !( VectorQ[factorRange, NumericQ] || TrueQ[factorRange == Automatic] || TrueQ[factorRange == None] ),
Echo["The value of the option \"FactorRange\" is expected to be a pair of numbers, Automatic.", "QRMonComponentsPartition:"];
Return[$QRMonFailure];
];
If[ !( VectorQ[factorRange, NumericQ] || TrueQ[offsetRange == Automatic]),
Echo["The value of the option \"OffsetRange\" is expected to be a pair of numbers, Automatic.", "QRMonComponentsPartition:"];
Return[$QRMonFailure];
];
data = Fold[ QRMonBind, QRMonUnit[xs, context], { QRMonGetData, QRMonTakeValue }];
If[ TrueQ[data === $QRMonFailure], Return[$QRMonFailure] ];
If[ TrueQ[factorRange === Automatic], factorRange = {1, 1,5} ];
If[ TrueQ[offsetRange === Automatic], offsetRange = {1, 3} * Mean[data[[All,2]]] ];
If[ alternateSigns,
factors = Table[ (-1)^i, {i,nComponents}] * RandomReal[factorRange, nComponents];
offsets = Table[ (-1)^i, {i,nComponents}] * RandomReal[offsetRange, nComponents],
(* ELSE *)
factors = RandomReal[factorRange, nComponents];
offsets = RandomReal[offsetRange, nComponents]
];
QRMonComponentsPartition[ Transpose[ {factors, offsets}], opts ][xs, context]
] /; nComponents > 0;
QRMonComponentsPartition[ factorOffsetPairs : { {_?NumericQ, _?NumericQ} .. }, opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{comps, data, nComponents = Length[factorOffsetPairs]},
data = Fold[ QRMonBind, QRMonUnit[xs, context], { QRMonGetData, QRMonTakeValue }];
If[ TrueQ[data === $QRMonFailure], Return[$QRMonFailure] ];
comps = Partition[ SortBy[data, #[[1]]& ], Floor[ Length[data] / nComponents ] ];
comps =
MapThread[
Transpose @ { #1[[All, 1]], Rescale[ #1[[All, 2]], MinMax[#1[[All, 2]]], #2 * MinMax[#1[[All, 2]]] ] + #3 }&,
{ comps, factorOffsetPairs[[All,1]], factorOffsetPairs[[All,2]] }
];
data = Join @@ comps;
QRMonUnit[ comps, Join[ context, <| "data" -> data |> ] ]
];
QRMonComponentsPartition[___][xs_, context_Association] :=
Block[{},
Echo[
"QRMonComponentsPartition[k, opts] partitions the data of the monad into k components." <>
"QRMonComponentsPartition[ { {f1, off1} ... {fK, offK}}, opts] partitions the data of the monad into K components \
using the specified factors and offsets.",
"QRMonComponentsPartition:"
];
$QRMonFailure
];
(**************************************************************)
(* Change conditional variance *)
(**************************************************************)
Clear[QRMonChangeConditionalVariance];
SyntaxInformation[QRMonChangeConditionalVariance] = { "ArgumentsPattern" -> { _., _., OptionsPattern[] } };
Options[QRMonChangeConditionalVariance] =
{
"Points" -> Automatic, "Radius" -> Automatic,
"Factor" -> Automatic, "AggregateFunction" -> Median
};
QRMonChangeConditionalVariance[$QRMonFailure] := $QRMonFailure;
QRMonChangeConditionalVariance[xs_, context_Association] := QRMonChangeConditionalVariance[][xs, context];
QRMonChangeConditionalVariance[][xs_, context_Association] := QRMonChangeConditionalVariance[ Options[QRMonPlaceOutliers] ][xs, context];
QRMonChangeConditionalVariance[ opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{ nComponents },
nComponents = OptionValue[ QRMonComponentsPartition, "NumberOfComponents" ];
If[ !( IntegerQ[nComponents] && nComponents > 0 ),
Echo["The value of the option \"nComponents\" is expected to be a positive integer.", "QRMonComponentsPartition:"];
Return[$QRMonFailure];
];
QRMonChangeConditionalVariance[ nComponents, opts ][xs, context]
];
QRMonChangeConditionalVariance[ points_?VectorQ, radius_?NumberQ, opts : OptionsPattern[] ][xs_, context_Association] :=
Block[{},
QRMonUnit[ None, Join[ context, <| "data" -> data |> ] ]
] /; nComponents > 0;
QRMonChangeConditionalVariance[___][xs_, context_Association] :=
Block[{},
Echo[
"QRMonChangeConditionalVariance[points, radius, opts] changes the variance of the data the specified points and using the specified radius.",
"QRMonChangeConditionalVariance:"
];
$QRMonFailure
];
End[]; (* `Private` *)
EndPackage[]