Small, lasting shifts: CUSUM and EWMA
- Lesson 11
- 40 min
- Module 3: Statistical process control and distribution drift
Where we are
Our charts catch big jumps quickly. Larkfield's refund rate rose by 0.3 points and stayed there, and the charts did not alarm for months. In this lesson, we build CUSUM and EWMA charts, which add up small differences over time, and we compare how fast each one catches the shift.
Lesson 11 of 38 · Anomaly Detection: Find What Is UnusualSmall, lasting shifts: CUSUM and EWMA
1. What "unusual" means
2. Simple baselines that are hard to beat
3. Statistical process control and distribution drift
4. Distance, density and isolation
5. Anomalies in time
6. Deep and embedding methods
7. Evaluate with few labels
8. Learn from the reviewers
9. Run a detector in production
What you will be able to do
Catch a small change that lasts, such as a refund rate that rises by 0.3 points and stays. One-day charts need months to see it, and every month costs refunds.
NotebookDo this lesson in a notebook
The notebook has every code cell, task and check of this lesson. It does CUSUM and EWMA by hand, their settings from a simulation, five charts on the same shift, and the per-agent charts. Its setup cells install the packages and download the data (less than 1 MB). About 40 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- The average run length (ARL) and the price of a chart, from Rules for patterns, and the price of each rule.
- The p-chart, z of the day and the Laney chart with its sigma_z, from Charts for rates and counts.
What you need on your computer
Nothing. The notebook runs in Colab, Kaggle or VS Code.
Tested with Python 3.12.13 in Jupyter on macOS, with pandas 3.1.1 and NumPy 2.5.4. Colab and Kaggle were not tested.
A tap that drips
Section titled “A tap that drips”A tap in your kitchen drips. One drop a minute is nothing: you do not notice it, and it dries up. Now put a bucket under the tap. After a day, the bucket is full. The drops were too small to see one by one, but together they are a lot of water.
A bucket with a small hole in the bottom is even better. The hole lets out the water from a very slow drip, so the bucket does not fill from normal wetness. Only a real drip fills it. When the bucket overflows, you call the plumber.
That is the idea of this lesson. A small, lasting change is like the drip. One day shows almost nothing, but the days add up.
The problem: a shift smaller than the noise
Section titled “The problem: a shift smaller than the noise”The planted rise of Larkfield’s refund rate is 0.3 points, from 2025-04-07. How big is that, in the sigmas of one day? The notebook computes it on the Laney scale of the last lesson. pbar and p1 are the p-bar and the phase I days of that lesson, and laney is its sigma_z:
shift = 0.003 / (np.sqrt(pbar * (1 - pbar) / p1.orders.mean()) * laney) # 0.3 points, in sigmas of one dayk = shift / 2print(f"Laney sigma_z {laney:.3f}; the 0.3-point shift is {shift:.3f} sigma; k = {k:.3f}")Laney sigma_z 1.121; the 0.3-point shift is 0.482 sigma; k = 0.241The shift is less than half a sigma a day. A Shewhart chart waits for one day beyond 3 sigmas. With a shift of 0.48 sigma, such a day is still rare. So the charts of the last lessons needed 98 days on the real data, and 40 to 78 days on average in the simulation.
CUSUM: the bucket
Section titled “CUSUM: the bucket”The CUSUM chart (cumulative sum) is the bucket. Each day, it adds the part of z that is above a small slack k. A day below the slack takes some water out, but the total never goes below 0. When the total passes an alarm level h, the chart alarms. In a formula, with S the total:
S today = max(0, S yesterday + z today − k)
CUSUM on eleven days
Section titled “CUSUM on eleven days”Take k = 0.5 and h = 4. Here are eleven days. No z is above 1.4, so no single day is anywhere near 3 sigmas. Before you read the last row, predict: does the CUSUM alarm, and on which day?
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| z | 0.2 | −0.5 | 0.9 | 1.1 | 0.4 | 1.3 | 0.8 | 1.2 | 0.6 | 1.4 | 1.0 |
| z − k | −0.3 | −1.0 | 0.4 | 0.6 | −0.1 | 0.8 | 0.3 | 0.7 | 0.1 | 0.9 | 0.5 |
| CUSUM | 0.0 | 0.0 | 0.4 | 1.0 | 0.9 | 1.7 | 2.0 | 2.7 | 2.8 | 3.7 | 4.2 |
On days 1 and 2, z − k is negative and the total stays at 0. From day 3, most days add a little. Day 5 takes 0.1 away. On day 11, the total reaches 4.2, above h = 4: the CUSUM alarms. Then it starts again from 0, as a person does after a check.
- CUSUM total
- z of the day
- Alarm level h = 4
Text description of the chart
The chart has two lines of dots over days 1 to 11. The dashed line of z values goes up and down between −0.5 and 1.4. The solid CUSUM line is at 0 on days 1 and 2. Then it climbs almost every day: 0.4, 1.0, 0.9, 1.7, 2.0, 2.7, 2.8 and 3.7. On day 11 it reaches 4.2 and crosses the dashed alarm line at 4.
The CUSUM has two sides. A high-side total adds z − k, for a rise. A low-side total adds −z − k, for a fall. The notebook’s cusum() keeps both and alarms when either one passes h.
EWMA: a memory of the recent days
Section titled “EWMA: a memory of the recent days”The EWMA chart (exponentially weighted moving average) keeps a running average with a short memory. Each day, the new average is λ (lambda) of today’s z, plus 1 − λ of yesterday’s average. With λ = 0.1, today counts 10% and the past 90%. Old days fade away slowly, like your memory of last month’s weather.
An average moves much less than one day does. So the EWMA’s limits are much narrower than 3 sigmas: L × √(λ / (2 − λ)) at most, with L = 2.6 here. On the same eleven days:
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| EWMA | 0.020 | −0.032 | 0.061 | 0.165 | 0.189 | 0.300 | 0.350 | 0.435 | 0.451 | 0.546 | 0.592 |
| Limit | 0.260 | 0.350 | 0.408 | 0.450 | 0.481 | 0.505 | 0.524 | 0.538 | 0.550 | 0.559 | 0.566 |
The limit grows in the first days, because the average has little history yet. On day 11, the EWMA (0.592) passes its limit (0.566), and the chart alarms. On these eleven days, both charts alarm on the same day.
Settings from the shift you must catch
Section titled “Settings from the shift you must catch”Each chart has two settings, and both come from your decision, not from habit.
- k (CUSUM) is half the shift that you must catch, in sigmas of one day. Larkfield wants to catch 0.3 points, which is 0.482 sigma, so k = 0.241. A day exactly in the middle between “no shift” and “the shift” adds nothing.
- h (CUSUM), or L (EWMA), sets the false alarms. A larger value gives fewer false alarms and later real alarms.
To choose h, the notebook uses the simulation of Rules for patterns: 2,000 runs of 1,500 days, with the same process as the data. The goal is a false alarm about once a year: an in-control ARL near 370 days, as a textbook 3-sigma chart.
CUSUM with h = 4.0: a false alarm every 43.9 daysCUSUM with h = 6.0: a false alarm every 150.5 daysCUSUM with h = 8.0: a false alarm every 427.7 daysThe course’s measurement tried h from 3.0 to 8.0, and 8.0 was the closest to 370. For the EWMA, λ = 0.1 and L = 2.6 were chosen the same way: a false alarm every 344.6 days.
Five charts on the same shift
Section titled “Five charts on the same shift”Now every chart of this module runs on Larkfield’s refunds, and on the simulation. Before you read the table, predict: which chart finds the 0.3-point shift first?
chart first alarm days after the shift false alarms a month (stable months) days to a false alarm (simulated) days to catch 0.3 points (simulated) I-MR 2025-07-14 98 0.0 209.8 60.6 p-chart 2025-07-14 98 0.0 155.1 40.3Laney p' 2025-07-14 98 0.0 354.5 78.0 CUSUM 2025-05-20 43 0.0 427.7 28.5 EWMA 2025-05-12 35 0.0 344.6 22.4On the real data, the EWMA alarmed 35 days after the shift and the CUSUM 43 days after it. The three Shewhart charts needed 98 days. No chart gave a false alarm in the stable months. In the simulation, the EWMA catches the shift in 22.4 days on average and the CUSUM in 28.5. Both also give false alarms less often than the I-MR chart and the p-chart.
Text description of the chart
A column chart with five columns: EWMA 22.4 days, CUSUM 28.5, p-chart 40.3, I-MR 60.6 and Laney p′ 78.0. The first two columns, the charts that add up many days, are the shortest. The Laney p′-chart is the slowest, because its limits are the widest.
Here is the CUSUM of the real data, from January to July 2025. The total stays low until the shift. Then it climbs, alarms, restarts from 0, and climbs again:
- CUSUM, high side
- Alarm level h = 8
Text description of the chart
The chart shows the CUSUM total for each day from 1 January to 31 July 2025, with a dashed alarm line at 8. From January to early April, the total stays mostly below 3 and often falls back to 0. A label marks the start of the shift on 7 April. After it, the total climbs with ups and downs and crosses 8 on 20 May. It drops to 0, climbs again, crosses 8 on 15 June, drops to 0 again, and crosses 8 a third time on 14 July.
A chart that alarms again and again after a restart is telling you that the change is still there. Grace does not need three alarms to act. She needs the first one, and a reason to check the carrier.
Try it on one series
Section titled “Try it on one series”The explorer runs the five charts on the same 120 days of refund rates. The CUSUM and the EWMA use the settings above. A shift of the size you choose starts on day 31.
Explore
Five charts on the same small shift
Predict first: Which chart alarms first on a 0.3-point shift? Write your guess for this one series, and for the average over 2,000 simulated runs.
- First high alarm after the shift
- day 43 of the shift
- False alarms before the shift
- 0 in the 30 days before the shift
- Simulated: false alarms
- one every 209.8 days (0.15 a month)
- Simulated: a 0.3-point shift caught
- after 60.6 days on average
I-MR chart with rule 1, a shift of 0.3 points from day 31. The first high alarm after the shift starts is on day 73 of the series: day 43 of the shift. With no shift, this chart also alarms on that day: that alarm comes from the noise of one day, not from the shift. Before the shift, 0 false alarms in 30 days. In the simulation of 2,000 runs, this chart gives a false alarm every 209.8 days and catches a 0.3-point shift after 60.6 days on average.
- the chart’s value
- centre line and limits
- alarm
| Day | Value | Side | Why |
|---|---|---|---|
| 73 | 4.56% | high | rule 1 |
| 79 | 4.65% | high | rule 1 |
Text description of the explorer
The explorer shows one chart of 120 days at a time, with a vertical dashed line at day 31, where the shift starts. The I-MR, p and Laney charts show the refund rate of each day with their limits. The p and Laney limits move with the orders of each day. The CUSUM shows the high-side total above 0 and the low-side total below 0, with alarm levels at plus and minus 8. The EWMA shows the running average with limits that widen during the first days. Alarm days are circled. The readout gives the day of the first high alarm after the shift, the false alarms before it, and the simulated average run lengths.
With the 0.3-point shift, the CUSUM and the EWMA alarm on day 60 of the series, day 30 of the shift. The I-MR and p charts alarm on day 73, day 43 of the shift. They alarm on that day with no shift too, so that alarm comes from the noise of one day. No chart alarms in the first 30 days. With no shift, each chart gives one or two alarm days in 120 days, all false. With a shift of 1 point, the I-MR and p charts alarm on the first day of the shift. The CUSUM and the EWMA alarm on day 5. The simulation’s averages for the 0.3-point shift are: EWMA 22.4 days, CUSUM 28.5, p-chart 40.3, I-MR 60.6 and Laney p′ 78.0.
Move the shift to 1 point. Now the one-day charts are as fast as the CUSUM and EWMA, or faster. CUSUM and EWMA are made for small, lasting shifts. For big, sudden jumps, a Shewhart chart is simpler and just as good. Many teams run both.
One agent: the honest limit
Section titled “One agent: the honest limit”The shift above is Larkfield-wide: about 680 orders a day. Grace also wants a chart for each of the 40 agents. One agent handles about 24.6 orders a day. That is a much smaller sample.
A big change on one agent is easy. We planted a rise of 3 points for agent A-17 from 2025-07-07. For one agent, 3 points is about one sigma a day. So the CUSUM uses the textbook settings for a one-sigma shift (k = 0.5, h = 4). A per-agent p-chart and this CUSUM both find it on 2025-07-13, 6 days later.
A-17: {'CUSUM': '2025-07-13', 'p-chart': '2025-07-13'}orders per agent and day: 24.6But what about a rise of 0.3 points on one agent? The notebook simulates it, with the same random numbers in the same order as the course’s measurement:
for one agent, 0.3 points is 0.104 sigmaCUSUM h = 16: a false alarm every 290.2 days, the shift caught after 160.9 daysFor one agent, 0.3 points is only a tenth of a sigma a day. Even a CUSUM tuned for exactly this shift needs 161 days on average: more than five months. No chart can do much better. This is the honest limit of any detector: a change smaller than the noise of the data it watches takes a long time to see. Size of the shift against size of the sample decides what is possible.
So Grace watches the Larkfield-wide rate for small shifts, and each agent for big ones. A small change on one agent shows up only after months, or when you combine agents into teams.
When to use CUSUM or EWMA
Section titled “When to use CUSUM or EWMA”Use them when a small, lasting change costs money every day: a refund rate, a fraud rate, an error rate. Choose k (or λ) from the size of the shift that matters, and h (or L) from the false alarms you can accept, by simulation.
They cost some clarity. A Shewhart chart says “today is unusual”. A CUSUM says “the last weeks add up to unusual”, which needs a sentence of explanation for Grace. They are tuned for one size of shift, and they are slower than a Shewhart chart on a big jump. And the restart after an alarm is a rule that you must write down.
Guided practice: the charts in the notebook
Section titled “Guided practice: the charts in the notebook”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells. The data cell ends with the
Ready:line. - In section 1, write your prediction for the eleven days. Then run the cell. The first
Trueis on day 11. - In section 2, run the EWMA cell. The first
Trueis on day 11 too. - In section 3, run the cell. You see
0.482 sigmaandk = 0.241. - Run the cell that defines
cusum()andewma(). It prints nothing. - In section 4, run the simulation and the grid. You see
427.7 daysforh = 8.0. - In section 5, write your prediction, then run the table. You see
2025-05-12for the EWMA. - In section 6, run the agent cells. You see
2025-07-13for A-17 and160.9days for one agent. - In section 7, write the three answers and run each check cell. Each prints
Check passed.
Your turn: a shorter memory
Section titled “Your turn: a shorter memory”Change one thing: the EWMA’s λ. With λ = 0.2, today counts 20%, so the chart reacts faster to new days, and its limits are wider. Before you run it, predict: will its first alarm after 2025-04-07 come before or after the 2025-05-12 of λ = 0.1? In the notebook, run ewma(zl, 0.2, 2.6, side="high") and write the first alarm day in ewma_02_day.
Show the answer
e2 = ewma(zl, 0.2, 2.6, side="high")ewma_02_day = str(e2.index[e2 & (e2.index >= "2025-04-07")][0].date())Check passed: ewma_02_day is right.The first alarm is on 2025-05-20, 43 days after the shift: 8 days later than with λ = 0.1. A shorter memory sees big jumps sooner, but it adds up fewer days, so it is slower on a small shift. Use a small λ for small shifts and a larger λ for larger ones. With λ = 0.2, you should also choose L again by simulation.
Common mistake
Settings by feel
Symptom: Tomás builds a CUSUM with h = 4, “because the textbook example uses 4”. It alarms every few weeks. Grace stops reading it.
CUSUM with h = 4.0: a false alarm every 43.9 daysCause: h = 4 suits a textbook example with k = 0.5, for a shift of one full sigma. Larkfield’s k is 0.241, for a shift of half a sigma. With a smaller k, each normal day adds more to the total, so the same h is reached much sooner by chance. Here, a false alarm comes every 43.9 days: about 0.7 a month.
Fix: choose k from the shift that you must catch. Then choose h by simulation of your own process, for the false alarms that you can accept: h = 8 gives 427.7 days. Write both settings, and the reason, next to the chart.
Consequence in AI work: a detector tuned by feel, with no measured false-alarm rate, is either noisy or late. Nobody knows which until the cost shows up. Every threshold in this course comes with its measured price.
Knowledge check
Section titled “Knowledge check”Check your understanding
Explain in your own words how a CUSUM adds up a small shift. What are k and h, and how do you choose each one?
Show the answer
Each day, the CUSUM adds the part of z that is above the slack k, and it never goes below 0. Days with no shift add little or take away, so the total stays low. Days with a small, lasting shift add a little every day, so the total climbs until it passes the alarm level h. k is half the shift that you must catch, in sigmas of one day: 0.241 for Larkfield’s 0.3 points. h sets the false alarms: you choose it by simulation, here h = 8 for a false alarm every 427.7 days.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
For the planted 0.3-point refund shift, which chart alarmed first on the real data, how many false alarms did each give in the stable months, and which chart would you give to Grace, and why?
Show the answer
The EWMA alarmed first, on 2025-05-12 (35 days), then the CUSUM on 2025-05-20 (43 days). The I-MR, p and Laney charts needed 98 days. All five gave 0 false alarms in the stable months. In the simulation, the EWMA catches the shift in 22.4 days and gives a false alarm every 344.6 days. So give Grace the EWMA (or the CUSUM) for small, lasting shifts, with one sentence that explains it. Keep a Shewhart chart for big, sudden jumps and broken feeds, because it is easy to read.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Omar asks for a CUSUM per agent that finds any agent whose refund rate rises by 0.3 points within a month. What do you tell him?
Show the answer
One agent has about 24.6 orders a day, so a 0.3-point rise is only about 0.1 sigma a day. Even a CUSUM tuned for it needs 160.9 days on average to find it, with a false alarm every 290.2 days. A month is not possible with this much data. Per-agent charts find big changes fast (A-17’s 3 points in 6 days). For small changes, watch the whole of Larkfield or groups of agents, or accept a wait of months.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- CUSUM adds up the part of each day above a slack k and alarms at a level h. EWMA keeps a running average with a memory λ. Both see a small, lasting shift that no single day shows.
- Choose k (or λ) from the shift that matters, and h (or L) by simulation. Here, k = 0.241 and h = 8, and λ = 0.1 and L = 2.6.
- On the 0.3-point shift: EWMA 22.4 and CUSUM 28.5 days on average, against 40.3 to 78.0 for the one-day charts. On the real data, 35 and 43 days against 98.
- For one agent, the same shift needs 160.9 days: a change smaller than the noise takes months to see.
You can now watch one number per day for any kind of change. But a payment has many numbers, and the whole shape of the amounts can change while the daily average stays the same. Next, you compare two whole distributions.