Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(alerts): preserve a zero iqr multiplier (#125136)

posthog[bot]161b333b52f5
diff --git a/posthog/tasks/alerts/detectors/statistical/iqr.py b/posthog/tasks/alerts/detectors/statistical/iqr.py
index 5ec1a4442c21..3715cb784918 100644
--- a/posthog/tasks/alerts/detectors/statistical/iqr.py
+++ b/posthog/tasks/alerts/detectors/statistical/iqr.py
@@ -54,9 +54,17 @@ class IQRDetector(BaseDetector):
window: int - Rolling window size (default: 30)
"""
+ DEFAULT_MULTIPLIER = 1.5
+
+ def __init__(self, config: dict[str, Any]):
+ super().__init__(config)
+ # An explicit 0 is valid, so only a missing or None multiplier takes the default.
+ multiplier = config.get("multiplier")
+ self.multiplier: float = self.DEFAULT_MULTIPLIER if multiplier is None else multiplier
+
def detect(self, data: np.ndarray) -> DetectionResult:
threshold = self.threshold
- multiplier = self.config.get("multiplier") or 1.5
+ multiplier = self.multiplier
window = self.window
# preprocess() only ever runs a single first-difference pass when diffs_n is truthy
# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point
@@ -120,7 +128,7 @@ class IQRDetector(BaseDetector):
def detect_batch(self, data: np.ndarray) -> DetectionResult:
threshold = self.threshold
- multiplier = self.config.get("multiplier") or 1.5
+ multiplier = self.multiplier
window = self.window
# preprocess() only ever runs a single first-difference pass when diffs_n is truthy
# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point
@@ -179,7 +187,7 @@ class IQRDetector(BaseDetector):
)
def fit(self, history: np.ndarray) -> DetectorArtifact:
- multiplier = self.config.get("multiplier") or 1.5
+ multiplier = self.multiplier
window = self.window
diffs_n = 1 if self.preprocessing_config.get("diffs_n") else 0
rows = self._fit_rows(history, min_rows=window + diffs_n)
@@ -240,6 +248,6 @@ class IQRDetector(BaseDetector):
return {
"type": DetectorType.IQR.value,
"threshold": cls.DEFAULT_THRESHOLD,
- "multiplier": 1.5,
+ "multiplier": cls.DEFAULT_MULTIPLIER,
"window": 30,
}
diff --git a/posthog/tasks/alerts/detectors/test/test_detectors.py b/posthog/tasks/alerts/detectors/test/test_detectors.py
index c569a4747211..f82b5767074a 100644
--- a/posthog/tasks/alerts/detectors/test/test_detectors.py
+++ b/posthog/tasks/alerts/detectors/test/test_detectors.py
@@ -278,6 +278,26 @@ class TestStatisticalDetectors:
assert detector.detect_batch(data).all_scores[-1] is not None
detector.fit(data[:-1])
+ @parameterized.expand(
+ [
+ ("zero", {"multiplier": 0.0}, 2.25, True),
+ ("none", {"multiplier": None}, 4.5, False),
+ ("omitted", {}, 4.5, False),
+ ]
+ )
+ def test_iqr_multiplier_sets_fences(
+ self, _name: str, multiplier: dict[str, Any], upper_fence: float, is_anomaly: bool
+ ) -> None:
+ # The training window [0, 1, 2, 3] has q1=0.75, q3=2.25 and iqr=1.5, so 4.0 is outside only the zero fence.
+ data = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
+ detector = IQRDetector({"window": 4, **multiplier})
+
+ result = detector.detect(data)
+ assert result.metadata["upper_fence"] == upper_fence
+ assert result.is_anomaly is is_anomaly
+ assert detector.detect_batch(data).is_anomaly is is_anomaly
+ assert detector.fit(data[:-1]).params["upper_fence"] == upper_fence
+
@parameterized.expand(
[
(f"{name}_diffs_{diffs_n}", detector_type, detector_cls, diffs_n)