tsadmetrics 0.1.12__py3-none-any.whl → 0.1.14__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)
@@ -539,12 +602,15 @@ def time_tolerant_recall(y_true: np.array, y_pred: np.array, t: int) -> float:
539
602
  Implementation of https://arxiv.org/pdf/1802.04431
540
603
 
541
604
  Parameters:
542
- y_true (np.array): The ground truth binary labels for the time series data.
543
- y_pred (np.array): The predicted binary labels for the time series data.
544
- 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
545
611
 
546
612
  Returns:
547
- 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.
548
614
  """
549
615
  if np.sum(y_pred) == 0:
550
616
  return 0
@@ -564,12 +630,15 @@ def time_tolerant_precision(y_true: np.array, y_pred: np.array, t: int) -> float
564
630
  Implementation of https://arxiv.org/pdf/1802.04431
565
631
 
566
632
  Parameters:
567
- y_true (np.array): The ground truth binary labels for the time series data.
568
- y_pred (np.array): The predicted binary labels for the time series data.
569
- 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
570
639
 
571
640
  Returns:
572
- 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.
573
642
  """
574
643
  if np.sum(y_pred) == 0:
575
644
  return 0
@@ -589,14 +658,18 @@ def time_tolerant_f_score(y_true: np.array, y_pred: np.array, t: int, beta=1):
589
658
  Implementation of https://arxiv.org/pdf/1802.04431
590
659
 
591
660
  Parameters:
592
- y_true (np.array): The ground truth binary labels for the time series data.
593
- y_pred (np.array): The predicted binary labels for the time series data.
594
- t (int): The time tolerance parameter
595
- beta (float): The beta value, which determines the weight of precision in the combined score.
596
- 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.
597
670
 
598
671
  Returns:
599
- 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.
600
673
 
601
674
  """
602
675
  precision = time_tolerant_precision(y_true,y_pred,t)
@@ -621,14 +694,19 @@ def range_based_recall(y_true: np.array, y_pred: np.array, alpha: float, bias='f
621
694
  https://proceedings.neurips.cc/paper_files/paper/2018/file/8f468c873a32bb0619eaeb2050ba45d1-Paper.pdf
622
695
 
623
696
  Parameters:
624
- y_true (np.array): The ground truth binary labels for the time series data.
625
- y_pred (np.array): The predicted binary labels for the time series data.
626
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
627
- bias (str): Positional bias. This should be "flat", "front", "middle", or "back".
628
- 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".
629
707
 
630
708
  Returns:
631
- float: The range-based recall score.
709
+ float: The range-based recall score.
632
710
  """
633
711
  if np.sum(y_pred) == 0:
634
712
  return 0
