---
title: Diagnostic accuracy, device software and regulation
heading: Sensitivity and specificity from a threshold, predictive values and prevalence by Bayes' rule, likelihood ratios and false alarms, embedded systems inside medical devices, software safety classes and cybersecurity, and how devices are classified and cleared for sale
eyebrow: S15 · Lesson 47 · Diagnostic accuracy, device software and regulation
xp: 50
---

::: hook
A new screening device detects atrial fibrillation from a wristband with 95% sensitivity and 90% specificity. That sounds excellent, until it is sold to millions of healthy people, of whom about 2% have the condition: then only one positive result in six is real. And before it could be sold at all, its makers had to show regulators how the device's software was designed, tested and protected, and which class of risk it falls into. This lesson covers how a medical instrument's performance is judged and how it reaches patients.
:::

::: play
- A test's value is spread as a bell curve in healthy people (centered at 0) and in people with the condition (centered at d). Anyone above the threshold is called positive.
- The curves show the test's sensitivity (the fraction of sick people called positive) and its specificity (the fraction of healthy people called negative) as the threshold moves.
- Raising the threshold trades sensitivity for specificity. The readouts give both at the threshold you set, and the predictive value of a positive result at the prevalence you set. Lower the prevalence and watch the positive predictive value collapse even though the test hasn't changed.
:::

```sim plot
view: graph
x: t = -2..5
y: p = 0..1
slider: d = 1..4 = 2.5
slider: th = -1..4 = 1.3
slider: prev = 0.005..0.5 = 0.02
plot: Q(t - d) | samples: 300 | label: sensitivity
plot: 1 - Q(t) | samples: 300 | color: ch3 | label: specificity
marker: t = th
readout: sensitivity = Q(th - d) | digits: 3
readout: specificity = 1 - Q(th) | digits: 3
readout: positive predictive value = Q(th - d)*prev/(Q(th - d)*prev + Q(th)*(1 - prev)) | digits: 3
readout: negative predictive value = (1 - Q(th))*(1 - prev)/((1 - Q(th))*(1 - prev) + (1 - Q(th - d))*prev) | digits: 4
```

::: predict
## A test has 95% sensitivity and 90% specificity. In a population where 2% have the disease, what fraction of positive results are true?

- [ ] 95%
- [ ] 90%
- [ ] About 50%
- [x] About 16%

::: reveal
Out of 1000 people, 20 have the disease and the test catches 19. Of the 980 healthy people, 10% (98) test positive anyway. So 19 of the 117 positives are real: 16%. Specificity counts heavily when the condition is rare, because the healthy outnumber the sick.
:::
:::

::: nameit
## Diagnostic accuracy

Compare a test against a reference standard and count true and false positives and negatives (TP, FP, TN, FN):

- **sensitivity** = TP/(TP + FN), the fraction of the diseased the test detects;
- **specificity** = TN/(TN + FP), the fraction of the healthy it clears.

Both depend on where the threshold is set. Plotting sensitivity against 1 − specificity as the threshold sweeps gives the **receiver operating characteristic** (ROC); the area under it summarizes how well the test separates the groups whatever the threshold.

What a clinician needs is the probability of disease given the result. By Bayes' rule, with prevalence p,

$$\text{PPV} = \frac{\text{sens}\cdot p}{\text{sens}\cdot p + (1 - \text{spec})(1 - p)}, \qquad \text{NPV} = \frac{\text{spec}(1 - p)}{\text{spec}(1 - p) + (1 - \text{sens})p}$$

The **likelihood ratio** of a positive result, sens/(1 − spec), multiplies the odds of disease before the test to give the odds after it.

## Alarms

Monitors face the same arithmetic per event. A detector that misclassifies one heartbeat in 10,000 as an arrhythmia raises about 10 false alarms a day at 100,000 beats a day. Staff learn to ignore frequent false alarms, so alarm algorithms favor specificity, add delays and require confirmation over several beats.

## Embedded systems in medical devices

