fix(examples): correct quantile indices, variable shadowing, and test design in anomaly + covariates examples

Anomaly detection fixes:
- Fix critical quantile index bug: index 0 is mean not q10; correct indices are q10=1, q20=2, q80=8, q90=9
- Redesign test: use all 36 months as context, inject 3 synthetic anomalies into future
- Result: 3 CRITICAL detected (was 11/12 — caused by test-set leakage + wrong indices)
- Update severity labels: CRITICAL = outside 80% PI, WARNING = outside 60% PI

Covariates fixes:
- Fix variable-shadowing bug: inner dict comprehension overwrote outer loop store_id
  causing all stores to get identical covariate arrays (store_A's price for everyone)
- Give each store a distinct price baseline (premium $12, standard $10, discount $7.50)
- Trim CONTEXT_LEN from 48 → 24 weeks; CSV now 108 rows (was 180)
- Add NOTE ON REAL DATA comment: temp file pattern for large external datasets

Both scripts regenerated with clean outputs.
This commit is contained in:
Clayton Young
2026-02-21 18:27:45 -05:00
parent 0d98fa353c
commit 509190118f
7 changed files with 612 additions and 697 deletions

View File

@@ -1,160 +1,152 @@
{
"method": "quantile_intervals",
"description": "Anomaly detection using TimesFM quantile forecasts as prediction intervals",
"thresholds": {
"warning": "Outside 80% CI (q20-q80)",
"critical": "Outside 90% CI (q10-q90)"
"method": "quantile_prediction_intervals",
"description": "Anomaly detection via TimesFM quantile forecasts. 80% PI = q10\u2013q90 (CRITICAL if violated). 60% PI = q20\u2013q80 (WARNING if violated).",
"context": "36 months of real NOAA temperature anomaly data (2022-2024)",
"future": "12 synthetic months with 3 injected anomalies",
"quantile_indices": {
"q10": 1,
"q20": 2,
"q80": 8,
"q90": 9
},
"anomalies": [
{
"month": "2024-01",
"actual": 1.9520000219345093,
"forecast": 1.1204800605773926,
"lower_80": 0.9561834335327148,
"upper_80": 1.19773530960083,
"lower_90": 1.1319338083267212,
"upper_90": 1.2482070922851562,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-02",
"actual": 1.350000023841858,
"forecast": 1.0831129550933838,
"lower_80": 0.9061079621315002,
"upper_80": 1.1693586111068726,
"lower_90": 1.1058242321014404,
"upper_90": 1.229236364364624,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-03",
"actual": 1.340000033378601,
"forecast": 1.0525826215744019,
"lower_80": 0.8687788844108582,
"upper_80": 1.14640212059021,
"lower_90": 1.0804548263549805,
"upper_90": 1.210077166557312,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-04",
"actual": 1.2599999904632568,
"forecast": 1.0186809301376343,
"lower_80": 0.8394415378570557,
"upper_80": 1.11386239528656,
"lower_90": 1.0469233989715576,
"upper_90": 1.18027925491333,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-05",
"actual": 1.149999976158142,
"forecast": 0.996323823928833,
"lower_80": 0.8218992948532104,
"upper_80": 1.082446813583374,
"lower_90": 1.0246795415878296,
"upper_90": 1.1515717506408691,
"severity": "WARNING",
"threshold": "80% CI",
"color": "orange"
},
{
"month": "2024-06",
"actual": 1.2000000476837158,
"forecast": 0.9761021733283997,
"lower_80": 0.8107370138168335,
"upper_80": 1.0650819540023804,
"lower_90": 1.0055618286132812,
"upper_90": 1.1297614574432373,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-07",
"actual": 1.2400000095367432,
"forecast": 0.966797411441803,
"lower_80": 0.8105956315994263,
"upper_80": 1.05680513381958,
"lower_90": 0.999349057674408,
"upper_90": 1.1205626726150513,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-08",
"actual": 2.0799999237060547,
"forecast": 0.9621630311012268,
"lower_80": 0.8031740784645081,
"upper_80": 1.0481219291687012,
"lower_90": 0.9949856996536255,
"upper_90": 1.1177691221237183,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-09",
"actual": 0.7680000066757202,
"forecast": 0.950423002243042,
"lower_80": 0.8004634380340576,
"upper_80": 1.0429224967956543,
"lower_90": 0.9896860718727112,
"upper_90": 1.112573504447937,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-10",
"actual": 1.2699999809265137,
"forecast": 0.9326475262641907,
"lower_80": 0.7854968309402466,
"upper_80": 1.024938702583313,
"lower_90": 0.9742559194564819,
"upper_90": 1.0930581092834473,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-11",
"actual": 1.2200000286102295,
"forecast": 0.9303779602050781,
"lower_80": 0.7851479053497314,
"upper_80": 1.0191327333450317,
"lower_90": 0.9675081968307495,
"upper_90": 1.084266185760498,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
},
{
"month": "2024-12",
"actual": 1.2000000476837158,
"forecast": 0.9362010955810547,
"lower_80": 0.7882705330848694,
"upper_80": 1.028489589691162,
"lower_90": 0.9734180569648743,
"upper_90": 1.0912758111953735,
"severity": "CRITICAL",
"threshold": "90% CI",
"color": "red"
}
],
"summary": {
"total_points": 12,
"critical": 11,
"total": 12,
"critical": 3,
"warning": 1,
"normal": 0
}
"normal": 8
},
"detections": [
{
"month": "2025-01",
"actual": 1.2559,
"forecast": 1.2593,
"lower_60pi": 1.1881,
"upper_60pi": 1.324,
"lower_80pi": 1.1407,
"upper_80pi": 1.3679,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-02",
"actual": 1.2372,
"forecast": 1.2857,
"lower_60pi": 1.1961,
"upper_60pi": 1.3751,
"lower_80pi": 1.1406,
"upper_80pi": 1.4254,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-03",
"actual": 1.8017,
"forecast": 1.295,
"lower_60pi": 1.1876,
"upper_60pi": 1.4035,
"lower_80pi": 1.1269,
"upper_80pi": 1.4643,
"severity": "CRITICAL",
"injected": true
},
{
"month": "2025-04",
"actual": 1.2648,
"forecast": 1.2208,
"lower_60pi": 1.1042,
"upper_60pi": 1.331,
"lower_80pi": 1.0353,
"upper_80pi": 1.4017,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-05",
"actual": 1.2245,
"forecast": 1.1703,
"lower_60pi": 1.0431,
"upper_60pi": 1.2892,
"lower_80pi": 0.9691,
"upper_80pi": 1.3632,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-06",
"actual": 1.2335,
"forecast": 1.1456,
"lower_60pi": 1.0111,
"upper_60pi": 1.2703,
"lower_80pi": 0.942,
"upper_80pi": 1.3454,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-07",
"actual": 1.2534,
"forecast": 1.1702,
"lower_60pi": 1.0348,
"upper_60pi": 1.2998,
"lower_80pi": 0.9504,
"upper_80pi": 1.3807,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-08",
"actual": 0.7517,
"forecast": 1.2027,
"lower_60pi": 1.0594,
"upper_60pi": 1.3408,
"lower_80pi": 0.9709,
"upper_80pi": 1.4195,
"severity": "CRITICAL",
"injected": true
},
{
"month": "2025-09",
"actual": 1.2514,
"forecast": 1.191,
"lower_60pi": 1.0404,
"upper_60pi": 1.3355,
"lower_80pi": 0.9594,
"upper_80pi": 1.417,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-10",
"actual": 1.2398,
"forecast": 1.1491,
"lower_60pi": 0.9953,
"upper_60pi": 1.2869,
"lower_80pi": 0.9079,
"upper_80pi": 1.3775,
"severity": "NORMAL",
"injected": false
},
{
"month": "2025-11",
"actual": 1.7317,
"forecast": 1.0805,
"lower_60pi": 0.926,
"upper_60pi": 1.2284,
"lower_80pi": 0.8361,
"upper_80pi": 1.3122,
"severity": "CRITICAL",
"injected": true
},
{
"month": "2025-12",
"actual": 1.2625,
"forecast": 1.0613,
"lower_60pi": 0.8952,
"upper_60pi": 1.2169,
"lower_80pi": 0.8022,
"upper_80pi": 1.296,
"severity": "WARNING",
"injected": false
}
]
}