
code, test and improve a rule that flags unusual readings in a heart-rate sequence, and explain how it compares with a learned sequence model.
Instruction
The approved course offers the option of building a model that detects heart-rate anomalies. Training a real recurrent network needs large labeled datasets and computing resources, so here you will build a transparent rule that also uses recent history, test it, find its weakness and fix it. Understanding why a simple rule fails is the best preparation for understanding what a learned model has to get right.
The data. You will use the illustrative sequence from Semester A (invented for teaching, not a real patient): twelve readings one minute apart, 72, 74, 71, 73, 75, 72, 70, 96, 98, 74, 73, 72 beats per minute. You do not measure or record anyone’s heart rate, yours or a family member’s, for this lesson; every series you test is provided in the reading.
Rule version 1. Compare each reading with the average of the three readings before it (its baseline). If it differs by more than 15 bpm, flag it. Work through minutes 8 to 11. Minute 8 (96): the baseline is (75 + 72 + 70) ÷ 3 ≈ 72.3; the difference is about 23.7, so it is flagged. Minute 9 (98): the baseline is (72 + 70 + 96) ÷ 3 ≈ 79.3; the difference is about 18.7, flagged. Minute 10 (74): the baseline is (70 + 96 + 98) ÷ 3 = 88; the difference is 14, not flagged. Minute 11 (73): the baseline is (96 + 98 + 74) ÷ 3 ≈ 89.3; the difference is about 16.3, so it is flagged, even though 73 is a perfectly ordinary reading. The spike contaminated the baseline, and the rule raised a false alarm on the return to normal.
Rule version 2. Build the baseline only from readings that were not flagged. Now minutes 8 and 9 are flagged (baseline about 72.3 for both), and minutes 10, 11 and 12 are not, because their baselines (about 72.3, 72.0 and 72.3) come from normal readings. One design change removed the false alarm.
// Illustrative data: invented for teaching, not a real patient
let bpm = [72, 74, 71, 73, 75, 72, 70, 96, 98, 74, 73, 72];
let limit = 15;
function setup() {
createCanvas(520, 300);
textSize(11);
noLoop();
}
function draw() {
background(250);
let normal = []; // readings used for the baseline
for (let i = 0; i < bpm.length; i++) {
let flagged = false;
if (normal.length >= 3) {
let n = normal.length;
let baseline = (normal[n - 1] + normal[n - 2] + normal[n - 3]) / 3;
flagged = abs(bpm[i] - baseline) > limit;
}
if (!flagged) {
normal.push(bpm[i]); // version 2: only unflagged readings
}
let x = map(i, 0, bpm.length - 1, 50, width - 40);
let y = map(bpm[i], 40, 120, 270, 30);
noStroke();
if (flagged) {
fill(200, 70, 40);
} else {
fill(40, 90, 160);
}
circle(x, y, 12);
fill(60);
text(i + 1, x - 4, 292);
}
}To see version 1, change the baseline so it uses the three previous readings of bpm directly instead of the normal list.
What the rule cannot do. It does not know context. A rise after climbing stairs is normal; the same rise at rest might not be. It uses one fixed threshold for everyone. A learned sequence model such as an RNN could, with enough labeled data, learn patterns across context and people, but it would be harder to explain and could learn shortcuts from its data. That trade-off between a transparent rule and a more capable but opaque model is central to AI design in medicine.
Design it. Add a legend, axis labels and on-screen text stating that the data is illustrative and the flag is not a diagnosis. Consider interaction: a slider or keys to change limit so viewers can see how the threshold changes the flags.
Option: a rule-based clinic-guide chatbot. If you prefer, build a p5.js chatbot that answers questions about your Semester A brochure topic (what happens in an MRI, how a blood pressure cuff works) from a fixed list of sourced answers, never gives advice or a diagnosis, and always offers “talk to a clinician” and the 988 Lifeline where relevant. Test it with 20 questions and tally correct, unanswered and wrong responses.
📖 Read for this lesson
The reading opens over the lesson; close it to come back to exactly where you were. The checks below draw on it.
Formative check
Work through these before moving on. They are not graded — they tell you whether the standard below has landed yet.
Under rule version 1, why is minute 11 (73 bpm) flagged?
With a threshold of 25 bpm instead of 15, which minutes does rule version 2 flag?
A learned sequence model is always a better choice for a medical flag than a transparent rule.
Simulation & tools
Code and test the anomaly flag
Open the p5.js web editor (no sign-in needed) and paste the lesson sketch. Run it and record which minutes are flagged. Then (1) switch to version 1 by basing the baseline on the three previous bpm readings and record the flags again; (2) try thresholds of 10, 15 and 25 and record the flags for each in a table; (3) add a line of on-screen text saying the data is illustrative and the flag is not a diagnosis. Answer: which threshold would you choose, and what does each choice trade off between missed anomalies and false alarms? Without a computer, apply both rules by hand to the twelve readings, recording each baseline and difference in the worksheet printed in this lesson’s reading.
Open p5.js Web Editor ↗ · p5.js Web Editor, Processing Foundation · LGPL-2.1 open source
How confident are you that you can code, test and improve an anomaly-flag rule and compare it with a learned sequence model?
Practice
Work these on paper or in your notebook, then open Check your answer. Aim for all of Fluency and Application; try at least one Challenge.
Printable version: this unit’s practice workbook (PDF)
Fluency
Build speed and accuracy with the core skill.
- Define baseline, anomaly detection and transparent model.
Check your answer
Answer: Baseline: the expected value a new reading is compared against, often an average of recent readings. Anomaly detection: finding readings that differ markedly from what is expected. Transparent model: a model whose reasoning people can read and check, such as a written rule.The rule in this lesson is a transparent model that uses recent history. - State rule version 1 and rule version 2 in one sentence each.
Check your answer
Answer: Version 1: compare each reading with the average of the three readings before it, and flag it if it differs by more than 15 bpm. Version 2: the same, but build the baseline only from readings that were not flagged.Version 2 keeps a spike from contaminating the baseline. - Compute the version-1 baseline (average of the three previous readings) for minute 5 (previous readings 72, 74, 71) and minute 7 (previous readings 73, 75, 72). Is either reading flagged at 15 bpm?
Check your answer
Answer: Minute 5: baseline 72.3, reading 75, difference 2.7 (calculated from the unrounded baseline: 2.67). Minute 7: baseline 73.3, reading 70, difference 3.3. Neither is flagged.(72 + 74 + 71) / 3 = 217 / 3 = 72.33; 75 – 72.33 = 2.67. (73 + 75 + 72) / 3 = 220 / 3 = 73.33; 73.33 – 70 = 3.33.
Application
Use the skill in context. Show your reasoning.
- Using the illustrative series (72, 74, 71, 73, 75, 72, 70, 96, 98, 74, 73, 72), apply version 1 with a threshold of 10 bpm to minutes 8 to 12. Which minutes are flagged?
Check your answer
Answer: Minutes 8, 9, 10 and 11 are flagged; minute 12 is not.Baselines and differences: minute 8, 72.3, 23.7; minute 9, 79.3, 18.7; minute 10, 88.0, 14.0; minute 11, 89.3, 16.3; minute 12, (98 + 74 + 73) / 3 = 81.7, 9.7. All above 10 except minute 12. - Apply version 1 with a threshold of 20 bpm. Which minutes are flagged, and what does this show about raising the threshold?
Check your answer
Answer: Only minute 8 (difference 23.7). Minute 9 (difference 18.7) is missed. Raising the threshold removes false alarms but can miss real anomalies.Minute 9: baseline (72 + 70 + 96) / 3 = 79.3; 98 – 79.3 = 18.7, under 20. Minutes 10 and 11 differ by 14.0 and 16.3, also under 20. - Apply version 2 with a threshold of 20 bpm. Which minutes are flagged? Compare with version 1 at 20 bpm.
Check your answer
Answer: Minutes 8 and 9. Version 2 catches minute 9, because the flagged 96 is kept out of its baseline, which stays at about 72.3, so 98 differs by about 25.7.Minute 8 is flagged and left out of the baseline, so minute 9’s baseline is (75 + 72 + 70) / 3 = 72.3; 98 – 72.3 = 25.7 > 20. Version 1 used 96 in the baseline and missed it. - Name two things the rule cannot do, and the trade-off the lesson describes between this rule and a learned sequence model.
Check your answer
Answer: It does not know context (a rise after climbing stairs is normal; the same rise at rest might not be), and it uses one fixed threshold for everyone. A learned model such as an RNN could, with enough labeled data, learn patterns across context and people, but it would be harder to explain and could learn shortcuts. The trade-off is transparency versus capability.That trade-off is central to AI design in medicine. - Write the on-screen text and legend for the heart-rate flag sketch, following the lesson’s design advice.
Check your answer
Answer: Model: Legend: blue circle = reading used for baseline; orange-red circle = flagged (differs from recent baseline by more than 15 bpm). Text: ‘Illustrative data, invented for teaching. A flag is not a diagnosis.’ Axis labels: ‘minute’ and ‘beats per minute’.The lesson asks for a legend, axis labels and on-screen text stating the data is illustrative and the flag is not a diagnosis.
Challenge
Stretch problems. Expect to think before you write.
- The optional clinic-guide chatbot is tested with 20 questions: 14 answered correctly, 4 unanswered (it says ‘I don’t have a sourced answer; please talk to a clinician’), 2 answered wrongly. Give each as a percentage, and argue which number matters most for safety.
Check your answer
Answer: Correct 70%, unanswered 20%, wrong 10%. The wrong answers matter most: an unanswered question sends the user to a clinician, but a wrong answer can mislead. The redesign should target the 2 wrong responses first.14 / 20 = 70%; 4 / 20 = 20%; 2 / 20 = 10%. The chatbot must never give advice or a diagnosis and should offer ‘talk to a clinician’ and 988 where relevant.
Review
Keep earlier skills sharp.
- Review (Semester A, Unit 4): Under the Semester A mapping map(bpm, 40, 120, 270, 30), where is the 74 bpm reading drawn?
Check your answer
Answer: y = 168.(74 – 40) / 80 = 0.425; 0.425 x 240 = 102; 270 – 102 = 168. The lesson sketch uses the same vertical mapping.
Heart-Rate Series, Worksheet and Answer Key for the Anomaly Flag
Series A, B and C are constructed teaching data written for this course and are illustrative, not patient data; reference range from MedlinePlus, National Library of Medicine, Pulse (retrieved 2026-09-26) · SLS original (constructed teaching data), with a public-domain reference range from the U.S. National Library of Medicine
Why this reading. The workshop asks you to code a flag, record which minutes it flags, try three thresholds and choose one. That needs a recorded series and the answer to check your code against. This reading prints three series, a worksheet for the lesson’s series with every baseline and difference worked out, the flags every rule and threshold should produce, and a normal-range reference. All series are invented for teaching: none is a real patient, and a flag here is never a diagnosis.
Reference: what is a normal resting pulse?
MedlinePlus (U.S. National Library of Medicine) lists a normal resting heart rate of 60 to 100 beats per minute for children 10 and older and adults, and 40 to 60 for well-trained athletes. It says continually high or low resting rates are worth discussing with a provider. Note that the rules below do not use this range: they flag a change from a recent baseline, so a reading of 96 can be normal in isolation and still be flagged.
The three series (SLS original, illustrative)
| Minute | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Series A (the lesson) | 72 | 74 | 71 | 73 | 75 | 72 | 70 | 96 | 98 | 74 | 73 | 72 |
| Series B (jittery) | 70 | 84 | 64 | 80 | 66 | 83 | 62 | 79 | 68 | 77 | 66 | 92 |
| Series C (slow drift) | 70 | 71 | 73 | 75 | 78 | 81 | 84 | 88 | 92 | 96 | 99 | 102 |
The rules: version 1 compares each reading with the mean of the three readings before it (from minute 4 on) and flags it when the absolute difference is more than the limit; version 2 builds the baseline only from readings that were not flagged. The first three readings are never flagged because they have no baseline. A difference exactly equal to the limit is not flagged.
Worksheet: Series A, limit 15
| Min | bpm | V1 baseline | V1 |diff| | V1 | V2 baseline | V2 |diff| | V2 |
|---|---|---|---|---|---|---|---|
| 1 | 72 | — | — | n/a | — | — | n/a |
| 2 | 74 | — | — | n/a | — | — | n/a |
| 3 | 71 | — | — | n/a | — | — | n/a |
| 4 | 73 | 72.33 | 0.67 | no | 72.33 | 0.67 | no |
| 5 | 75 | 72.67 | 2.33 | no | 72.67 | 2.33 | no |
| 6 | 72 | 73.00 | 1.00 | no | 73.00 | 1.00 | no |
| 7 | 70 | 73.33 | 3.33 | no | 73.33 | 3.33 | no |
| 8 | 96 | 72.33 | 23.67 | FLAG | 72.33 | 23.67 | FLAG |
| 9 | 98 | 79.33 | 18.67 | FLAG | 72.33 | 25.67 | FLAG |
| 10 | 74 | 88.00 | 14.00 | no | 72.33 | 1.67 | no |
| 11 | 73 | 89.33 | 16.33 | FLAG | 72.00 | 1.00 | no |
| 12 | 72 | 81.67 | 9.67 | no | 72.33 | 0.33 | no |
Minute 11 is the false alarm of version 1: baseline (96 + 98 + 74) / 3 = 89.33, and |73 − 89.33| = 16.33 > 15, although 73 is an ordinary reading. In version 2 the baseline for minutes 10 to 12 comes only from unflagged readings, so the spike does not contaminate it.
Answer key: which minutes each rule flags
| Series | Limit (bpm) | Version 1 flags minutes | Version 2 flags minutes |
|---|---|---|---|
| A | 10 | 8, 9, 10, 11 | 8, 9 |
| A | 15 | 8, 9, 11 | 8, 9 |
| A | 25 | none | none |
| B | 10 | 6, 7, 12 | 6, 12 |
| B | 15 | 12 | 12 |
| B | 25 | none | none |
| C | 10 | none | none |
| C | 15 | none | none |
| C | 25 | none | none |
Ground truth for scoring (constructed): in Series A the anomaly is minutes 8 and 9 (the spike); in Series B it is minute 12 only; in Series C there is a sustained climb from 70 to 102 that a clinician would want noticed, but no single reading jumps.
Scoring version 2 on Series A and B together (24 readings, 3 true anomalies):
| Limit | TP | FP | FN | TN | Accuracy | Sensitivity |
|---|---|---|---|---|---|---|
| 10 | 3 | 1 | 0 | 20 | 95.8% | 100% |
| 15 | 3 | 0 | 0 | 21 | 100.0% | 100% |
| 25 | 0 | 0 | 3 | 21 | 87.5% | 0% |
What the trade-off looks like: at a limit of 10 the rule catches every anomaly but raises a false alarm at minute 6 of Series B (83 against a baseline of 70.0, difference 13.0); at 15 it catches all three with no false alarm; at 25 it misses all three, yet its accuracy is still 87.5% (21 of 24) because most readings are normal. Series C shows the rule’s blind spot: no limit flags the slow climb, because each reading is close to the average of the three before it. That is what a learned sequence model, or a rule about trend, would have to add, and the price of a learned model is that you can no longer read its reasoning.
If you cannot run code
Fill the worksheet by hand for Series A, then find the version 2 flags for Series B at limits 10, 15 and 25 and check them against the answer key. The three numbers to look for are the baseline at minute 6 of Series B (70.00), its difference (13.00) and the baseline at minute 12 (70.33).
Sources: MedlinePlus, Pulse (U.S. National Library of Medicine) (medlineplus.gov)