Most instruments in this course end in a microcontroller: an amplifier and anti-alias filter, an ADC whose step referred to the input sets the resolution (lesson 1), processing such as QRS detection (lesson 52), a display, alarms, and often a wireless link. Webster's example is an ECG system on a small microcontroller board, streaming to a phone. Two concerns are specific to medicine:

- **Software safety.** IEC 62304 assigns software a safety class by the worst harm a failure could contribute to: A (no injury), B (non-serious injury), C (death or serious injury). The class sets how much design documentation, verification and testing the software needs. Risk management (ISO 14971) identifies hazards and shows each is reduced to an acceptable level.
- **Cybersecurity.** Connected devices, from infusion pumps to pacemakers with wireless programmers, must authenticate who can change their settings and protect patient data. Regulators now require a security plan and the ability to patch software.

## Regulation

In the United States the FDA sorts devices into three classes by risk:

- **Class I** (low risk, such as tongue depressors and stethoscopes): general controls, mostly exempt from premarket review.
- **Class II** (moderate risk, such as ECG monitors, infusion pumps and most diagnostic equipment): usually a **510(k)** submission showing the device is substantially equivalent to one already legally sold, plus special controls such as performance standards.
- **Class III** (life-sustaining or implanted, such as pacemakers and implantable defibrillators): **premarket approval** (PMA), with clinical evidence of safety and effectiveness.

Electrical safety and essential performance are demonstrated against IEC 60601-1 and its collateral and particular standards (the leakage-current limits of lesson 46 come from it). Other regions use similar risk classes; the European Union requires a CE mark under its medical device regulation. Development follows **design controls**: documented user needs, requirements, design reviews, verification that the design meets its requirements, and validation that it meets the users' needs.
:::

::: example
## A wristband rhythm test has 95% sensitivity and 90% specificity. Find its predictive values in a population with 2% prevalence, and what improving specificity to 99% does.

::: solution
1. **Count each outcome.** Per person tested:
   $$\begin{aligned} \text{TP} &= 0.95 \times 0.02 \\ &= 0.019 \\ \text{FP} &= 0.10 \times 0.98 \\ &= 0.098 \\ \text{TN} &= 0.90 \times 0.98 \\ &= 0.882 \\ \text{FN} &= 0.05 \times 0.02 \\ &= 0.001 \end{aligned}$$
2. **Positive predictive value.**
   $$\begin{aligned} \text{PPV} &= \frac{0.019}{0.019 + 0.098} \\ &= \frac{0.019}{0.117} \\ &= 16\% \end{aligned}$$
3. **Negative predictive value.**
   $$\begin{aligned} \text{NPV} &= \frac{0.882}{0.882 + 0.001} \\ &= \frac{0.882}{0.883} \\ &= 99.9\% \end{aligned}$$
   A negative result is very reassuring; a positive one usually isn't disease.
4. **With 99% specificity.** The false positives fall from 98 to 9.8 per 1000:
   $$\begin{aligned} \text{PPV} &= \frac{0.019}{0.019 + 0.0098} \\ &= 66\% \end{aligned}$$
5. **Likelihood ratio.** It rises from 9.5 to 95:
   $$\begin{aligned} \frac{0.95}{0.10} &= 9.5 \\ \frac{0.95}{0.01} &= 95 \end{aligned}$$
   For screening healthy people, specificity matters most.

**Answer:** at 2% prevalence the PPV is 16% and the NPV 99.9%; raising specificity to 99% lifts the PPV to 66% and the likelihood ratio from 9.5 to 95.
:::
:::

::: example
## Classify these devices and the software in them: a bedside ECG monitor with arrhythmia alarms, and an implantable cardioverter-defibrillator.

::: solution
1. **The bedside monitor's class.** It is moderate risk: a Class II device cleared through a 510(k), compared against monitors already on the market and tested to IEC 60601-1 and the particular standard for ECG monitoring.
2. **Its software.** The arrhythmia software could contribute to serious injury if it missed a lethal rhythm with no other safeguard, so it is likely safety class C.
3. **What can lower that.** The class can be lowered if the risk analysis shows that hardware or procedures outside the software prevent that harm.
4. **The implantable defibrillator's class.** It sustains life and is implanted: Class III, requiring premarket approval with clinical data.
5. **Its firmware.** It decides when to shock, so it is class C, with the most rigorous verification, and its wireless programming link must be secured.

