tsadmetrics 0.1.11__py3-none-any.whl → 0.1.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,14 +11,17 @@ def point_wise_recall(y_true: np.array, y_pred: np.array):
11
11
  Calculate point-wise recall for anomaly detection in time series.
12
12
  Esta métrica consiste en el recall clásico, sin tener en cuenta el contexto
13
13
  temporal.
14
+
14
15
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
15
16
 
16
17
  Parameters:
17
- y_true (np.array): The ground truth binary labels for the time series data.
18
- y_pred (np.array): The predicted binary labels for the time series data.
18
+ y_true (np.array):
19
+ The ground truth binary labels for the time series data.
20
+ y_pred (np.array):
21
+ The predicted binary labels for the time series data.
19
22
 
20
23
  Returns:
21
- float: The point-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
24
+ float: The point-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
22
25
  """
23
26
  m = Pointwise_metrics(len(y_true),y_true,y_pred)
24
27
  m.set_confusion()
@@ -32,14 +35,17 @@ def point_wise_precision(y_true: np.array, y_pred: np.array):
32
35
  Calculate point-wise precision for anomaly detection in time series.
33
36
  Esta métrica consiste en la precisión clásica, sin tener en cuenta el contexto
34
37
  temporal.
38
+
35
39
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
36
40
 
37
41
  Parameters:
38
- y_true (np.array): The ground truth binary labels for the time series data.
39
- y_pred (np.array): The predicted binary labels for the time series data.
42
+ y_true (np.array):
43
+ The ground truth binary labels for the time series data.
44
+ y_pred (np.array):
45
+ The predicted binary labels for the time series data.
40
46
 
41
47
  Returns:
42
- float: The point-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
48
+ float: The point-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
43
49
  """
44
50
  m = Pointwise_metrics(len(y_true),y_true,y_pred)
45
51
  m.set_confusion()
@@ -53,16 +59,20 @@ def point_wise_f_score(y_true: np.array, y_pred: np.array, beta=1):
53
59
  Calculate point-wise F-score for anomaly detection in time series.
54
60
  Esta métrica consiste en la F-score clásica, sin tener en cuenta el contexto
55
61
  temporal.
62
+
56
63
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
57
64
 
58
65
  Parameters:
59
- y_true (np.array): The ground truth binary labels for the time series data.
60
- y_pred (np.array): The predicted binary labels for the time series data.
61
- betha (float): The beta value, which determines the weight of precision in the combined score.
62
- Default is 1, which gives equal weight to precision and recall.
66
+ y_true (np.array):
67
+ The ground truth binary labels for the time series data.
68
+ y_pred (np.array):
69
+ The predicted binary labels for the time series data.
70
+ beta (float):
71
+ The beta value, which determines the weight of precision in the combined score.
72
+ Default is 1, which gives equal weight to precision and recall.
63
73
 
64
74
  Returns:
65
- float: The point-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
75
+ float: The point-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
66
76
  """
67
77
  precision = point_wise_precision(y_true, y_pred)
68
78
  recall = point_wise_recall(y_true, y_pred)
@@ -84,11 +94,13 @@ def point_adjusted_recall(y_true: np.array, y_pred: np.array):
84
94
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
85
95
 
86
96
  Parameters:
87
- y_true (np.array): The ground truth binary labels for the time series data.
88
- y_pred (np.array): The predicted binary labels for the time series data.
97
+ y_true (np.array):
98
+ The ground truth binary labels for the time series data.
99
+ y_pred (np.array):
100
+ The predicted binary labels for the time series data.
89
101
 
90
102
  Returns:
91
- float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
103
+ float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
92
104
  """
93
105
  if np.sum(y_pred) == 0:
94
106
  return 0
@@ -106,14 +118,17 @@ def point_adjusted_precision(y_true: np.array, y_pred: np.array):
106
118
  if at least one point within that segment is predicted as anomalous, all points in the segment
107
119
  are marked as correctly detected. The adjusted predictions are then compared to the ground-truth
108
120
  labels using the standard point-wise precision formulation.
121
+
109
122
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
110
123
 
111
124
  Parameters:
112
- y_true (np.array): The ground truth binary labels for the time series data.
113
- y_pred (np.array): The predicted binary labels for the time series data.
125
+ y_true (np.array):
126
+ The ground truth binary labels for the time series data.
127
+ y_pred (np.array):
128
+ The predicted binary labels for the time series data.
114
129
 
115
130
  Returns:
116
- float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
131
+ float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
117
132
  """
118
133
  if np.sum(y_pred) == 0:
119
134
  return 0
@@ -134,13 +149,16 @@ def point_adjusted_f_score(y_true: np.array, y_pred: np.array, beta=1):
134
149
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
135
150
 
136
151
  Parameters:
137
- y_true (np.array): The ground truth binary labels for the time series data.
138
- y_pred (np.array): The predicted binary labels for the time series data.
139
- beta (float): The beta value, which determines the weight of precision in the combined score.
140
- Default is 1, which gives equal weight to precision and recall.
152
+ y_true (np.array):
153
+ The ground truth binary labels for the time series data.
154
+ y_pred (np.array):
155
+ The predicted binary labels for the time series data.
156
+ beta (float):
157
+ The beta value, which determines the weight of precision in the combined score.
158
+ Default is 1, which gives equal weight to precision and recall.
141
159
 
142
160
  Returns:
143
- float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
161
+ float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
144
162
  """
145
163
  precision = point_adjusted_precision(y_true, y_pred)
146
164
  recall = point_adjusted_recall(y_true, y_pred)
@@ -160,15 +178,20 @@ def delay_th_point_adjusted_recall(y_true: np.array, y_pred: np.array, k: int):
160
178
  if at least one point within the first k time steps of the segment is predicted as anomalous,
161
179
  all points in the segment are marked as correctly detected. The adjusted predictions are then
162
180
  used to compute the standard point-wise recall formulation.
181
+
163
182
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
164
183
 
165
184
  Parameters:
166
- y_true (np.array): The ground truth binary labels for the time series data.
167
- y_pred (np.array): The predicted binary labels for the time series data.
168
- k (int): Maximum number of time steps from the start of an anomaly segment within which a prediction must occur for the segment to be considered detected.
185
+ y_true (np.array):
186
+ The ground truth binary labels for the time series data.
187
+ y_pred (np.array):
188
+ The predicted binary labels for the time series data.
189
+ k (int):
190
+ Maximum number of time steps from the start of an anomaly segment within which a
191
+ prediction must occur for the segment to be considered detected.
169
192
 
170
193
  Returns:
171
- float: The delay thresholded point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
194
+ float: The delay thresholded point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
172
195
  """
173
196
  if np.sum(y_pred) == 0:
174
197
  return 0
@@ -186,15 +209,20 @@ def delay_th_point_adjusted_precision(y_true: np.array, y_pred: np.array, k: int
186
209
  if at least one point within the first k time steps of the segment is predicted as anomalous,
187
210
  all points in the segment are marked as correctly detected. The adjusted predictions are then
188
211
  used to compute the standard point-wise precision fromulation.
212
+
189
213
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
190
214
 
191
215
  Parameters:
192
- y_true (np.array): The ground truth binary labels for the time series data.
193
- y_pred (np.array): The predicted binary labels for the time series data.
194
- k (int): Maximum number of time steps from the start of an anomaly segment within which a prediction must occur for the segment to be considered detected.
216
+ y_true (np.array):
217
+ The ground truth binary labels for the time series data.
218
+ y_pred (np.array):
219
+ The predicted binary labels for the time series data.
220
+ k (int):
221
+ Maximum number of time steps from the start of an anomaly segment
222
+ within which a prediction must occur for the segment to be considered detected.
195
223
 
196
224
  Returns:
197
- float: The delay thresholded point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
225
+ float: The delay thresholded point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
198
226
  """
