Finding Unusual Readings Without Letting Them Set the Reference
A faulty sensor can pull an ordinary mean toward itself. Use a robust centre and spread, then decide what a large deviation actually warrants.
13 sections · 4 questions · 2 coding challenges
The arrow keys move between sections too.
Estimating a Reference Despite Contamination
BOTIE
Our median and median-absolute-deviation score is a simple robust teaching example, not Huber’s specific estimator. It lets us inspect how the reference changes when an extreme reading is present.
Sources for this history
Should a Broken Sensor Help Define What Looks Normal?
Most of Botie’s pressure readings are close together, but one is much larger. If that reading helps determine the average and spread used to judge it, it can make itself look less unusual.
Can we choose a reference that one extreme reading has less power to move? The median gives us a centre, and the median absolute deviation gives us a measure of spread. We can use them to judge how unusual a reading is relative to the reference records. That score will not tell us why the reading is unusual; a fault is one possibility to investigate.
Reference and incoming readings are separate illustrative samples.
Step 1 of 3
Find a typical centre
A median uses order rather than the size of an extreme tail.
Median: 10
Reference readings
Values: 8, 9, 10, 11, 100
Step 2 of 3
Measure typical distance from it
Take absolute deviations and their median.
| Reading | Distance from median 10 |
|---|---|
| 8 | 2 |
| 9 | 1 |
| 10 | 0 |
| 11 | 1 |
| 100 | 90 |
8
- Distance from median 10
- 2
9
- Distance from median 10
- 1
10
- Distance from median 10
- 0
11
- Distance from median 10
- 1
100
- Distance from median 10
- 90
Step 3 of 3
Score a new reading
Compare its distance with that fixed reference spread.
Reference median: 10 Reference MAD: 1 New reading: 13 Raw MAD score = |13 − 10| / 1 = 3
The reference should resist the extremes it is meant to reveal
The reference is fitted separately and the spread resists a small number of extreme values.
Can an unusual reading pull the reference towards itself?
Fit the centre and spread from the reference records before scoring incoming values. Introduce an extreme reference value and compare its influence on those summaries.
BOTIE
Robust deviations of the incoming readings
- 00
- 13
Follow the operations
1. Incoming readings
- 010
- 113
2. Reference readings
- 08
- 19
- 210
- 311
- 4100
3. Median of a list
10
4. Subtract
- 00
- 13
5. Absolute value
- 00
- 13
6. Subtract
- 0-2
- 1-1
- 20
- 31
- 490
7. Absolute value
- 02
- 11
- 20
- 31
- 490
8. Median of a list
1
9. Divide
- 00
- 13
10. Result
- 00
- 13
Requires positive MAD. A large score indicates unusualness, not a diagnosed fault.
1. Define the Reference Population
Choose records that represent the operating condition in which the detector will be used. A pressure normal during startup may be unusual during steady operation. Split reference fitting from later threshold selection and final testing.
An anomaly score measures disagreement with the reference. It does not tell us whether an observation is a sensor fault, a genuine event, or a new operating regime.
The same numerical deviation can have different meanings in different conditions.
2. Calculate Centre and Spread Separately
The median is a central ordered value. The median absolute deviation, or MAD, is the median of distances from that centre. Neither step requires squaring a large deviation.
Reference = [8,9,10,11,100]
Median = 10
Absolute deviations = [2,1,0,1,90]
MAD = 1The centre and spread should be retained for scoring later records.
3. Make the Score’s Convention Explicit
Our raw score divides absolute distance from the median by MAD. A commonly used normal-consistency scaling multiplies MAD by approximately 1.4826; that produces a different numerical score and threshold.
We use the raw convention throughout this lesson. If MAD is zero, many reference values coincide. Do not silently divide by zero or insert an unexplained epsilon: define a fallback scale, exact-match policy, or a different model.
Raw score for 13 = abs(13-10) / 1 = 3A threshold has meaning only together with the score definition and reference data.
Measure spread around the middle reading
Begin with an orderly reference sample. Find its middle value, measure every absolute departure from it, then take the middle departure.
import numpy as np
reference = np.array([4., 5., 6., 7., 8.])
centre = np.median(reference)
departures = np.abs(reference - centre)
print(float(centre))
print(departures.tolist())
print(float(np.median(departures)))6.0
[2.0, 1.0, 0.0, 1.0, 2.0]
1.0The MAD is one in the original measurement units. Practice adds an extreme reference value and then scores a separate incoming reading.
4. Turn a Score Into a Reviewed Alert
Select a threshold using representative validation records and the cost of missed faults versus unnecessary investigations. Evaluate precision and recall when reliable labels exist, and inspect the burden of alerts per day.
Updating the reference automatically with every incoming reading can gradually absorb a developing fault. Decide which records are eligible to update the baseline and keep versions for comparison.
Botie’s workshop fits a median reference, then scores separate new readings.
5. A Zero MAD Is a Missing Scale, Not an Infinite Certainty
Several repeated pressure readings can give a median absolute deviation of zero. Dividing by it is undefined. Adding a tiny constant merely chooses an extremely sensitive scale, often without a physical justification.
Declare a fallback based on measurement resolution or another fitted scale, or report that this score cannot be formed. Keep the median and scale fitted on the reference period when scoring new batches. Re-estimating them on each incoming batch can hide a whole shifted batch.
6. Ordinary Coordinates Can Form an Unusual Pair
Pressure and flow may usually rise together. A new observation can be ordinary on both individual ranges while contradicting their relationship. Separate univariate scores will miss that kind of anomaly.
A residual from an explicitly fitted relationship provides one way to inspect it. This example supplies a simple reference relationship so that we can distinguish marginal range checks from joint behaviour.
Compare ranges with a relationship residual
The reference points follow equal pressure and flow in these illustrative units.
import numpy as np
reference = np.array([[1.,1.],[2.,2.],[3.,3.]])
new = np.array([1.,3.])
inside = (new >= reference.min(axis=0)) & (new <= reference.max(axis=0))
print(inside.tolist())
print(float(new[1]-new[0]))[True, True]
2.0Both coordinates lie inside their historical ranges, but their difference is unlike the reference pairs. The residual still needs its own scale and threshold.
7. Choose a Threshold for an Investigation Process
An unusually large score is a reason to investigate, not a fault label. Choose the inspection threshold or review budget using a suitable validation period and whatever outcome labels are available.
Record unresolved cases separately from confirmed healthy cases. If only high-scoring records are inspected, the labelled subset cannot establish how many low-scoring faults were missed. A sampling plan for some lower-scoring records can help assess that blind spot.
Check yourself
Questions on Parts 1 to 7
Question 1 of 4
What is its median?
Use the information below.
Reference [8,9,10,11,100].Try it with Botie
Build with Botie
Write and run Python