Sub topic 9
Analytics: spotting unusual metric changes
This chapter explains how analytics platforms flag a metric that moves more than normal. It assumes you know what a dashboard and a time series are, and that you have a rough idea of what a baseline is.
What the feature automates
Without anomaly detection, someone has to look at a dashboard and decide whether a number looks wrong. Anomaly detection (finding values that differ from what is expected) does that check on every metric, every day, and sends an alert when a value is unusual.
This is a predictive or statistical feature, not a generative one. It does not write text. It compares new data with a pattern learned from earlier data.
The working model
Most tools follow the same four steps.
- Collect a history of the metric, such as daily signups.
- Build a baseline: the range of values you would normally expect.
- Measure how far the newest value is from that baseline.
- Alert if the distance passes a threshold.
A minimal example
The simplest baseline is the mean and standard deviation of earlier values. The standard deviation measures how much the values usually vary. A z-score says how many standard deviations the newest value sits from the mean.
from statistics import mean, stdev
daily_signups = [102, 98, 105, 99, 101, 103, 97, 160]
baseline = daily_signups[:-1]
latest = daily_signups[-1]
z = (latest - mean(baseline)) / stdev(baseline)
print(f"z-score: {z:.1f}")
if abs(z) > 3:
print("Alert: latest value is unusual compared with the baseline")The baseline values stay between 97 and 105, so a value of 160 is far outside the usual spread. The z-score is large and the alert fires. Note that the code excludes the latest value from the baseline. If you included it, the outlier would inflate the mean and spread and hide itself.
Why real products are more complex
A plain mean and spread fails on many real metrics. Commercial tools usually add the following.
| Problem | What the tool adds |
|---|---|
| Weekly cycles, such as lower traffic on weekends | Compare Saturday with earlier Saturdays, not with the whole week |
| Long-term growth or decline | Use a moving baseline or a trend model |
| Holidays and campaigns | Let users mark known events so they are not flagged |
| Many metrics at once | Raise the threshold or group alerts to limit false alarms |
Common errors
- Alert fatigue. A low threshold produces many false positives (alerts where nothing is wrong). People then ignore alerts, including real ones.
- Missed changes. A high threshold produces false negatives (real problems with no alert). A slow decline may never cross the threshold on any single day.
- Bad data read as a business change. A broken tracking tag can drop a metric to zero. The alert is correct that the number changed, but the cause is a data fault, not customer behavior.
- Expected spikes. A planned email send raises traffic. Without context, the tool calls it an anomaly.
Where the human stays involved
Detection is cheap to automate. Diagnosis is not. A person usually decides whether the change is a real event, a tracking fault, or a known campaign effect. Many platforms let you mark an alert as "expected" or "not useful", and some use that feedback to adjust sensitivity.
Exercise
A daily metric has run between 400 and 420 for weeks. Today it is 380, and the tool raises no alert. Give two possible reasons.
Show answer
The threshold may be set high enough that a drop of this size is not flagged, or the tool's baseline may already allow for variation larger than the normal range (for example, a wide spread from past outliers or a weekly pattern that makes 380 normal for today). The tool may also have been told that today is an expected event.
Exercise
Why does the example code exclude the latest value from the baseline?
Show answer
Including the outlier would raise the mean and standard deviation, which shrinks its z-score and can hide the anomaly.