199
227
  if np.sum(y_pred) == 0:
200
228
  return 0
@@ -216,14 +244,18 @@ def delay_th_point_adjusted_f_score(y_true: np.array, y_pred: np.array, k: int,
216
244
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
217
245
 
218
246
  Parameters:
219
- y_true (np.array): The ground truth binary labels for the time series data.
220
- y_pred (np.array): The predicted binary labels for the time series data.
221
- k (int): Maximum number of time steps from the start of an anomaly segment within which a prediction must occur for the segment to be considered detected.
222
- beta (float): The beta value, which determines the weight of precision in the combined score.
223
- Default is 1, which gives equal weight to precision and recall.
247
+ y_true (np.array):
248
+ The ground truth binary labels for the time series data.
249
+ y_pred (np.array):
250
+ The predicted binary labels for the time series data.
251
+ k (int):
252
+ Maximum number of time steps from the start of an anomaly segment within which a prediction must occur for the segment to be considered detected.
253
+ beta (float):
254
+ The beta value, which determines the weight of precision in the combined score.
255
+ Default is 1, which gives equal weight to precision and recall.
224
256
 
225
257
  Returns:
226
- float: The delay thresholded point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
258
+ float: The delay thresholded point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
227
259
  """
228
260
  precision = delay_th_point_adjusted_precision(y_true, y_pred, k)
229
261
  recall = delay_th_point_adjusted_recall(y_true, y_pred, k)
@@ -243,17 +275,20 @@ def point_adjusted_at_k_recall(y_true: np.array, y_pred: np.array, k: float):
243
275
  the segment are marked as correctly detected. Otherwise, the entire segment is treated as
244
276
  missed, even if some points were correctly predicted. The adjusted predictions are then used
245
277
  to compute the standard point-wise recall.
278
+
246
279
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
247
280
 
248
281
  Parameters:
249
- y_true (np.array): The ground truth binary labels for the time series data.
250
- y_pred (np.array): The predicted binary labels for the time series data.
251
- k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
282
+ y_true (np.array):
283
+ The ground truth binary labels for the time series data.
284
+ y_pred (np.array):
285
+ The predicted binary labels for the time series data.
286
+ k (float):
287
+ The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
252
288
 
253
289
  Returns:
254
- float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
290
+ float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
255
291
  """
256
- #TP, _, FP, FN = get_tp_tn_fp_fn_point_adjusted_at_k(y_true, y_pred, k)
257
292
  m = PointAdjustKPercent(len(y_true),y_true,y_pred,k=k)
258
293
  TP,FN = m.tp,m.fn
259
294
  if TP == 0:
@@ -272,12 +307,15 @@ def point_adjusted_at_k_precision(y_true: np.array, y_pred: np.array, k: float):
272
307
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
273
308
 
274
309
  Parameters:
275
- y_true (np.array): The ground truth binary labels for the time series data.
276
- y_pred (np.array): The predicted binary labels for the time series data.
277
- k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
310
+ y_true (np.array):
311
+ The ground truth binary labels for the time series data.
312
+ y_pred (np.array):
313
+ The predicted binary labels for the time series data.
314
+ k (float):
315
+ The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
278
316
 
279
317
  Returns:
280
- float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
318
+ float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
281
319
  """
282
320
  m = PointAdjustKPercent(len(y_true),y_true,y_pred,k=k)
283
321
  TP,FP = m.tp,m.fp
@@ -294,17 +332,22 @@ def point_adjusted_at_k_f_score(y_true: np.array, y_pred: np.array, k: float, be
294
332
  the segment are marked as correctly detected. Otherwise, the entire segment is treated as
295
333
  missed, even if some points were correctly predicted. The adjusted predictions are then used
296
334
  to compute the standard F-Score precision.
335
+
297
336
  Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
298
337
 
299
338
  Parameters:
300
- y_true (np.array): The ground truth binary labels for the time series data.
301
- y_pred (np.array): The predicted binary labels for the time series data.
302
- k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
303
- beta (float): The beta value, which determines the weight of precision in the combined score.
304
- Default is 1, which gives equal weight to precision and recall.
339
+ y_true (np.array):
340
+ The ground truth binary labels for the time series data.
341
+ y_pred (np.array):
342
+ The predicted binary labels for the time series data.
343
+ k (float):
344
+ The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
345
+ beta (float):
346
+ The beta value, which determines the weight of precision in the combined score.
347
+ Default is 1, which gives equal weight to precision and recall.
305
348
 
306
349
  Returns:
307
- float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
350
+ float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
308
351
  """
309
352
  precision = point_adjusted_at_k_precision(y_true, y_pred, k)
310
353
  recall = point_adjusted_at_k_recall(y_true, y_pred, k)
@@ -326,15 +369,19 @@ def latency_sparsity_aw_recall(y_true: np.array, y_pred: np.array, ni: int):
326
369
  scattered false positives, predictions are subsampled using a sparsity factor n, so that
327
370
  only one prediction is considered every n time steps. The adjusted predictions are then used
328
371
  to compute the standard point-wise recall.
372
+
329
373
  Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
330
374
 
331
375
  Parameters:
332
- y_true (np.array): The ground truth binary labels for the time series data.
333
- y_pred (np.array): The predicted binary labels for the time series data.
334
- ni (int): The batch size used in the implementation to handle latency and sparsity.
376
+ y_true (np.array):
377
+ The ground truth binary labels for the time series data.
378
+ y_pred (np.array):
379
+ The predicted binary labels for the time series data.
380
+ ni (int):
381
+ The batch size used in the implementation to handle latency and sparsity.
335
382
 
336
383
  Returns:
337
- float: The latency and sparsity aware recall score, which is the ratio of true positives to the sum of true positives and false negatives.
384
+ float: The latency and sparsity aware recall score, which is the ratio of true positives to the sum of true positives and false negatives.
338
385
  """
339
386
  if np.sum(y_pred) == 0:
340
387
  return 0
@@ -358,12 +405,15 @@ def latency_sparsity_aw_precision(y_true: np.array, y_pred: np.array, ni: int):
358
405
  Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
359
406
 
360
407
  Parameters:
361
- y_true (np.array): The ground truth binary labels for the time series data.
362
- y_pred (np.array): The predicted binary labels for the time series data.
363
- ni (int): The batch size used in the implementation to handle latency and sparsity.
408
+ y_true (np.array):
409
+ The ground truth binary labels for the time series data.
410
+ y_pred (np.array):
411
+ The predicted binary labels for the time series data.
412
+ ni (int):
413
+ The batch size used in the implementation to handle latency and sparsity.
364
414
 
365
415
  Returns:
366
- float: The latency and sparsity aware precision score, which is the ratio of true positives to the sum of true positives and false positives.
416
+ float: The latency and sparsity aware precision score, which is the ratio of true positives to the sum of true positives and false positives.
367
417
  """
368
418
  if np.sum(y_pred) == 0:
369
419
  return 0
@@ -388,14 +438,18 @@ def latency_sparsity_aw_f_score(y_true: np.array, y_pred: np.array, ni: int, bet
388
438
  Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
389
439
 
390
440
  Parameters:
391
- y_true (np.array): The ground truth binary labels for the time series data.
392
- y_pred (np.array): The predicted binary labels for the time series data.
393
- ni (int): The batch size used in the implementation to handle latency and sparsity.
394
- beta (float): The beta value, which determines the weight of precision in the combined score.
395
- Default is 1, which gives equal weight to precision and recall.
441
+ y_true (np.array):
442
+ The ground truth binary labels for the time series data.
443
+ y_pred (np.array):
444
+ The predicted binary labels for the time series data.
445
+ ni (int):
446
+ The batch size used in the implementation to handle latency and sparsity.
447
+ beta (float):
448
+ The beta value, which determines the weight of precision in the combined score.
449
+ Default is 1, which gives equal weight to precision and recall.
396
450
 
397
451
  Returns:
398
- float: The latency and sparsity aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
452
+ float: The latency and sparsity aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
399
453
  """
400
454
  if np.sum(y_pred) == 0:
401
455
  return 0
@@ -421,11 +475,13 @@ def segment_wise_recall(y_true: np.array, y_pred: np.array):
421
475
  Implementation of https://arxiv.org/pdf/1802.04431
422
476
 
423
477
  Parameters:
424
- y_true (np.array): The ground truth binary labels for the time series data.
425
- y_pred (np.array): The predicted binary labels for the time series data.
478
+ y_true (np.array):
479
+ The ground truth binary labels for the time series data.
480
+ y_pred (np.array):
481
+ The predicted binary labels for the time series data.
426
482
 
427
483
  Returns:
428
- float: The segment-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
484
+ float: The segment-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
429
485
  """
430
486
  m = Segmentwise_metrics(len(y_true),y_true,y_pred)
431
487
  TP,FN = m.tp,m.fn
@@ -443,14 +499,17 @@ def segment_wise_precision(y_true: np.array, y_pred: np.array):
443
499
  is detected, and a false positive is recorded for each predicted anomalous segment that does not
444
500
  overlap with any ground-truth anomaly. The final precision is computed using these adjusted
445
501
  segment-level counts.
502
+
446
503
  Implementation of https://arxiv.org/pdf/1802.04431
447
504
 
448
505
  Parameters:
449
- y_true (np.array): The ground truth binary labels for the time series data.
450
- y_pred (np.array): The predicted binary labels for the time series data.
506
+ y_true (np.array):
507
+ The ground truth binary labels for the time series data.
508
+ y_pred (np.array):
509
+ The predicted binary labels for the time series data.
451
510
 
452
511
  Returns:
453
- float: The segment-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
512
+ float: The segment-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
454
513
  """
455
514
  m = Segmentwise_metrics(len(y_true),y_true,y_pred)
456
515
  TP,FP = m.tp,m.fp
@@ -468,16 +527,20 @@ def segment_wise_f_score(y_true: np.array, y_pred: np.array, beta=1):
468
527
  is detected, and a false positive is recorded for each predicted anomalous segment that does not
469
528
  overlap with any ground-truth anomaly. The final F-score is computed using these adjusted
470
529
  segment-level counts.
530
+
471
531
  Implementation of https://arxiv.org/pdf/1802.04431
472
532
 
473
533
  Parameters:
474
- y_true (np.array): The ground truth binary labels for the time series data.
475
- y_pred (np.array): The predicted binary labels for the time series data.
476
- beta (float): The beta value, which determines the weight of precision in the combined score.
477
- Default is 1, which gives equal weight to precision and recall.
534
+ y_true (np.array):
535
+ The ground truth binary labels for the time series data.
536
+ y_pred (np.array):
537
+ The predicted binary labels for the time series data.
538
+ beta (float):
539
+ The beta value, which determines the weight of precision in the combined score.
540
+ Default is 1, which gives equal weight to precision and recall.
478
541
 
479
542
  Returns:
480
- float: The segment-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
543
+ float: The segment-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
481
544
 
482
545
  """
483
546
  m = Segmentwise_metrics(len(y_true),y_true,y_pred)
@@ -503,13 +566,16 @@ def composite_f_score(y_true: np.array, y_pred: np.array, beta=1):
503
566
  Implementation of https://ieeexplore.ieee.org/document/9525836
504
567
 
505
568
  Parameters:
506
- y_true (np.array): The ground truth binary labels for the time series data.
507
- y_pred (np.array): The predicted binary labels for the time series data.
508
- beta (float): The beta value, which determines the weight of precision in the combined score.
509
- Default is 1, which gives equal weight to precision and recall.
569
+ y_true (np.array):
570
+ The ground truth binary labels for the time series data.
571
+ y_pred (np.array):
572
+ The predicted binary labels for the time series data.
573
+ beta (float):
574
+ The beta value, which determines the weight of precision in the combined score.
575
+ Default is 1, which gives equal weight to precision and recall.
510
576
 
511
577
  Returns:
512
- float: The composite F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
578
+ float: The composite F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
513
579
 
514
580
  """
515
581
  m = Composite_f(len(y_true),y_true,y_pred)
@@ -536,12 +602,15 @@ def time_tolerant_recall(y_true: np.array, y_pred: np.array, t: int) -> float:
536
602
  Implementation of https://arxiv.org/pdf/1802.04431
537
603
 
538
604
  Parameters:
539
- y_true (np.array): The ground truth binary labels for the time series data.
540
- y_pred (np.array): The predicted binary labels for the time series data.
541
- t (int): The time tolerance parameter
605
+ y_true (np.array):
606
+ The ground truth binary labels for the time series data.
607
+ y_pred (np.array):
608
+ The predicted binary labels for the time series data.
609
+ t (int):
610
+ The time tolerance parameter
542
611
 
543
612
  Returns:
544
- float: The time tolerant recall score, which is the ratio of true positives to the sum of true positives and false negatives.
613
+ float: The time tolerant recall score, which is the ratio of true positives to the sum of true positives and false negatives.
545
614
  """
546
615
  if np.sum(y_pred) == 0:
547
616
  return 0
@@ -561,12 +630,15 @@ def time_tolerant_precision(y_true: np.array, y_pred: np.array, t: int) -> float
561
630
  Implementation of https://arxiv.org/pdf/1802.04431
562
631
 
563
632
  Parameters:
564
- y_true (np.array): The ground truth binary labels for the time series data.
565
- y_pred (np.array): The predicted binary labels for the time series data.
566
- t (int): The time tolerance parameter
633
+ y_true (np.array):
634
+ The ground truth binary labels for the time series data.
635
+ y_pred (np.array):
636
+ The predicted binary labels for the time series data.
637
+ t (int):
638
+ The time tolerance parameter
567
639
 
568
640
  Returns:
569
- float: The time tolerant precision score, which is the ratio of true positives to the sum of true positives and false positives.
641
+ float: The time tolerant precision score, which is the ratio of true positives to the sum of true positives and false positives.
570
642
  """
571
643
  if np.sum(y_pred) == 0:
572
644
  return 0
@@ -586,14 +658,18 @@ def time_tolerant_f_score(y_true: np.array, y_pred: np.array, t: int, beta=1):
586
658
  Implementation of https://arxiv.org/pdf/1802.04431
587
659
 
588
660
  Parameters:
589
- y_true (np.array): The ground truth binary labels for the time series data.
590
- y_pred (np.array): The predicted binary labels for the time series data.
591
- t (int): The time tolerance parameter
592
- beta (float): The beta value, which determines the weight of precision in the combined score.
593
- Default is 1, which gives equal weight to precision and recall.
661
+ y_true (np.array):
662
+ The ground truth binary labels for the time series data.
663
+ y_pred (np.array):
664
+ The predicted binary labels for the time series data.
665
+ t (int):
666
+ The time tolerance parameter
667
+ beta (float):
668
+ The beta value, which determines the weight of precision in the combined score.
669
+ Default is 1, which gives equal weight to precision and recall.
594
670
 
595
671
  Returns:
596
- float: The time tolerant F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
672
+ float: The time tolerant F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
597
673
 
598
674
  """
599
675
  precision = time_tolerant_precision(y_true,y_pred,t)
@@ -618,14 +694,19 @@ def range_based_recall(y_true: np.array, y_pred: np.array, alpha: float, bias='f
618
694
  https://proceedings.neurips.cc/paper_files/paper/2018/file/8f468c873a32bb0619eaeb2050ba45d1-Paper.pdf
619
695
 
620
696
  Parameters:
621
- y_true (np.array): The ground truth binary labels for the time series data.
622
- y_pred (np.array): The predicted binary labels for the time series data.
623
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
624
- bias (str): Positional bias. This should be "flat", "front", "middle", or "back".
625
- cardinality_mode (str, optional): Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
697
+ y_true (np.array):
698
+ The ground truth binary labels for the time series data.
699
+ y_pred (np.array):
700
+ The predicted binary labels for the time series data.
701
+ alpha (float):
702
+ Relative importance of existence reward. 0 ≤ alpha ≤ 1.
703
+ bias (str):
704
+ Positional bias. This should be "flat", "front", "middle", or "back".
705
+ cardinality_mode (str, optional):
706
+ Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
626
707
 
627
708
  Returns:
628
- float: The range-based recall score.
709
+ float: The range-based recall score.
629
710
  """
630
711
  if np.sum(y_pred) == 0:
631
712
  return 0
@@ -647,14 +728,19 @@ def range_based_precision(y_true: np.array, y_pred: np.array, alpha: float, bias
647
728
  https://proceedings.neurips.cc/paper_files/paper/2018/file/8f468c873a32bb0619eaeb2050ba45d1-Paper.pdf
648
729
 
649
730
  Parameters:
650
- y_true (np.array): The ground truth binary labels for the time series data.
651
- y_pred (np.array): The predicted binary labels for the time series data.
652
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
653
- bias (str): Positional bias. This should be "flat", "front", "middle", or "back".
654
- cardinality_mode (str, optional): Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
731
+ y_true (np.array):
732
+ The ground truth binary labels for the time series data.
733
+ y_pred (np.array):
734
+ The predicted binary labels for the time series data.
735
+ alpha (float):
736
+ Relative importance of existence reward. 0 ≤ alpha ≤ 1.
737
+ bias (str):
738
+ Positional bias. This should be "flat", "front", "middle", or "back".
739
+ cardinality_mode (str, optional):
740
+ Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
655
741
 
656
742
  Returns:
657
- float: The range-based precision score.
743
+ float: The range-based precision score.
658
744
  """
659
745
  if np.sum(y_pred) == 0:
660
746
  return 0
@@ -680,17 +766,24 @@ def range_based_f_score(y_true: np.array, y_pred: np.array, p_alpha: float, r_al
680
766
 
681
767
 
682
768
  Parameters:
683
- y_true (np.array): The ground truth binary labels for the time series data.
684
- y_pred (np.array): The predicted binary labels for the time series data.
685
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
686
- p_bias (str): Positional bias for precision. This should be "flat", "front", "middle", or "back".
687
- r_bias (str): Positional bias for recall. This should be "flat", "front", "middle", or "back".
688
- cardinality_mode (str, optional): Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
689
- beta (float): The beta value, which determines the weight of precision in the combined score.
690
- Default is 1, which gives equal weight to precision and recall.
769
+ y_true (np.array):
770
+ The ground truth binary labels for the time series data.
771
+ y_pred (np.array):
772
+ The predicted binary labels for the time series data.
773
+ alpha (float):
774
+ Relative importance of existence reward. 0 alpha 1.
775
+ p_bias (str):
776
+ Positional bias for precision. This should be "flat", "front", "middle", or "back".
777
+ r_bias (str):
778
+ Positional bias for recall. This should be "flat", "front", "middle", or "back".
779
+ cardinality_mode (str, optional):
780
+ Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
781
+ beta (float):
782
+ The beta value, which determines the weight of precision in the combined score.
783
+ Default is 1, which gives equal weight to precision and recall.
691
784
 
692
785
  Returns:
693
- float: The range-based F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
786
+ float: The range-based F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
694
787
  """
695
788
  if np.sum(y_pred) == 0:
696
789
  return 0
@@ -712,22 +805,28 @@ def ts_aware_recall(y_true: np.array, y_pred: np.array, alpha: float, delta: flo
712
805
  focusing solely on overlap fraction and end‑tolerance decay.
713
806
 
714
807
  Parameters:
715
- y_true (np.array): The ground truth binary labels for the time series data.
716
- y_pred (np.array): The predicted binary labels for the time series data.
717
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
718
- delta (float): Tolerance window length at the end of each true anomaly segment.
719
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
720
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
721
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
722
- time steps to extend each segment.
723
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
724
- predictions for the segment to count as detected.
725
- past_range (bool): Determines how δ is interpreted.
726
- - True: δ is treated as a fractional extension of each segment’s length.
727
- - False: δ is treated as an absolute number of time steps.
808
+ y_true (np.array):
809
+ The ground truth binary labels for the time series data.
810
+ y_pred (np.array):
811
+ The predicted binary labels for the time series data.
812
+ alpha (float):
813
+ Relative importance of the existence reward versus overlap reward (0 α 1).
814
+ delta (float):
815
+ Tolerance window length at the end of each true anomaly segment.
816
+ - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
817
+ length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
818
+ - If past_range is False, δ must be a non-negative integer, representing an absolute number of
819
+ time steps to extend each segment.
820
+ theta (float):
821
+ Minimum fraction (0 ≤ θ ≤ 1) of the true anomaly range that must be overlapped by
822
+ predictions for the segment to count as detected.
823
+ past_range (bool):
824
+ Determines how δ is interpreted.
825
+ - True: δ is treated as a fractional extension of each segment’s length.
826
+ - False: δ is treated as an absolute number of time steps.
728
827
 
729
828
  Returns:
730
- float: The time series aware recall score.
829
+ float: The time series aware recall score.
731
830
  """
732
831
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
733
832
  return m.recall()
@@ -747,22 +846,28 @@ def ts_aware_precision(y_true: np.array, y_pred: np.array,alpha: float, delta: f
747
846
  focusing solely on overlap fraction and end‑tolerance decay.
748
847
 
749
848
  Parameters:
750
- y_true (np.array): The ground truth binary labels for the time series data.
751
- y_pred (np.array): The predicted binary labels for the time series data.
752
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
753
- delta (float): Tolerance window length at the end of each true anomaly segment.
754
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
755
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
756
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
757
- time steps to extend each segment.
758
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
759
- predictions for the segment to count as detected.
760
- past_range (bool): Determines how δ is interpreted.
761
- - True: δ is treated as a fractional extension of each segment’s length.
762
- - False: δ is treated as an absolute number of time steps.
849
+ y_true (np.array):
850
+ The ground truth binary labels for the time series data.
851
+ y_pred (np.array):
852
+ The predicted binary labels for the time series data.
853
+ alpha (float):
854
+ Relative importance of the existence reward versus overlap reward (0 α 1).
855
+ delta (float):
856
+ Tolerance window length at the end of each true anomaly segment.
857
+ - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
858
+ length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
859
+ - If past_range is False, δ must be a non-negative integer, representing an absolute number of
860
+ time steps to extend each segment.
861
+ theta (float):
862
+ Minimum fraction (0 ≤ θ ≤ 1) of the true anomaly range that must be overlapped by
863
+ predictions for the segment to count as detected.
864
+ past_range (bool):
865
+ Determines how δ is interpreted.
866
+ - True: δ is treated as a fractional extension of each segment’s length.
867
+ - False: δ is treated as an absolute number of time steps.
763
868
 
764
869
  Returns:
765
- float: The time series aware precision score.
870
+ float: The time series aware precision score.
766
871
  """
767
872
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
768
873
  return m.precision()
@@ -783,22 +888,28 @@ def ts_aware_f_score(y_true: np.array, y_pred: np.array, beta: float, alpha: flo
783
888
  focusing solely on overlap fraction and end‑tolerance decay.
784
889
 
785
890
  Parameters:
786
- y_true (np.array): The ground truth binary labels for the time series data.
787
- y_pred (np.array): The predicted binary labels for the time series data.
788
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
789
- delta (float): Tolerance window length at the end of each true anomaly segment.
790
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
791
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
792
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
793
- time steps to extend each segment.
794
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
795
- predictions for the segment to count as detected.
796
- past_range (bool): Determines how δ is interpreted.
797
- - True: δ is treated as a fractional extension of each segment’s length.
798
- - False: δ is treated as an absolute number of time steps.
891
+ y_true (np.array):
892
+ The ground truth binary labels for the time series data.
893
+ y_pred (np.array):
894
+ The predicted binary labels for the time series data.
895
+ alpha (float):
896
+ Relative importance of the existence reward versus overlap reward (0 α 1).
897
+ delta (float):
898
+ Tolerance window length at the end of each true anomaly segment.
899
+ - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
900
+ length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
901
+ - If past_range is False, δ must be a non-negative integer, representing an absolute number of
902
+ time steps to extend each segment.
903
+ theta (float):
904
+ Minimum fraction (0 ≤ θ ≤ 1) of the true anomaly range that must be overlapped by
905
+ predictions for the segment to count as detected.
906
+ past_range (bool):
907
+ Determines how δ is interpreted.
908
+ - True: δ is treated as a fractional extension of each segment’s length.
909
+ - False: δ is treated as an absolute number of time steps.
799
910
 
800
911
  Returns:
801
- float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
912
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
802
913
  """
803
914
 
804
915
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
@@ -824,13 +935,16 @@ def enhanced_ts_aware_recall(y_true: np.array, y_pred: np.array, theta: float):
824
935
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
825
936
 
826
937
  Parameters:
827
- y_true (np.array): The ground truth binary labels for the time series data.
828
- y_pred (np.array): The predicted binary labels for the time series data.
829
- theta (float): Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
938
+ y_true (np.array):
939
+ The ground truth binary labels for the time series data.
940
+ y_pred (np.array):
941
+ The predicted binary labels for the time series data.
942
+ theta (float):
943
+ Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
830
944
  by predictions to count as detected.
831
945
 
832
946
  Returns:
833
- float: The time series aware recall score.
947
+ float: The time series aware recall score.
834
948
  """
835
949
  if np.sum(y_pred) == 0:
836
950
  return 0
@@ -851,13 +965,16 @@ def enhanced_ts_aware_precision(y_true: np.array, y_pred: np.array, theta: float
851
965
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
852
966
 
853
967
  Parameters:
854
- y_true (np.array): The ground truth binary labels for the time series data.
855
- y_pred (np.array): The predicted binary labels for the time series data.
856
- theta (float): Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
968
+ y_true (np.array):
969
+ The ground truth binary labels for the time series data.
970
+ y_pred (np.array):
971
+ The predicted binary labels for the time series data.
972
+ theta (float):
973
+ Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
857
974
  by predictions to count as detected.
858
975
 
859
976
  Returns:
860
- float: The time series aware precision score.
977
+ float: The time series aware precision score.
861
978
  """
862
979
  if np.sum(y_pred) == 0:
863
980
  return 0
@@ -879,13 +996,16 @@ def enhanced_ts_aware_f_score(y_true: np.array, y_pred: np.array, theta_p: float
879
996
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
880
997
 
881
998
  Parameters:
882
- y_true (np.array): The ground truth binary labels for the time series data.
883
- y_pred (np.array): The predicted binary labels for the time series data.
884
- theta (float): Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
999
+ y_true (np.array):
1000
+ The ground truth binary labels for the time series data.
1001
+ y_pred (np.array):
1002
+ The predicted binary labels for the time series data.
1003
+ theta (float):
1004
+ Minimum fraction (0 ≤ θ ≤ 1) of a true segment that must be overlapped
885
1005
  by predictions to count as detected.
886
1006
 
887
1007
  Returns:
888
- float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
1008
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
889
1009
  """
890
1010
  if np.sum(y_pred) == 0:
891
1011
  return 0
@@ -903,11 +1023,13 @@ def affiliation_based_recall(y_true: np.array, y_pred: np.array):
903
1023
  predicted anomaly point.
904
1024
 
905
1025
  Parameters:
906
- y_true (np.array): The ground truth binary labels for the time series data.
907
- y_pred (np.array): The predicted binary labels for the time series data.
1026
+ y_true (np.array):
1027
+ The ground truth binary labels for the time series data.
1028
+ y_pred (np.array):
1029
+ The predicted binary labels for the time series data.
908
1030
 
909
1031
  Returns:
910
- float: The affiliation based recall score.
1032
+ float: The affiliation based recall score.
911
1033
  """
912
1034
  if np.sum(y_pred) == 0:
913
1035
  return 0
@@ -925,12 +1047,14 @@ def affiliation_based_precision(y_true: np.array, y_pred: np.array):
925
1047
  ground truth anomaly point.
926
1048
 
927
1049
  Parameters:
928
- y_true (np.array): The ground truth binary labels for the time series data.
929
- y_pred (np.array): The predicted binary labels for the time series data.
1050
+ y_true (np.array):
1051
+ The ground truth binary labels for the time series data.
1052
+ y_pred (np.array):
1053
+ The predicted binary labels for the time series data.
930
1054
 
931
1055
 
932
1056
  Returns:
933
- float: The affiliation based precision score.
1057
+ float: The affiliation based precision score.
934
1058
  """
935
1059
  if np.sum(y_pred) == 0:
936
1060
  return 0
@@ -950,13 +1074,16 @@ def affiliation_based_f_score(y_true: np.array, y_pred: np.array, beta=1):
950
1074
  anomalies and vice versa.
951
1075
 
952
1076
  Parameters:
953
- y_true (np.array): The ground truth binary labels for the time series data.
954
- y_pred (np.array): The predicted binary labels for the time series data.
955
- beta (float): The beta value, which determines the weight of precision in the combined score.
1077
+ y_true (np.array):
1078
+ The ground truth binary labels for the time series data.
1079
+ y_pred (np.array):
1080
+ The predicted binary labels for the time series data.
1081
+ beta (float):
1082
+ The beta value, which determines the weight of precision in the combined score.
956
1083
 
957
1084
 
958
1085
  Returns:
959
- float: The affiliation based F-score.
1086
+ float: The affiliation based F-score.
960
1087
  """
961
1088
  if np.sum(y_pred) == 0:
962
1089
  return 0
@@ -974,12 +1101,14 @@ def nab_score(y_true: np.array, y_pred: np.array):
974
1101
  positive prediction contributes negatively.
975
1102
 
976
1103
  Parameters:
977
- y_true (np.array): The ground truth binary labels for the time series data.
978
- y_pred (np.array): The predicted binary labels for the time series data.
1104
+ y_true (np.array):
1105
+ The ground truth binary labels for the time series data.
1106
+ y_pred (np.array):
1107
+ The predicted binary labels for the time series data.
979
1108
 
980
1109
 
981
1110
  Returns:
982
- float: The nab score.
1111
+ float: The nab score.
983
1112
  """
984
1113
 
985
1114
  m = NAB_score(len(y_true),y_true,y_pred)
@@ -995,15 +1124,18 @@ def temporal_distance(y_true: np.array, y_pred: np.array, distance: int = 0):
995
1124
 
996
1125
 
997
1126
  Parameters:
998
- y_true (np.array): The ground truth binary labels for the time series data.
999
- y_pred (np.array): The predicted binary labels for the time series data.
1000
- distance (int): The distance type parameter for the temporal distance calculation.
1001
- 0: Euclidean distance
1002
- 1: Squared Euclidean distance
1127
+ y_true (np.array):
1128
+ The ground truth binary labels for the time series data.
1129
+ y_pred (np.array):
1130
+ The predicted binary labels for the time series data.
1131
+ distance (int):
1132
+ The distance type parameter for the temporal distance calculation.
1133
+ 0: Euclidean distance
1134
+ 1: Squared Euclidean distance
1003
1135
 
1004
1136
 
1005
1137
  Returns:
1006
- float: The temporal distance.
1138
+ float: The temporal distance.
1007
1139
  """
1008
1140
 
1009
1141
  m = Temporal_Distance(len(y_true),y_true,y_pred,distance=distance)
@@ -1021,12 +1153,14 @@ def average_detection_count(y_true: np.array, y_pred: np.array):
1021
1153
  https://ceur-ws.org/Vol-1226/paper31.pdf
1022
1154
 
1023
1155
  Parameters:
1024
- y_true (np.array): The ground truth binary labels for the time series data.
1025
- y_pred (np.array): The predicted binary labels for the time series data.
1156
+ y_true (np.array):
1157
+ The ground truth binary labels for the time series data.
1158
+ y_pred (np.array):
1159
+ The predicted binary labels for the time series data.
1026
1160
 
1027
1161
 
1028
1162
  Returns:
1029
- float: The average detection count score.
1163
+ float: The average detection count score.
1030
1164
  """
1031
1165
 
1032
1166
  b = Binary_detection(len(y_true),y_true,y_pred)
@@ -1052,15 +1186,18 @@ def absolute_detection_distance(y_true: np.array, y_pred: np.array):
1052
1186
  those distances and divides by the total number of such matching predicted points, yielding the
1053
1187
  mean distance to segment centers for correctly detected points.
1054
1188
 
1055
- Parameters:
1056
- y_true (np.array): The ground truth binary labels for the time series data.
1057
- y_pred (np.array): The predicted binary labels for the time series data.
1058
-
1059
1189
  For more information, see the original paper:
1060
1190
  https://ceur-ws.org/Vol-1226/paper31.pdf
1061
1191
 
1192
+ Parameters:
1193
+ y_true (np.array):
1194
+ The ground truth binary labels for the time series data.
1195
+ y_pred (np.array):
1196
+ The predicted binary labels for the time series data.
1197
+
1198
+
1062
1199
  Returns:
1063
- float: The absolute detection distance.
1200
+ float: The absolute detection distance.
1064
1201
  """
1065
1202
 
1066
1203
  b = Binary_detection(len(y_true),y_true,y_pred)
@@ -1096,13 +1233,16 @@ def total_detected_in_range(y_true: np.array, y_pred: np.array, k: int):
1096
1233
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1097
1234
 
1098
1235
  Parameters:
1099
- y_true (np.array): The ground truth binary labels for the time series data.
1100
- y_pred (np.array): The predicted binary labels for the time series data.
1101
- k (int): Half-window size for tolerance around each true anomaly point. A prediction within k
1102
- time steps of a true point counts toward detection.
1236
+ y_true (np.array):
1237
+ The ground truth binary labels for the time series data.
1238
+ y_pred (np.array):
1239
+ The predicted binary labels for the time series data.
1240
+ k (int):
1241
+ Half-window size for tolerance around each true anomaly point. A prediction within k
1242
+ time steps of a true point counts toward detection.
1103
1243
 
1104
1244
  Returns:
1105
- float: The total detected in range score.
1245
+ float: The total detected in range score.
1106
1246
  """
1107
1247
  if np.sum(y_pred) == 0:
1108
1248
  return 0
@@ -1131,13 +1271,16 @@ def detection_accuracy_in_range(y_true: np.array, y_pred: np.array, k: int):
1131
1271
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1132
1272
 
1133
1273
  Parameters:
1134
- y_true (np.array): The ground truth binary labels for the time series data.
1135
- y_pred (np.array): The predicted binary labels for the time series data.
1136
- k (int): Half-window size for tolerance around each true anomaly point. A prediction within k
1137
- time steps of a true point counts toward detection.
1274
+ y_true (np.array):
1275
+ The ground truth binary labels for the time series data.
1276
+ y_pred (np.array):
1277
+ The predicted binary labels for the time series data.
1278
+ k (int):
1279
+ Half-window size for tolerance around each true anomaly point. A prediction within k
1280
+ time steps of a true point counts toward detection.
1138
1281
 
1139
1282
  Returns:
1140
- float: The total detected in range.
1283
+ float: The total detected in range.
1141
1284
  """
1142
1285
  if np.sum(y_pred) == 0:
1143
1286
  return 0
@@ -1174,12 +1317,15 @@ def weighted_detection_difference(y_true: np.array, y_pred: np.array, k: int):
1174
1317
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1175
1318
 
1176
1319
  Parameters:
1177
- y_true (np.array): The ground truth binary labels for the time series data.
1178
- y_pred (np.array): The predicted binary labels for the time series data.
1179
- k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
1320
+ y_true (np.array):
1321
+ The ground truth binary labels for the time series data.
1322
+ y_pred (np.array):
1323
+ The predicted binary labels for the time series data.
1324
+ k (int):
1325
+ The maximum number of time steps within which an anomaly must be predicted to be considered detected.
1180
1326
 
1181
1327
  Returns:
1182
- float: The weighted detection difference.
1328
+ float: The weighted detection difference.
1183
1329
  """
1184
1330
  if np.sum(y_pred) == 0:
1185
1331
  return 0
@@ -1231,13 +1377,17 @@ def binary_pate(y_true: np.array, y_pred: np.array, early: int, delay: int):
1231
1377
  https://arxiv.org/abs/2405.12096
1232
1378
 
1233
1379
  Parameters:
1234
- y_true (np.array): The ground truth binary labels for the time series data.
1235
- y_pred (np.array): The predicted binary labels for the time series data.
1236
- early (int): The maximum number of time steps before an anomaly must be predicted to be considered early.
1237
- delay (int): The maximum number of time steps after an anomaly must be predicted to be considered delayed.
1380
+ y_true (np.array):
1381
+ The ground truth binary labels for the time series data.
1382
+ y_pred (np.array):
1383
+ The predicted binary labels for the time series data.
1384
+ early (int):
1385
+ The maximum number of time steps before an anomaly must be predicted to be considered early.
1386
+ delay (int):
1387
+ The maximum number of time steps after an anomaly must be predicted to be considered delayed.
1238
1388
 
1239
1389
  Returns:
1240
- float: The PATE score.
1390
+ float: The PATE score.
1241
1391
  """
1242
1392
 
1243
1393
  return PATE(y_true, y_pred, early, delay, binary_scores=True)
@@ -1255,14 +1405,16 @@ def mean_time_to_detect(y_true: np.array, y_pred: np.array):
1255
1405
  the average number of time steps between the true onset of an anomaly and its first detection.
1256
1406
 
1257
1407
  Parameters:
1258
- y_true (np.array): The ground truth binary labels for the time series data.
1259
- y_pred (np.array): The predicted binary labels for the time series data.
1408
+ y_true (np.array):
1409
+ The ground truth binary labels for the time series data.
1410
+ y_pred (np.array):
1411
+ The predicted binary labels for the time series data.
1260
1412
 
1261
1413
  For more information, see the original paper:
1262
1414
  https://arxiv.org/pdf/2211.05244
1263
1415
 
1264
1416
  Returns:
1265
- float: The mean time to detect.
1417
+ float: The mean time to detect.
1266
1418
  """
1267
1419
 
1268
1420
  b = Binary_detection(len(y_true),y_true,y_pred)
@@ -15,8 +15,13 @@ def precision_at_k(y_true : np.array, y_anomaly_scores: np.array):
15
15
  y_true. That is, k = sum(y_true).
16
16
 
17
17
  Parameters:
18
- y_true (np.array): The ground truth binary labels for the time series data.
19
- y_anomaly_scores (np.array): The predicted anomaly scores for the time series data.
18
+ y_true (np.array):
19
+ The ground truth binary labels for the time series data.
20
+ y_anomaly_scores (np.array):
21
+ The predicted anomaly scores for the time series data.
22
+
23
+ Returns:
24
+ float: The precision at k score.
20
25
  """
21
26
  m = PatK_pw(y_true,y_anomaly_scores)
22
27
 
@@ -31,8 +36,10 @@ def auc_roc_pw(y_true : np.array, y_anomaly_scores: np.array):
31
36
  independently when calculating true positives, false positives, and false negatives.
32
37
 
33
38
  Parameters:
34
- y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
35
- y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
39
+ y_true (np.array):
40
+ Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
41
+ y_anomaly_scores (np.array):
42
+ Continuous anomaly scores assigned to each point in the series.
36
43
 
37
44
  Returns:
38
45
  float: AUC-ROC score.
@@ -52,8 +59,10 @@ def auc_pr_pw(y_true : np.array ,y_anomaly_scores: np.array):
52
59
  independently when calculating precision and recall.
53
60
 
54
61
  Parameters:
55
- y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
56
- y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
62
+ y_true (np.array):
63
+ Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
64
+ y_anomaly_scores (np.array):
65
+ Continuous anomaly scores assigned to each point in the series.
57
66
 
58
67
  Returns:
59
68
  float: AUC-PR score.
@@ -76,8 +85,10 @@ def auc_pr_pa(y_true: np.array, y_anomaly_scores: np.array):
76
85
  which are used to construct the PR curve.
77
86
 
78
87
  Parameters:
79
- y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
80
- y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
88
+ y_true (np.array):
89
+ Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
90
+ y_anomaly_scores (np.array):
91
+ Continuous anomaly scores assigned to each point in the series.
81
92
 
82
93
  Returns:
83
94
  float: AUC-PR score (with point-adjusted evaluation).
@@ -166,8 +177,10 @@ def auc_pr_sw(y_true: np.array, y_anomaly_scores: np.array):
166
177
  to compute precision and recall for constructing the PR curve.
167
178
 
168
179
  Parameters:
169
- y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
170
- y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
180
+ y_true (np.array):
181
+ Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
182
+ y_anomaly_scores (np.array):
183
+ Continuous anomaly scores assigned to each point in the series.
171
184
 
172
185
  Returns:
173
186
  float: AUC-PR score (with segment-wise evaluation).
@@ -271,16 +284,21 @@ def vus_roc(y_true : np.array ,y_anomaly_scores: np.array, window=4):
271
284
  the ROC-AUC over different values of the tolerance parameter, from 0 to `window`, thus producing
272
285
  a volume under the ROC surface.
273
286
 
287
+ For more information, see the original paper:
288
+ https://dl.acm.org/doi/10.14778/3551793.3551830
289
+
274
290
  Parameters:
275
- y_true (np.array): Ground-truth binary labels (0 = normal, 1 = anomaly).
276
- y_anomaly_scores (np.array): Anomaly scores for each time point.
277
- window (int): Maximum temporal tolerance `l` used to smooth the evaluation.
291
+ y_true (np.array):
292
+ Ground-truth binary labels (0 = normal, 1 = anomaly).
293
+ y_anomaly_scores (np.array):
294
+ Anomaly scores for each time point.
295
+ window (int):
296
+ Maximum temporal tolerance `l` used to smooth the evaluation.
278
297
 
279
298
  Returns:
280
299
  float: VUS-ROC score.
281
300
 
282
- For more information, see the original paper:
283
- https://dl.acm.org/doi/10.14778/3551793.3551830
301
+
284
302
  """
285
303
  m = VUS_ROC(y_true,y_anomaly_scores,max_window=window)
286
304
 
@@ -296,16 +314,21 @@ def vus_pr(y_true : np.array ,y_anomaly_scores: np.array, window=4):
296
314
  anomalies that are temporally close to the true events. The final metric integrates the PR-AUC
297
315
  over several levels of temporal tolerance (from 0 to `window`), yielding a volume under the PR surface.
298
316
 
317
+ For more information, see the original paper:
318
+ https://dl.acm.org/doi/10.14778/3551793.3551830
319
+
299
320
  Parameters:
300
- y_true (np.array): Ground-truth binary labels (0 = normal, 1 = anomaly).
301
- y_anomaly_scores (np.array): Anomaly scores for each time point.
302
- window (int): Maximum temporal tolerance `l` used to smooth the evaluation.
321
+ y_true (np.array):
322
+ Ground-truth binary labels (0 = normal, 1 = anomaly).
323
+ y_anomaly_scores (np.array):
324
+ Anomaly scores for each time point.
325
+ window (int):
326
+ Maximum temporal tolerance `l` used to smooth the evaluation.
303
327
 
304
328
  Returns:
305
329
  float: VUS-PR score.
306
330
 
307
- For more information, see the original paper:
308
- https://dl.acm.org/doi/10.14778/3551793.3551830
331
+
309
332
  """
310
333
  m = VUS_PR(y_true,y_anomaly_scores,max_window=window)
311
334
 
@@ -331,10 +354,14 @@ def real_pate(y_true: np.array, y_anomaly_scores: np.array, early: int, delay: i
331
354
  https://arxiv.org/abs/2405.12096
332
355
 
333
356
  Parameters:
334
- y_true (np.array): Ground truth binary labels (0 = normal, 1 = anomaly).
335
- y_anomaly_scores (np.array): Real-valued anomaly scores for each time point.
336
- early (int): Length of the early buffer zone before each anomaly interval.
337
- delay (int): Length of the delay buffer zone after each anomaly interval.
357
+ y_true (np.array):
358
+ Ground truth binary labels (0 = normal, 1 = anomaly).
359
+ y_anomaly_scores (np.array):
360
+ Real-valued anomaly scores for each time point.
361
+ early (int):
362
+ Length of the early buffer zone before each anomaly interval.
363
+ delay (int):
364
+ Length of the delay buffer zone after each anomaly interval.
338
365
 
339
366
  Returns:
340
367
  float: The real-valued PATE score.
tsadmetrics/utils.py CHANGED
@@ -7,14 +7,20 @@ def compute_metrics(y_true: np.array,y_pred: np.array, metrics: list, metrics_pa
7
7
  Computes the specified metrics for the given true and predicted values.
8
8
 
9
9
  Parameters:
10
- - y_true (np.array): True labels.
11
- - y_pred (np.array): Predicted labels or scores.
12
- - metrics (list): List of metric names to compute.
13
- - metrics_params (dict): Dictionary of parameters for each metric.
14
- - is_anomaly_score (bool): Flag indicating if y_true and y_pred are anomaly scores. Otherwise, they are treated as binary labels.
15
- - verbose (bool): Flag to print additional information.
10
+ y_true (np.array):
11
+ True labels.
12
+ y_pred (np.array):
13
+ Predicted labels or scores.
14
+ metrics (list):
15
+ List of metric names to compute.
16
+ metrics_params (dict):
17
+ Dictionary of parameters for each metric.
18
+ is_anomaly_score (bool):
19
+ Flag indicating if y_true and y_pred are anomaly scores. Otherwise, they are treated as binary labels.
20
+ verbose (bool):
21
+ Flag to print additional information.
16
22
  Returns:
17
- - metrics_df (DataFrame): DataFrame containing the computed metrics and their values.
23
+ results (DataFrame): DataFrame containing the computed metrics and their values.
18
24
  """
19
25
  if not (np.array_equal(np.unique(y_true), [0, 1]) or np.array_equal(np.unique(y_true), [0]) or np.array_equal(np.unique(y_true), [1])):
20
26
  raise ValueError("y_true must be binary labels (0 or 1).")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tsadmetrics
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: =?unknown-8bit?q?Librer=C3=ADa_para_evaluaci=C3=B3n_de_detecci=C3=B3n_de_anomal=C3=ADas?= en series temporales
5
5
  Home-page: https://github.com/pathsko/TSADmetrics
6
6
  Author: Pedro Rafael Velasco Priego
@@ -16,11 +16,11 @@ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
16
  tests/test_binary.py,sha256=dj9BsKBo5rpWw4JGiKKoVkg4rIW4YylTie2VxH2DAGo,29787
17
17
  tests/test_non_binary.py,sha256=syANlwm0DKsL6geGeq6nQI6ZVe6T_YXWTyk2-Hmck4s,11308
18
18
  tsadmetrics/__init__.py,sha256=MTWOa43fgOdkMNo5NglCReRnB8hoF0ob2PIvDziCNHw,1575
19
- tsadmetrics/binary_metrics.py,sha256=pEIe8s3_obGN1hHhfvQwg0BXKafs4lQ3l1-K03P3Ews,60067
19
+ tsadmetrics/binary_metrics.py,sha256=UdXoITIATf585Kvz5i3xhdqEVUogrUHMmJgxj-HoeB0,62477
20
20
  tsadmetrics/metric_utils.py,sha256=fm8v0X37_AlqWpkcUT9r3680QsjLljrHe2YuXkRLAZ4,10873
21
- tsadmetrics/non_binary_metrics.py,sha256=yo620BWZIq-OkBqQV7t7ynjGhcuX6QWQ6iq_7eJq9gI,13074
21
+ tsadmetrics/non_binary_metrics.py,sha256=O6AqceHrjCVV1kJPBzXQIgtiu6afzoiJz2biNsxf3_4,13389
22
22
  tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- tsadmetrics/utils.py,sha256=15X_RkHdCxhu_-OH8fEm3gRVQ4tTMqCkNaQsQoloEYQ,2361
23
+ tsadmetrics/utils.py,sha256=BqsG4DyP3AffuMFQCJ-Qy4YaDu4IkFudZWYCvyTGvvY,2444
24
24
  tsadmetrics/_tsadeval/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
25
  tsadmetrics/_tsadeval/auc_roc_pr_plot.py,sha256=PHqJUXq2qI248XV9o04D8SsUJgowetaKq0Cu5bYrIAE,12689
26
26
  tsadmetrics/_tsadeval/discontinuity_graph.py,sha256=Ci65l_DPi6HTtb8NvQJe1najgGrRuEpOMWvSyi2AeR0,4088
@@ -53,7 +53,7 @@ tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py,sha256=pJz4iuPyVGNvwsaR
53
53
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py,sha256=jLkcMg7UNl25SHtZUBGkP-RV8HsvaZCtjakryl7PFWU,3204
54
54
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py,sha256=OhUJSm_I7VZ_gX_SSg8AYUq3_NW9rMIy7lAVsnOFw4Q,417
55
55
  tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py,sha256=LL-0pPer3ymovVRlktaHo5XDzpgiDhWOVfdPOzKR6og,3152
56
- tsadmetrics-0.1.11.dist-info/METADATA,sha256=JrsyLRUVbWIhrBkE56hn3ALYUycm3j52kSmmcq8TMhA,831
57
- tsadmetrics-0.1.11.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
58
- tsadmetrics-0.1.11.dist-info/top_level.txt,sha256=s2VIr_ePl-WZbYt9FsYbsDGM7J-Qc5cgpwEOeQ3FVpM,31
59
- tsadmetrics-0.1.11.dist-info/RECORD,,
56
+ tsadmetrics-0.1.13.dist-info/METADATA,sha256=k3BVjOM_Ife2-uXsEWu3o-6tRTUXUqLDd9I9NBV-61g,831
57
+ tsadmetrics-0.1.13.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
58
+ tsadmetrics-0.1.13.dist-info/top_level.txt,sha256=s2VIr_ePl-WZbYt9FsYbsDGM7J-Qc5cgpwEOeQ3FVpM,31
59
+ tsadmetrics-0.1.13.dist-info/RECORD,,