**Answer:** the bedside monitor is a Class II device whose arrhythmia software is likely safety class C; the implantable defibrillator is Class III, with class C firmware.
:::
:::

::: example
## A new arrhythmia detector is expected to have 95% sensitivity. How many patients with the arrhythmia must a study enroll so that the 95% confidence interval's lower edge stays at or above 90%?

::: solution
1. **Standard error of a sensitivity.** The sensitivity is a proportion measured on the diseased patients:
   $$\text{SE} = \sqrt{\frac{p(1 - p)}{n}}$$
2. **Sample size.** Keeping 1.96 standard errors within 5 points needs
   $$\begin{aligned} n &= \frac{1.96^2 \times 0.95 \times 0.05}{0.05^2} \\ &= 73 \end{aligned}$$
   diseased patients.
3. **People to screen.** At a prevalence of 2% that means screening about 3650 people, which is why such studies enroll known cases, then test specificity separately on healthy volunteers.

**Answer:** the study must enroll 73 patients with the arrhythmia.
:::
:::

::: check
given: a = 80 | 90 | 95; b = 3 | 8 | 15
answer: 100*a/(a + b)
unit: %
tolerance: 0.5%

## Against a reference standard, a test is positive in {a} of the patients who have the disease and negative in {b} of them. What's its sensitivity?

::: right
Right. TP/(TP + FN) = {answer}%.
:::

::: misses
- [100*b/(a + b)] That's the miss rate, 1 − sensitivity.
- [any] Not yet. The fraction of diseased patients the test detects.
:::
:::

::: check
given: n = 850 | 900 | 950; f = 20 | 50 | 90
answer: 100*n/(n + f)
unit: %
tolerance: 0.5%

## Among healthy subjects the test is negative in {n} and positive in {f}. What's its specificity?

::: right
Right. TN/(TN + FP) = {answer}%.
:::

::: misses
- [100*f/(n + f)] That's the false-positive rate, 1 − specificity.
- [any] Not yet. The fraction of healthy subjects correctly cleared.
:::
:::

::: check
given: s = 0.85 | 0.95 | 0.99; c = 0.9 | 0.95 | 0.99; p = 0.01 | 0.05 | 0.2
answer: 100*s*p/(s*p + (1 - c)*(1 - p))
unit: %
tolerance: 0.5%

## A test with sensitivity {s} and specificity {c} is used where the prevalence is {p}. What's its positive predictive value?

::: right
Right. Bayes' rule gives {answer}%.
:::

::: misses
- [100*s] That's the sensitivity: the chance of a positive test given disease, not of disease given a positive test.
- [100*c] Specificity alone isn't the predictive value; combine it with sensitivity and prevalence.
- [any] Not yet. True positives over all positives, per person tested.
:::
:::

::: check
given: s = 0.85 | 0.95 | 0.99; c = 0.9 | 0.95 | 0.99; p = 0.01 | 0.05 | 0.2
answer: 100*c*(1 - p)/(c*(1 - p) + (1 - s)*p)
unit: %
tolerance: 0.1%

## For the same test and prevalence (sensitivity {s}, specificity {c}, prevalence {p}), what's the negative predictive value?

::: right
Right. {answer}%.
:::

::: misses
- [100*c] Specificity is the chance of a negative test given health, not of health given a negative test.
- [any] Not yet. True negatives over all negatives.
:::
:::

::: check
given: s = 0.8 | 0.9 | 0.95; c = 0.9 | 0.95 | 0.98
answer: s/(1 - c)
tolerance: 0.5%

## What's the positive likelihood ratio of a test with sensitivity {s} and specificity {c}?

