This content originally appeared on DEV Community and was authored by Naomi Jepkorir
In statistics, making a decision is a bit like crossing a busy street without traffic lights, you have to weigh the risk of moving too soon against the risk of waiting too long. In hypothesis testing, those two risks are called Type I and Type II errors, and you can’t avoid them both entirely.
Meet the Errors
Type I Error (False Positive) – Rejecting the null hypothesis when it’s actually true. In medicine, this might mean diagnosing a patient with a disease they don’t have.
Type II Error (False Negative) – Failing to reject the null hypothesis when it’s false. In medicine, this might mean missing a diagnosis when the disease is present.
They’re like opposite sides of a see-saw , lowering one usually raises the other.
The Malaria Example
Picture yourself in a clinic in a malaria-endemic region. A patient walks in with fever, chills and body aches. You suspect malaria, and you have a rapid test .
If you make a Type I error , you say they have malaria when they don’t. They take unnecessary medicine , maybe get mild side effects, and the real cause of illness is missed.
If you make a Type II error, you say they don’t have malaria when they do. Without treatment, the disease can worsen quickly, and in severe cases, become life-threatening .
The Trade-off
In this setting, Type II errors are generally more dangerous . Why?
- Malaria progresses fast, especially in children and pregnant women.
- Anti-malarial treatment is relatively safe and inexpensive .
- Missing a real case can have far worse consequences than treating a false one.
That’s why some clinics treat suspected malaria even when the test is negative but symptoms are strong, better to risk a false positive than lose a life .
Simulating Type I and Type II Errors in Python
Here’s a small simulation showing how false positives and false negatives might look in malaria testing
import numpy as np
# Seed for reproducibility
np.random.seed(42)
# Number of patients
n_patients = 1000
# True malaria status (1 = has malaria, 0 = no malaria)
true_malaria = np.random.binomial(1, 0.3, n_patients) # 30% prevalence
# Test characteristics
sensitivity = 0.95 # correctly detect malaria (reduces Type II errors)
specificity = 0.90 # correctly detect no malaria (reduces Type I errors)
# Simulated test outcomes
test_positive = []
for case in true_malaria:
if case == 1:
# True malaria case: test is positive with probability = sensitivity
test_positive.append(np.random.rand() < sensitivity)
else:
# No malaria: false positive occurs with probability = (1 - specificity)
test_positive.append(np.random.rand() > specificity)
# Convert to NumPy array
test_positive = np.array(test_positive)
# Count errors
type_I_errors = np.sum((test_positive == 1) & (true_malaria == 0))
type_II_errors = np.sum((test_positive == 0) & (true_malaria == 1))
print(f"Type I errors (False Positives): {type_I_errors}")
print(f"Type II errors (False Negatives): {type_II_errors}")
Output:
Type I errors (False Positives): 73
Type II errors (False Negatives): 18
How to use it:
Change the
sensitivity
to see what happens when you try to catch every malaria case.Change the
specificity
to see what happens when you try to avoid false alarms.Notice how improving one tends to worsen the other, the eternal trade-off.
The Balancing Act
The choice between minimizing Type I or Type II errors depends on the context:
When the cost of a false positive is high (e.g., invasive surgery , expensive drugs ), reduce Type I errors.
When the cost of a false negative is high (e.g., fast-progressing diseases ), reduce Type II errors.
You can’t eliminate both, so you set your alpha (Type I error rate) and power (linked to Type II error rate) based on the stakes .
Final Thought
Choosing between Type I and Type II errors isn’t about perfection,it’s about priorities. In malaria diagnosis, the priority is saving lives , even if it means some people take medicine they don’t actually need.
In every field, the key question is:
“Which mistake can we live with, and which can we not afford to make?”
This content originally appeared on DEV Community and was authored by Naomi Jepkorir