A Z-score can miss an overheating bearing. A median will not
Before training any model, a median-and-MAD baseline gives the client's engineers a rule they can check and tune for themselves once you have left.
In brief
- A Z-score based on mean and standard deviation is easily skewed by the very outlier it should catch, especially in small samples; median and MAD are far more robust.
- A baseline that runs in production needs four more things: an offset window, a minimum sample count, a static fallback threshold and a way to handle seasonality.
- An anomaly is not necessarily an incident. Add context before alerting the client.
In your first week at a client’s plant, the operations shift lead sends over a CSV of bearing temperatures from twenty pumps and asks: “Can your team do AI anomaly detection?” The temptation is to open a notebook and reach straight for an isolation forest or an autoencoder.
Hold back. What the client needs in the first month is a rule their engineers can understand, check and adjust when you are not in the room. A statistical baseline meets all three conditions. It also becomes the yardstick that later proves whether a more complex model is genuinely better.
For an FDE this is a highly practical skill. Most IoT, infrastructure monitoring and data quality projects start with the question “is this normal?”. Whoever can answer it with ten lines of code and an explainable threshold earns the client’s trust sooner.
Why do mean and standard deviation mislead you?
When Grafana Labs built anomaly detection on Prometheus, it started with the formula almost everyone reaches for first: a band of the mean plus or minus a few standard deviations, a variant of the Z-score. That works when data is plentiful and clean. Client sensor data rarely is.
The NIST statistics handbook warns that the ordinary Z-score can be misleading, particularly with small samples, because the largest Z value attainable is bounded by the sample size.
The reason is that an outlier inflates both the mean and the standard deviation, so it masks itself. Instead, NIST cites the recommendation of Iglewicz and Hoaglin: use the modified Z-score, computed from the median and the MAD (the median of the absolute deviations from the median).
The formula is M = 0.6745 × (x − median) / MAD. Iglewicz and Hoaglin recommend labeling any point with |M| greater than 3.5 as a “potential outlier”.
Working it by hand with ten readings
Say a bearing reports ten temperature readings (°C): 61, 62, 60, 63, 61, 62, 61, 60, 62, 85. Anyone looking can see that 85 is a problem. The question is whether the formula sees it too.
Start with the ordinary Z-score. The sum is 637, so the mean is 63.7. The sample standard deviation is about 7.54, so the Z of the reading 85 is 21.3 / 7.54 ≈ 2.82. Against the familiar cutoff of 3, the point slips through. It could never exceed 3 anyway: with 10 points, the maximum Z is only about 2.85, exactly the limit NIST describes.
Now try median and MAD. Sorted, the values are 60, 60, 61, 61, 61, 62, 62, 62, 63, 85, so the median is 61.5. The absolute deviations are six values of 0.5, three of 1.5 and one of 23.5, so the MAD is 0.5.
The reading 85 has M = 0.6745 × 23.5 / 0.5 ≈ 31.7, nine times the 3.5 cutoff. The reading 63 has M ≈ 2.02, so it is not flagged, and rightly so.
When the client asks why you chose the median, one point is enough: a single outlier cannot drag the median anywhere or inflate the MAD, so it cannot hide itself the way it does with mean and standard deviation.
From a hand calculation to a baseline that runs in production
A calculation on ten points is not yet a system. Run it continuously and you hit four problems. OneUptime describes the first three in its article on dynamic thresholds; the fourth is one Grafana had to handle separately when building baselines for Prometheus.
First, an incident leaks into its own baseline. OneUptime suggests offsetting the baseline window by one hour, since the most recent hour is the period most likely to contain an incident in progress.
With a seven-day window, a few dozen minutes of heating will barely move the median. But if you shrink the window to a few hours to react faster, incident data will fill most of it and can push the median up with it, so the flag fires late or not at all.
Second, cold start. The same source notes that a new host with one hour of data cannot have a reliable seven-day baseline. A newly installed or replaced sensor is no different, so set a minimum sample count before trusting the baseline.
The third problem follows from the second: what triggers an alert for a device without enough samples? OneUptime advises keeping a static fallback threshold for resources with no baseline history. In a factory, the natural candidate is the maximum temperature in the manufacturer’s datasheet.
Fourth, seasonality. Grafana says it was the last case it had to handle separately, because a plain sliding band was not enough.
It chose a one-hour window after experiments showed this was the right balance for short-term anomaly detection. To smooth the band, it used 26 hours instead of the usual 24, giving users some extra margin.
In a factory, seasonality usually follows shifts. Pumps run under heavy load on the day shift and rest at night, so a simple approach is to compute a separate baseline for each time slot of the day. Asking the shift lead for the production schedule before looking at the data will save you a week.
import pandas as pd
def flag_anomalies(df, window="7D", offset="1h",
min_points=500, static_max=90.0, cutoff=3.5):
s = df.set_index("ts")["temp_c"].sort_index()
hist = s.shift(freq=offset) # baseline does not see the latest hour
med = hist.rolling(window, min_periods=min_points).median()
mad = (hist - med).abs().rolling(window, min_periods=min_points).median()
med = med.reindex(s.index, method="ffill")
mad = mad.reindex(s.index, method="ffill")
m = 0.6745 * (s - med) / mad.where(mad > 0)
has_baseline = med.notna() & mad.gt(0)
flag = (m.abs() > cutoff) & has_baseline
fallback = (~has_baseline) & (s > static_max) # cold start
return pd.DataFrame({"temp_c": s, "m": m, "flag": flag | fallback})
A rolling-window MAD is only an approximation here, but it is good enough for a baseline. Note the mad > 0 line: sensors that round to whole degrees often produce a MAD of 0 during hours when the machine is idle, and dividing by zero would flag the entire morning.
The flag is up. Should you call the client?
Grafana states plainly that an anomaly on its own is not a strong enough signal that something is broken. A bearing at 85°C may mean a failing bearing, but it may also mean a scheduled pipeline flush just ran.
So an anomaly flag is only an input; an alert needs context: is the machine running, are neighboring sensors rising too, and has the flag persisted for more than a few cycles?
In meetings with the client, say “the system sees a point far from normal” rather than “the system detected a fault”. The difference sounds small, but it decides whether the client mutes your alert channel after week two.
The most common mistakes
The most common is using mean and standard deviation on a handful of points, then concluding “the data has no anomalies”. The second is letting the baseline window reach the present moment, so an incident skews its own baseline. Next comes forgetting cold start, so every sensor replacement triggers a burst of false alerts.
The last group of mistakes is human. Some people ignore the shift schedule and flag every machine start at 6 a.m. Others push every flag straight to the client as a message. Both destroy trust faster than any bad model.
Turning this skill into an interview advantage
When reading FDE job descriptions, watch for phrases such as “industrial data”, “observability”, “time series” or “IoT”. These are roles where baseline skills get used, so it is worth having a concrete example ready to tell in interviews.
On your CV, do not write “built an anomaly detection model”. Spell out which threshold you chose and why, how you handled cold start and seasonality, and how you measured false alerts before and after.
Machine learning models still have their place, but only once the baseline is running and you have numbers to compare against. A model that cannot beat a median and a MAD is not ready for the client’s plant.
Was this article useful?
Thanks for the feedback!