@@ -650,14 +728,19 @@ def range_based_precision(y_true: np.array, y_pred: np.array, alpha: float, bias
650
728
  https://proceedings.neurips.cc/paper_files/paper/2018/file/8f468c873a32bb0619eaeb2050ba45d1-Paper.pdf
651
729
 
652
730
  Parameters:
653
- y_true (np.array): The ground truth binary labels for the time series data.
654
- y_pred (np.array): The predicted binary labels for the time series data.
655
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
656
- bias (str): Positional bias. This should be "flat", "front", "middle", or "back".
657
- 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".
658
741
 
659
742
  Returns:
660
- float: The range-based precision score.
743
+ float: The range-based precision score.
661
744
  """
662
745
  if np.sum(y_pred) == 0:
663
746
  return 0
@@ -683,17 +766,24 @@ def range_based_f_score(y_true: np.array, y_pred: np.array, p_alpha: float, r_al
683
766
 
684
767
 
685
768
  Parameters:
686
- y_true (np.array): The ground truth binary labels for the time series data.
687
- y_pred (np.array): The predicted binary labels for the time series data.
688
- alpha (float): Relative importance of existence reward. 0 ≤ alpha ≤ 1.
689
- p_bias (str): Positional bias for precision. This should be "flat", "front", "middle", or "back".
690
- r_bias (str): Positional bias for recall. This should be "flat", "front", "middle", or "back".
691
- cardinality_mode (str, optional): Cardinality type. This should be "one", "reciprocal" or "udf_gamma".
692
- beta (float): The beta value, which determines the weight of precision in the combined score.
693
- 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.
694
784
 
695
785
  Returns:
696
- 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.
697
787
  """
698
788
  if np.sum(y_pred) == 0:
699
789
  return 0
@@ -715,22 +805,28 @@ def ts_aware_recall(y_true: np.array, y_pred: np.array, alpha: float, delta: flo
715
805
  focusing solely on overlap fraction and end‑tolerance decay.
716
806
 
717
807
  Parameters:
718
- y_true (np.array): The ground truth binary labels for the time series data.
719
- y_pred (np.array): The predicted binary labels for the time series data.
720
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
721
- delta (float): Tolerance window length at the end of each true anomaly segment.
722
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
723
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
724
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
725
- time steps to extend each segment.
726
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
727
- predictions for the segment to count as detected.
728
- past_range (bool): Determines how δ is interpreted.
729
- - True: δ is treated as a fractional extension of each segment’s length.
730
- - 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.
731
827
 
732
828
  Returns:
733
- float: The time series aware recall score.
829
+ float: The time series aware recall score.
734
830
  """
735
831
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
736
832
  return m.recall()
@@ -750,22 +846,28 @@ def ts_aware_precision(y_true: np.array, y_pred: np.array,alpha: float, delta: f
750
846
  focusing solely on overlap fraction and end‑tolerance decay.
751
847
 
752
848
  Parameters:
753
- y_true (np.array): The ground truth binary labels for the time series data.
754
- y_pred (np.array): The predicted binary labels for the time series data.
755
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
756
- delta (float): Tolerance window length at the end of each true anomaly segment.
757
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
758
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
759
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
760
- time steps to extend each segment.
761
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
762
- predictions for the segment to count as detected.
763
- past_range (bool): Determines how δ is interpreted.
764
- - True: δ is treated as a fractional extension of each segment’s length.
765
- - 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.
766
868
 
767
869
  Returns:
768
- float: The time series aware precision score.
870
+ float: The time series aware precision score.
769
871
  """
770
872
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
771
873
  return m.precision()
@@ -786,22 +888,28 @@ def ts_aware_f_score(y_true: np.array, y_pred: np.array, beta: float, alpha: flo
786
888
  focusing solely on overlap fraction and end‑tolerance decay.
787
889
 
788
890
  Parameters:
789
- y_true (np.array): The ground truth binary labels for the time series data.
790
- y_pred (np.array): The predicted binary labels for the time series data.
791
- alpha (float): Relative importance of the existence reward versus overlap reward (0 ≤ α ≤ 1).
792
- delta (float): Tolerance window length at the end of each true anomaly segment.
793
- - If past_range is True, δ must be a float in (0, 1], representing the fraction of the segment’s
794
- length to extend. E.g., δ = 0.5 extends a segment of length 10 by 5 time steps.
795
- - If past_range is False, δ must be a non-negative integer, representing an absolute number of
796
- time steps to extend each segment.
797
- theta (float): Minimum fraction (0 θ 1) of the true anomaly range that must be overlapped by
798
- predictions for the segment to count as detected.
799
- past_range (bool): Determines how δ is interpreted.
800
- - True: δ is treated as a fractional extension of each segment’s length.
801
- - 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.
802
910
 
803
911
  Returns:
804
- 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.
805
913
  """
806
914
 
807
915
  m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
@@ -827,13 +935,16 @@ def enhanced_ts_aware_recall(y_true: np.array, y_pred: np.array, theta: float):
827
935
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
828
936
 
829
937
  Parameters:
830
- y_true (np.array): The ground truth binary labels for the time series data.
831
- y_pred (np.array): The predicted binary labels for the time series data.
832
- 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
833
944
  by predictions to count as detected.
834
945
 
835
946
  Returns:
836
- float: The time series aware recall score.
947
+ float: The time series aware recall score.
837
948
  """
838
949
  if np.sum(y_pred) == 0:
839
950
  return 0
@@ -854,13 +965,16 @@ def enhanced_ts_aware_precision(y_true: np.array, y_pred: np.array, theta: float
854
965
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
855
966
 
856
967
  Parameters:
857
- y_true (np.array): The ground truth binary labels for the time series data.
858
- y_pred (np.array): The predicted binary labels for the time series data.
859
- 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
860
974
  by predictions to count as detected.
861
975
 
862
976
  Returns:
863
- float: The time series aware precision score.
977
+ float: The time series aware precision score.
864
978
  """
865
979
  if np.sum(y_pred) == 0:
866
980
  return 0
@@ -882,13 +996,16 @@ def enhanced_ts_aware_f_score(y_true: np.array, y_pred: np.array, theta_p: float
882
996
  true segment’s length, providing a compromise between point-wise and segment-wise approaches.
883
997
 
884
998
  Parameters:
885
- y_true (np.array): The ground truth binary labels for the time series data.
886
- y_pred (np.array): The predicted binary labels for the time series data.
887
- 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
888
1005
  by predictions to count as detected.
889
1006
 
890
1007
  Returns:
891
- 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.
892
1009
  """
893
1010
  if np.sum(y_pred) == 0:
894
1011
  return 0
@@ -906,11 +1023,13 @@ def affiliation_based_recall(y_true: np.array, y_pred: np.array):
906
1023
  predicted anomaly point.
907
1024
 
908
1025
  Parameters:
909
- y_true (np.array): The ground truth binary labels for the time series data.
910
- 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.
911
1030
 
912
1031
  Returns:
913
- float: The affiliation based recall score.
1032
+ float: The affiliation based recall score.
914
1033
  """
915
1034
  if np.sum(y_pred) == 0:
916
1035
  return 0
@@ -928,12 +1047,14 @@ def affiliation_based_precision(y_true: np.array, y_pred: np.array):
928
1047
  ground truth anomaly point.
929
1048
 
930
1049
  Parameters:
931
- y_true (np.array): The ground truth binary labels for the time series data.
932
- 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.
933
1054
 
934
1055
 
935
1056
  Returns:
936
- float: The affiliation based precision score.
1057
+ float: The affiliation based precision score.
937
1058
  """
938
1059
  if np.sum(y_pred) == 0:
939
1060
  return 0
@@ -953,13 +1074,16 @@ def affiliation_based_f_score(y_true: np.array, y_pred: np.array, beta=1):
953
1074
  anomalies and vice versa.
954
1075
 
955
1076
  Parameters:
956
- y_true (np.array): The ground truth binary labels for the time series data.
957
- y_pred (np.array): The predicted binary labels for the time series data.
958
- 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.
959
1083
 
960
1084
 
961
1085
  Returns:
962
- float: The affiliation based F-score.
1086
+ float: The affiliation based F-score.
963
1087
  """
964
1088
  if np.sum(y_pred) == 0:
965
1089
  return 0
@@ -977,12 +1101,14 @@ def nab_score(y_true: np.array, y_pred: np.array):
977
1101
  positive prediction contributes negatively.
978
1102
 
979
1103
  Parameters:
980
- y_true (np.array): The ground truth binary labels for the time series data.
981
- 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.
982
1108
 
983
1109
 
984
1110
  Returns:
985
- float: The nab score.
1111
+ float: The nab score.
986
1112
  """
987
1113
 
988
1114
  m = NAB_score(len(y_true),y_true,y_pred)
@@ -998,15 +1124,18 @@ def temporal_distance(y_true: np.array, y_pred: np.array, distance: int = 0):
998
1124
 
999
1125
 
1000
1126
  Parameters:
1001
- y_true (np.array): The ground truth binary labels for the time series data.
1002
- y_pred (np.array): The predicted binary labels for the time series data.
1003
- distance (int): The distance type parameter for the temporal distance calculation.
1004
- 0: Euclidean distance
1005
- 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
1006
1135
 
1007
1136
 
1008
1137
  Returns:
1009
- float: The temporal distance.
1138
+ float: The temporal distance.
1010
1139
  """
1011
1140
 
1012
1141
  m = Temporal_Distance(len(y_true),y_true,y_pred,distance=distance)
@@ -1024,12 +1153,14 @@ def average_detection_count(y_true: np.array, y_pred: np.array):
1024
1153
  https://ceur-ws.org/Vol-1226/paper31.pdf
1025
1154
 
1026
1155
  Parameters:
1027
- y_true (np.array): The ground truth binary labels for the time series data.
1028
- 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.
1029
1160
 
1030
1161
 
1031
1162
  Returns:
1032
- float: The average detection count score.
1163
+ float: The average detection count score.
1033
1164
  """
1034
1165
 
1035
1166
  b = Binary_detection(len(y_true),y_true,y_pred)
@@ -1055,15 +1186,18 @@ def absolute_detection_distance(y_true: np.array, y_pred: np.array):
1055
1186
  those distances and divides by the total number of such matching predicted points, yielding the
1056
1187
  mean distance to segment centers for correctly detected points.
1057
1188
 
1058
- Parameters:
1059
- y_true (np.array): The ground truth binary labels for the time series data.
1060
- y_pred (np.array): The predicted binary labels for the time series data.
1061
-
1062
1189
  For more information, see the original paper:
1063
1190
  https://ceur-ws.org/Vol-1226/paper31.pdf
1064
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
+
1065
1199
  Returns:
1066
- float: The absolute detection distance.
1200
+ float: The absolute detection distance.
1067
1201
  """
1068
1202
 
1069
1203
  b = Binary_detection(len(y_true),y_true,y_pred)
@@ -1086,26 +1220,35 @@ def total_detected_in_range(y_true: np.array, y_pred: np.array, k: int):
1086
1220
 
1087
1221
  This metric measures the proportion of true anomaly events that are correctly detected.
1088
1222
  It is defined as:
1089
- TDIR = (EM + DA) / (EM + DA + MA)
1223
+
1224
+ .. math::
1225
+ \\text{TDIR} = \\frac{EM + DA}{EM + DA + MA}
1090
1226
 
1091
1227
  Where:
1092
- EM (Exact Match) = number of predicted anomaly segments that exactly match a true anomaly segment.
1093
- DA (Detected Anomaly)= number of true anomaly points not exactly matched where at least one prediction falls
1094
- within a window [i-k, i+k] around the true point index i or within the true segment range.
1095
- FA (False Anomaly) = number of predicted anomaly segments that do not overlap any true anomaly segment
1096
- even within a k-step tolerance window around true points.
1228
+
1229
+ - EM (Exact Match):
1230
+ Number of predicted anomaly segments that exactly match a true anomaly segment.
1231
+ - DA (Detected Anomaly):
1232
+ Number of true anomaly points not exactly matched where at least one prediction falls
1233
+ within a window [i-k, i+k] around the true point index i or within the true segment range.
1234
+ - MA (Missed Anomaly):
1235
+ Number of true anomaly segments that do not overlap any predicted anomaly segment
1236
+ even within a k-step tolerance window around true points.
1097
1237
 
1098
1238
  For more information, see the original paper:
1099
1239
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1100
1240
 
1101
1241
  Parameters:
1102
- y_true (np.array): The ground truth binary labels for the time series data.
1103
- y_pred (np.array): The predicted binary labels for the time series data.
1104
- k (int): Half-window size for tolerance around each true anomaly point. A prediction within k
1105
- time steps of a true point counts toward detection.
1242
+ y_true (np.array):
1243
+ The ground truth binary labels for the time series data.
1244
+ y_pred (np.array):
1245
+ The predicted binary labels for the time series data.
1246
+ k (int):
1247
+ Half-window size for tolerance around each true anomaly point. A prediction within k
1248
+ time steps of a true point counts toward detection.
1106
1249
 
1107
1250
  Returns:
1108
- float: The total detected in range score.
1251
+ float: The total detected in range score.
1109
1252
  """
1110
1253
  if np.sum(y_pred) == 0:
1111
1254
  return 0
@@ -1121,26 +1264,35 @@ def detection_accuracy_in_range(y_true: np.array, y_pred: np.array, k: int):
1121
1264
 
1122
1265
  This metric measures the proportion of predicted anomaly events that correspond to true anomalies.
1123
1266
  It is defined as:
1124
- DAIR = (EM + DA) / (EM + DA + FA)
1267
+
1268
+ .. math::
1269
+ \\text{DAIR} = \\frac{EM + DA}{EM + DA + FA}
1125
1270
 
1126
1271
  Where:
1127
- EM (Exact Match) = number of predicted anomaly segments that exactly match a true anomaly segment.
1128
- DA (Detected Anomaly)= number of true anomaly points not exactly matched where at least one prediction falls
1129
- within a window [i-k, i+k] around the true point index i or within the true segment range.
1130
- FA (False Anomaly) = number of predicted anomaly segments that do not overlap any true anomaly segment
1131
- even within a k-step tolerance window around true points.
1272
+
1273
+ - EM (Exact Match):
1274
+ Number of predicted anomaly segments that exactly match a true anomaly segment.
1275
+ - DA (Detected Anomaly):
1276
+ Number of true anomaly points not exactly matched where at least one prediction falls
1277
+ within a window [i-k, i+k] around the true point index i or within the true segment range.
1278
+ - FA (False Anomaly):
1279
+ Number of predicted anomaly segments that do not overlap any true anomaly segment
1280
+ even within a k-step tolerance window around true points.
1132
1281
 
1133
1282
  For more information, see the original paper:
1134
1283
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1135
1284
 
1136
1285
  Parameters:
1137
- y_true (np.array): The ground truth binary labels for the time series data.
1138
- y_pred (np.array): The predicted binary labels for the time series data.
1139
- k (int): Half-window size for tolerance around each true anomaly point. A prediction within k
1140
- time steps of a true point counts toward detection.
1286
+ y_true (np.array):
1287
+ The ground truth binary labels for the time series data.
1288
+ y_pred (np.array):
1289
+ The predicted binary labels for the time series data.
1290
+ k (int):
1291
+ Half-window size for tolerance around each true anomaly point. A prediction within k
1292
+ time steps of a true point counts toward detection.
1141
1293
 
1142
1294
  Returns:
1143
- float: The total detected in range.
1295
+ float: The detection accuracy in range score.
1144
1296
  """
1145
1297
  if np.sum(y_pred) == 0:
1146
1298
  return 0
@@ -1165,24 +1317,35 @@ def weighted_detection_difference(y_true: np.array, y_pred: np.array, k: int):
1165
1317
  not overlap any true anomaly segment (within the same extension).
1166
1318
 
1167
1319
  The final score is:
1168
- WDD = WS - WF*FA
1320
+
1321
+ .. math::
1322
+ \\text{WDD} = \\text{WS} - \\text{WF} \\cdot \\text{FA}
1169
1323
 
1170
1324
  Where:
1171
- WS = Σ weights_true_predictions
1172
- WF = Σ weights_false_positives
1173
- FA (False Anomaly) = number of predicted anomaly segments that do not overlap any true anomaly segment
1174
- even within a k-step tolerance window around true points.
1325
+
1326
+ - WS:
1327
+ Sum of Gaussian weights for all predicted anomaly points that fall
1328
+ within any true anomaly segment (extended by delta time steps at the ends).
1329
+ - WF:
1330
+ Sum of Gaussian weights for all predicted anomaly points that do not
1331
+ overlap any true anomaly segment (within the same extension).
1332
+ - FA (False Anomaly):
1333
+ Number of predicted anomaly segments that do not overlap any true anomaly segment
1334
+ even within a k-step tolerance window around true points.
1175
1335
 
1176
1336
  For more information, see the original paper:
1177
1337
  https://acta.sapientia.ro/content/docs/evaluation-metrics-for-anomaly-detection.pdf
1178
1338
 
1179
1339
  Parameters:
1180
- y_true (np.array): The ground truth binary labels for the time series data.
1181
- y_pred (np.array): The predicted binary labels for the time series data.
1182
- k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
1340
+ y_true (np.array):
1341
+ The ground truth binary labels for the time series data.
1342
+ y_pred (np.array):
1343
+ The predicted binary labels for the time series data.
1344
+ k (int):
1345
+ The maximum number of time steps within which an anomaly must be predicted to be considered detected.
1183
1346
 
1184
1347
  Returns:
1185
- float: The weighted detection difference.
1348
+ float: The weighted detection difference.
1186
1349
  """
1187
1350
  if np.sum(y_pred) == 0:
1188
1351
  return 0
@@ -1234,13 +1397,17 @@ def binary_pate(y_true: np.array, y_pred: np.array, early: int, delay: int):
1234
1397
  https://arxiv.org/abs/2405.12096
1235
1398
 
1236
1399
  Parameters:
1237
- y_true (np.array): The ground truth binary labels for the time series data.
1238
- y_pred (np.array): The predicted binary labels for the time series data.
1239
- early (int): The maximum number of time steps before an anomaly must be predicted to be considered early.
1240
- delay (int): The maximum number of time steps after an anomaly must be predicted to be considered delayed.
1400
+ y_true (np.array):
1401
+ The ground truth binary labels for the time series data.
1402
+ y_pred (np.array):
1403
+ The predicted binary labels for the time series data.
1404
+ early (int):
1405
+ The maximum number of time steps before an anomaly must be predicted to be considered early.
1406
+ delay (int):
1407
+ The maximum number of time steps after an anomaly must be predicted to be considered delayed.
1241
1408
 
1242
1409
  Returns:
1243
- float: The PATE score.
1410
+ float: The PATE score.
1244
1411
  """
1245
1412
 
1246
1413
  return PATE(y_true, y_pred, early, delay, binary_scores=True)
@@ -1253,19 +1420,24 @@ def mean_time_to_detect(y_true: np.array, y_pred: np.array):
1253
1420
  For each ground-truth anomaly segment, let i be the index where the segment starts,
1254
1421
  and let j ≥ i be the first index within that segment where the model predicts an anomaly.
1255
1422
  The detection delay for that event is defined as:
1256
- Δ = j - i
1423
+
1424
+ .. math::
1425
+ \Delta = j - i
1426
+
1257
1427
  The MTTD is the mean of all such Δ values, one per true anomaly segment, and expresses
1258
1428
  the average number of time steps between the true onset of an anomaly and its first detection.
1259
1429
 
1260
1430
  Parameters:
1261
- y_true (np.array): The ground truth binary labels for the time series data.
1262
- y_pred (np.array): The predicted binary labels for the time series data.
1431
+ y_true (np.array):
1432
+ The ground truth binary labels for the time series data.
1433
+ y_pred (np.array):
1434
+ The predicted binary labels for the time series data.
1263
1435
 
1264
1436
  For more information, see the original paper:
1265
1437
  https://arxiv.org/pdf/2211.05244
1266
1438
 
1267
1439
  Returns:
1268
- float: The mean time to detect.
1440
+ float: The mean time to detect.
1269
1441
  """
1270
1442
 
1271
1443
  b = Binary_detection(len(y_true),y_true,y_pred)