::: right
Right. sens/(1 − spec) = {answer}: a positive result multiplies the odds of disease by that much.
:::

::: misses
- [s/c] Divide by the false-positive rate, 1 − specificity.
- [any] Not yet. LR+ = sens/(1 − spec).
:::
:::

::: check
given: f = 1 | 2 | 5; r = 60 | 80; h = 24 | 72
answer: f*1e-5*r*60*h
tolerance: 0.5%

## An arrhythmia detector falsely flags {f} beats in 100,000. How many false alarms does it raise for a patient with a heart rate of {r} beats/min monitored for {h} hours?

::: right
Right. {r} × 60 × {h} beats times {f} × 10⁻⁵: {answer} false alarms.
:::

::: misses
- [f*1e-5*r*h] Convert hours to minutes.
- [any] Not yet. Count the beats, then multiply by the false-alarm rate per beat.
:::
:::

::: check
kind: choice

## Under the FDA's classification, which pathway does an infusion pump usually take to market?

- [ ] Exempt from premarket review, as a Class I device
- [x] A 510(k) showing substantial equivalence, as a Class II device
- [ ] Premarket approval with clinical trials, as a Class III device
- [ ] No regulation, since it only delivers drugs

::: misses
- [1] A pump that can overdose a patient is more than low risk.
- [3] Pumps are moderate risk; Class III is for life-sustaining or implanted devices such as pacemakers.
- [4] Infusion pumps are medical devices, and the FDA scrutinizes them closely.
:::
:::

::: check
kind: parts
given: p = 0.85 | 0.95 | 0.98; m = 0.02 | 0.05; q = 0.01 | 0.1

## A study must show a sensitivity of about {p} with a 95% confidence half-width of {m}, in a condition with prevalence {q}.

::: part
answer: 1.96^2*p*(1 - p)/m^2
unit: patients
tolerance: 1%

### How many patients with the condition must it include?

::: misses
- [1.96*p*(1 - p)/m^2] Square the 1.96.
- [any] $z^2p(1 - p)/m^2$.
:::
:::

::: part
answer: 1.96^2*p*(1 - p)/m^2/q
unit: people
tolerance: 1%

### How many people would an unselected screening study need?

::: misses
- [any] Divide by the prevalence.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.5..0.999, c = 0.5..0.999, p = 0.001..0.5

## A test has sensitivity s and specificity c, used at prevalence p.

::: step
answer: s*p/(s*p + (1 - c)*(1 - p))

### What is its positive predictive value?
:::

::: step
answer: c*(1 - p)/(c*(1 - p) + (1 - s)*p)

### What is its negative predictive value?
:::

::: right
Right. Rare conditions drag the positive predictive value down whatever the test's quality.
:::
:::

::: explain
## Why does a test with excellent sensitivity and specificity produce mostly false positives when used to screen a healthy population?

::: model
Sensitivity and specificity describe how the test behaves in people already known to be sick or healthy. A screened population is overwhelmingly healthy, so even a small false-positive rate applies to a very large number of people, while the true positives come from the small diseased fraction. At 2% prevalence, a test with 90% specificity falsely flags 10% of the 98% who are healthy, about 9.8% of everyone, against at most 2% true positives, so most positives are false. The predictive value of a positive result therefore depends on prevalence as much as on the test, which is why screening tests are followed by more specific confirmatory tests, and why raising specificity matters more than raising sensitivity for screening.
:::
:::

::: practice

::: check
given: s = 0.6..0.99; c = 0.6..0.99; p = 0.005..0.5
answer: 100*s*p/(s*p + (1 - c)*(1 - p))
unit: %
tolerance: 0.5%

## PPV: sensitivity {s}, specificity {c}, prevalence {p}?

::: misses
- [any] Bayes: sens·p over all positives.
:::
:::

::: check
given: s = 0.5..0.99; c = 0.5..0.99
answer: s/(1 - c)
tolerance: 0.5%

## LR+ for sensitivity {s}, specificity {c}?

::: misses
- [any] sens/(1 − spec).
:::
:::
:::
