Functional performance testing of probabilistic systems
Why This Matters
“The model is 77.5% accurate” sounds like a fact. Measured on 40 cases, the honest statement is “somewhere between 62.5% and 87.7%, with 95% confidence”. Whether that passes an acceptance criterion of 70% depends on which end of the range is true, and 40 cases cannot tell you. ML functional performance testing is about measuring metrics with their uncertainty, and knowing how many test cases you need before a result means anything.
What you will learn
- How acceptance criteria for probabilistic systems combine a metric, a margin of error and a confidence level
- How sample size, margin of error and confidence trade off, and what sequential testing adds
- How to calculate sample sizes and confidence intervals, and report results honestly
Syllabus: AI-6.1.3.
Metrics, statistically
ML functional performance testing measures accuracy, precision, recall and F1 (lesson 18) against the acceptance criteria, but statistically, not as a single pass/fail. A complete acceptance criterion has three parts:
- a metric and its target: accuracy of 98%
- a margin of error (MoE): ±4 percentage points
- a confidence level (CL): 95%
Read it as: “we want to be 95% confident that the true accuracy lies within 4 points of what we measured”.
The trade-off
Three quantities, one budget of test cases:
- Fix the confidence level and test more cases: the margin of error narrows.
- Fix the margin of error and test more cases: the confidence rises.
- Halving the margin needs roughly four times as many test cases.
A common approximation for the number of cases needed to measure a proportion p (accuracy, recall) within a margin E:
n = z² × p × (1 − p) / E², with z = 1.96 for 95% confidence.
| Expected accuracy | Margin | Test cases needed |
|---|---|---|
| 90% | ±5 points | 139 |
| 90% | ±2 points | 865 |
| 98% | ±1 point | 753 |
The syllabus contains its own worked example with different assumptions (“98% accuracy, ±4% MoE, 95% CL”). It is illustrative: the exam does not ask you to calculate sample sizes, but it does ask how they trade off.
Sequential testing
You don’t have to decide the number of test cases in advance. With sequential testing, you analyse the results as they come in and stop early once the evidence is strong enough. If the model keeps performing well, the target can be confirmed with far fewer cases than a fixed plan would use (the syllabus mentions about 170 instead of 601 in its example). If it performs badly, you find out early too.
Safety-critical: no failures allowed
For “at least 99% reliability, with 95% confidence”, a common approach is a success run: a number of test cases that must all pass.
n = ln(1 − confidence) / ln(reliability) = ln(0.05) / ln(0.99) = 298.1, so 299 test cases, all passing.
One failure and the claim cannot be made with that test set.
The test data
The test data must be:
- large enough for the margin and confidence you need
- fully independent of the training and validation data (lesson 16)
- representative of the operational domain (lesson 37)
Reporting
Report results with their uncertainty: “accuracy 94% ±4% at the 95% confidence level”, or as an interval: “between 90% and 98%”. A single number without a margin invites over-interpretation.
Hands-on
You continue in the practice project. Create module-7/test_functional_performance.py:
"""Lesson "Functional performance testing of probabilistic systems": metrics with a margin of error and a confidence level."""
import math
import pytest
Z_95 = 1.96
def sample_size(expected, margin, z=Z_95):
"""Test cases needed to measure a proportion (accuracy) within +/- margin (normal approximation)."""
return math.ceil(z * z * expected * (1 - expected) / (margin * margin))
def success_run(reliability, confidence):
"""Test cases that must ALL pass to show `reliability` with `confidence` (zero failures allowed)."""
return math.ceil(math.log(1 - confidence) / math.log(reliability))
def wilson_interval(successes, n, z=Z_95):
p = successes / n
centre = (p + z * z / (2 * n)) / (1 + z * z / n)
margin = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / (1 + z * z / n)
return centre - margin, centre + margin
@pytest.mark.parametrize("expected, margin, needed", [(0.9, 0.05, 139), (0.9, 0.02, 865), (0.98, 0.01, 753)])
def test_sample_size(expected, margin, needed):
assert sample_size(expected, margin) == needed
def test_safety_critical_success_run():
assert success_run(0.99, 0.95) == 299
@pytest.fixture(scope="module")
def correct(api):
"""Did the bank's model (v1, threshold 0.5) get each of the 40 test applicants right?"""
rows = api.get(f"{api.base}/test-set", timeout=10).json()["data"]
return [(row["probability"] >= 0.5) == row["repaid"] for row in rows]
def test_report_accuracy_with_its_margin(correct):
k, n = sum(correct), len(correct)
low, high = wilson_interval(k, n)
print(f"\n accuracy {k}/{n} = {k / n:.3f}, 95% interval {low:.3f} to {high:.3f}")
assert low < k / n < high
@pytest.mark.xfail(strict=True, reason="40 test cases are too few to show 70% with 95% confidence")
def test_accuracy_is_at_least_70_percent_with_95_percent_confidence(correct):
low, _ = wilson_interval(sum(correct), len(correct))
assert low >= 0.70
The last test is how you write an acceptance criterion that respects uncertainty: the lower bound of the interval must meet the target, not the measured value.
Run it
pytest module-7/test_functional_performance.py -v -s
module-7/test_functional_performance.py::test_sample_size[0.9-0.05-139] PASSED
module-7/test_functional_performance.py::test_sample_size[0.9-0.02-865] PASSED
module-7/test_functional_performance.py::test_sample_size[0.98-0.01-753] PASSED
module-7/test_functional_performance.py::test_safety_critical_success_run PASSED
module-7/test_functional_performance.py::test_report_accuracy_with_its_margin
accuracy 31/40 = 0.775, 95% interval 0.625 to 0.877
PASSED
module-7/test_functional_performance.py::test_accuracy_is_at_least_70_percent_with_95_percent_confidence XFAIL
The model measured 77.5%, above the 70% target. But the interval runs from 62.5% to 87.7%, and its lower end is below 70%. With 40 test cases you cannot claim the target is met with 95% confidence. You need more test data: with an expected accuracy around 78% and a margin of 5 points, about 264 cases.
Exercise
- How many test cases do you need to show an accuracy of about 92% within ±3 points, with 95% confidence?
- A safety requirement asks for 99.9% reliability with 95% confidence, with no failures allowed. How many test cases must all pass?
- A report says “recall: 0.96”. What questions do you ask before you accept it?
Show suggested answer
- n = 1.96² × 0.92 × 0.08 / 0.03² = 3.8416 × 0.0736 / 0.0009 = 315 (rounded up).
- n = ln(0.05) / ln(0.999) = 2.996 / 0.0010005 = 2,995 test cases, all passing. Ten times the reliability margin costs ten times the test cases.
- On how many positive cases? (Recall only counts actual positives: 0.96 on 25 positives is 24 of 25, with a wide interval.) With which margin and confidence? Was the test data independent of training and validation, and representative of production? At which threshold? And what are precision and the other metrics at that threshold?
Exam-style questions
1. A team wants to narrow the margin of error of its accuracy estimate without lowering the confidence level. What should it do?
- A. Use fewer test cases
- B. Use more test cases
- C. Report only the measured accuracy
- D. Lower the acceptance threshold
Show answer
2. Which three elements should an acceptance criterion for a probabilistic ML system contain?
- A. A metric, a margin of error and a confidence level
- B. A test script, a tester and a deadline
- C. Accuracy, latency and cost
- D. Training, validation and test data
Show answer
3. What is the advantage of sequential testing?
- A. It removes the need for test data
- B. Results are analysed as they accumulate, so testing can stop early once the evidence is sufficient
- C. It guarantees 100% accuracy
- D. It uses the training data for testing
Show answer
Your project so far
ai-testing-practice/
├── ...
└── module-7/
├── test_functional_performance.py new in this lesson
└── test_metamorphic.py
The finished version is in the example repository: module-7/test_functional_performance.py.