herd-alert-rule
Lower the false-alarm rate of a dairy-herd illness alert rule: false alarms per 100 healthy animal-weeks, measured at a fixed 80% early recall. Read README.md for the data and the rules of the scorer, then improve risk() in alert_rule.py. Ideas must generalize to a different herd on a different farm.
Score over time
Accepted changes (5)
#1 · Replace the 7-day mean/stdev rumination z-score with a 14-day median/MAD baseline that skips partial-coverage days (<0.6) and sums clipped z-scores of rumination, activity and lying.
The dev score went from 55.94 to 13.06 false alarms per 100 healthy animal-weeks, at early recall 0.803 over 71 episodes. I didn't run an ablation, so I can't say how much comes from the robust baseline, the coverage filter or the extra signals. I haven't checked held-out data.
Diff
diff --git a/alert_rule.py b/alert_rule.pyindex 9c6ea7a..232cbbb 100644--- a/alert_rule.py+++ b/alert_rule.py@@ -1,26 +1,27 @@-"""The alert rule Hillclimb is allowed to change.+"""Robust per-animal baseline, multi-signal, coverage-aware.""" -risk(history, today, herd) -> float. Higher means more likely to be getting sick.-- history: this animal's previous days, oldest first (at most 60), each a dict- with keys animal, day, weekday, temp_c, activity, rumination, lying, coverage.- Sensor values are None on days the collar reported nothing.- today: the same dict for today.- herd: today's dicts for every animal in the herd (including this one).+import statistics -Baseline: how far today's rumination has dropped below this animal's mean-over the previous 7 days, in units of its standard deviation.-""" -import statistics+def _z(history, today, key, sign, n=14):+ v = today[key]+ if v is None or today["coverage"] < 0.6:+ return None+ vals = [h[key] for h in history[-n:] if h[key] is not None and h["coverage"] >= 0.6]+ if len(vals) < 4:+ return None+ med = statistics.median(vals)+ mad = statistics.median([abs(x - med) for x in vals]) * 1.4826+ sd = max(mad, 0.3 * (statistics.pstdev(vals) or 1.0), 1e-6)+ return sign * (med - v) / sd def risk(history, today, herd): - if today["rumination"] is None:- return 0.0- recent = [h["rumination"] for h in history[-7:] if h["rumination"] is not None]- if len(recent) < 3:- return 0.0- mean = statistics.fmean(recent)- sd = statistics.stdev(recent) or 1.0- return (mean - today["rumination"]) / sd+ zs = [_z(history, today, "rumination", 1), _z(history, today, "activity", 1),+ _z(history, today, "lying", -1)]+ w = [1.0, 0.5, 0.5]+ tot = 0.0+ for z, wi in zip(zs, w):+ if z is not None:+ tot += wi * max(min(z, 6.0), -3.0)+ return tot
#3 · Add 0.6 times yesterday's multi-signal score (computed from history before yesterday) to today's score, so persistent multi-day deviations count more than one-day blips.
Dev score went from 13.062 to 9.039 false alarms per 100 healthy animal-weeks, at early recall 0.803 over 71 episodes. That is an improvement of about 4, well above the 0.25 threshold. I tried only the 0.6 weight, so it isn't tuned, and I haven't checked held-out data.
Diff
diff --git a/alert_rule.py b/alert_rule.pyindex 232cbbb..dae953f 100644--- a/alert_rule.py+++ b/alert_rule.py@@ -17,6 +17,13 @@ def _z(history, today, key, sign, n=14): def risk(history, today, herd): + t = _score(history, today)+ if len(history) >= 5:+ t += 0.6 * _score(history[:-1], history[-1])+ return t+++def _score(history, today): zs = [_z(history, today, "rumination", 1), _z(history, today, "activity", 1), _z(history, today, "lying", -1)] w = [1.0, 0.5, 0.5]
#5 · Compute each animal's median/MAD baseline from days 3–16 back, skipping the most recent 2 days, so the onset of illness doesn't contaminate its own baseline.
Dev score fell from 9.0393 to 6.2944 false alarms per 100 healthy animal-weeks, at early recall 0.8028 over 71 episodes. That is a drop of about 2.7, above the 0.25 threshold. I tried only a 2-day gap and didn't tune it. I haven't checked held-out data.
Diff
diff --git a/alert_rule.py b/alert_rule.pyindex dae953f..a719ab7 100644--- a/alert_rule.py+++ b/alert_rule.py@@ -7,7 +7,7 @@ def _z(history, today, key, sign, n=14): v = today[key] if v is None or today["coverage"] < 0.6: return None - vals = [h[key] for h in history[-n:] if h[key] is not None and h["coverage"] >= 0.6]+ vals = [h[key] for h in history[-n - 2:-2] if h[key] is not None and h["coverage"] >= 0.6] if len(vals) < 4: return None med = statistics.median(vals)
#6 · Subtract the herd-wide median of today's per-animal multi-signal score from each animal's score, and do the same for yesterday's term. Weather and management shifts that hit the whole herd then cancel out, and only animal-specific deviations raise an alert.
The dev score went from 9.0393 to 2.461 false alarms per 100 healthy animal-weeks (52 false alarms), with early recall unchanged at 0.8028 over 71 episodes. The herd median uses module-level per-animal history, because `herd` only holds today's rows. It is only applied when at least 10 animals have enough history, so it falls back to no shift otherwise. I did not test it on held-out data, and I did not run an ablation on the 0.6 yesterday weight.
Diff
diff --git a/alert_rule.py b/alert_rule.pyindex a719ab7..14c7251 100644--- a/alert_rule.py+++ b/alert_rule.py@@ -16,10 +16,30 @@ def _z(history, today, key, sign, n=14): return sign * (med - v) / sd +_H = {}+_SHIFT = {}+++def _shift(herd, day):+ if day in _SHIFT:+ return _SHIFT[day]+ vals = []+ for r in herd:+ h = _H.get(r["animal"], [])+ if len(h) >= 5:+ vals.append(_score(h, r))+ _SHIFT[day] = statistics.median(vals) if len(vals) >= 10 else 0.0+ for r in herd:+ _H.setdefault(r["animal"], []).append(r)+ return _SHIFT[day]++ def risk(history, today, herd): - t = _score(history, today)+ d = today["day"]+ sh = _shift(herd, d)+ t = _score(history, today) - sh if len(history) >= 5: - t += 0.6 * _score(history[:-1], history[-1])+ t += 0.6 * (_score(history[:-1], history[-1]) - _SHIFT.get(d - 1, 0.0)) return t
#9 · Widen each animal's median/MAD baseline window from 14 to 21 days (still skipping the latest 2 days and partial-coverage days), so the per-animal baseline is steadier and less affected by missing days.
Dev score went from 1.4198 to 1.0412 false alarms per 100 healthy animal-weeks (22 false alarms), with early recall unchanged at 0.8028 over 71 episodes. The gain is about 0.38, above the 0.25 threshold. I tried only n=21 and did not tune it, and I haven't checked held-out data.
Diff
diff --git a/alert_rule.py b/alert_rule.pyindex 14c7251..b77856a 100644--- a/alert_rule.py+++ b/alert_rule.py@@ -3,7 +3,7 @@ import statistics -def _z(history, today, key, sign, n=14):+def _z(history, today, key, sign, n=21): v = today[key] if v is None or today["coverage"] < 0.6: return None
Dev vs held-out
The held-out command ran 6 time(s), only on candidates that had already won on dev. Its scores never entered a prompt or a worktree. Policy: gate (tolerance 0). 1 dev win(s) were blocked by the gate.
Held-out improved 95.3% against 98.1% on dev: the gains carry over.
| Step | Dev | Held-out | Held-out − dev |
|---|---|---|---|
| baseline | 55.9394 | 49.7365 | -6.2029 |
| #1 | 13.062 | 18.5913 | 5.5293 |
| #3 | 9.0393 | 12.5539 | 3.5146 |
| #5 | 6.2944 | 10.1581 | 3.8637 |
| #6 | 1.4198 | 2.4916 | 1.0718 |
| #9 | 1.0412 | 2.3479 | 1.3067 |
| #7 (blocked) | 5.8211 | 12.6018 | 6.7807 |
Rejected ideas (5)
| # | Hypothesis | Dev | Why it was not kept |
|---|---|---|---|
| 2 | Subtract the herd-wide median rumination deviation from its own 7-day baseline today from each animal's rumination before scoring, so weather and management shifts hitting the whole herd don't trigger alerts. | 41.7889 | rejected: conflicts with a newer accepted change |
| 4 | Veto the risk score (return -1) on any day when activity is above the animal's own 14-day median, since illness lowers activity and an activity rise is an oestrus-like benign burst. | 12.5414 | rejected: no improvement once combined with newer best: 12.5414 vs 9.0393 |
| 7 | Add 0.35 times the multi-signal score from two days ago to the existing today + 0.6×yesterday sum, so evidence over three days counts rather than two. | 5.8211 | rejected: held-out gate: 12.6018 is worse than best held-out 10.1581 |
| 8 | Weight activity surges (activity above the animal's baseline, a sign of oestrus) at 2.0 instead of 0.5 so they subtract more from the illness score, while activity drops keep the 0.5 weight. | 6.1051 | rejected: no improvement over its parent beyond noise: 6.1051 (needed better than 6.0444) |
| 10 | Divide each animal's herd-median-adjusted score by the herd's robust day-wide spread of scores (floored at 1.0) so that days with herd-wide noise need larger animal-specific deviations to alert. | – | rejected: no change: the agent left the editable files as they were (made no edit, or reverted its own) |
Cost and time
- Active wall clock
- 1.9 min over 1 session(s)
- Agent time (summed over workers)
- 2.7 min
- Metric time
- 21s
- Agent cost
- $0.44 (reported by the backend; on a Claude subscription this is the notional API price, not a bill)
- Backend · model
- claude · claude-sonnet-5-5 · 2 worker(s)
| # | Status | Agent | Turns | Metric | Cost |
|---|---|---|---|---|---|
| 1 | accepted | 12s | 7 | 1s | $0.04 |
| 2 | rejected | 15s | 7 | 0s | $0.05 |
| 3 | accepted | 13s | 6 | 2s | $0.03 |
| 4 | rejected | 21s | 12 | 1s | $0.05 |
| 5 | accepted | 13s | 6 | 2s | $0.03 |
| 6 | accepted | 22s | 9 | 4s | $0.06 |
| 7 | rejected | 10s | 5 | 3s | $0.03 |
| 8 | rejected | 19s | 8 | 2s | $0.04 |
| 9 | accepted | 13s | 6 | 4s | $0.04 |
| 10 | rejected | 24s | 12 | 0s | $0.07 |
Setup
- Metric
- python3 score.py --split dev (min)
- Held-out
- python3 score.py --split heldout
- Editable
- alert_rule.py
- Frozen
- score.py, generate.py, README.md, data/dev_*
- Noise policy
- 1 run(s) per candidate, baseline 3 run(s) (std 0); margin max(0.25, 2σ)
- Branch
- hillclimb/best @ 737842152560 (base 0ea67dfe78ff)