import numpy as np
# 为了可复现
np.random.seed(42)
# 病人数量
n_patients = 1000
# 真实疟疾状态(1=有疟疾,0=无疟疾),设定流行率 30%
true_malaria = np.random.binomial(1, 0.3, n_patients)
# 检测性能
sensitivity = 0.95 # 敏感度:检出真阳性的能力(高→更少第二类错误)
specificity = 0.90 # 特异度:检出真阴性的能力(高→更少第一类错误)
# 模拟检测结果
test_positive = []
for case in true_malaria:
if case == 1:
# 真有疟疾:以敏感度的概率测为阳性
test_positive.append(np.random.rand() < sensitivity)
else:
# 无疟疾:以 (1 - 特异度) 的概率出现假阳性
test_positive.append(np.random.rand() > specificity)
test_positive = np.array(test_positive)
# 统计错误
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}")