Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Interpreting test results

Max Planck Institute for Astronomy, KΓΆnigstuhl 17, 69117 Heidelberg, Germany

Apply Bayes theorem to infer COVID probability given test result

# import matplotlib and define our alias
from matplotlib import pyplot as plt

# numpy
import numpy as np
# Define posterior function
def post(p_d_m, p_d_nm, p_m):
    """
    Posterior P(M | D) calculation given
    P(D | M), P(D | M'),  P(M).
    """
    p_nm = 1 - p_m
    oddsr = (p_d_m * p_m)/ (p_d_nm * p_nm)
    p_m_d = 1/(1 + 1/oddsr)
    return p_m_d, oddsr
# Vary reliability of test P(D | M)
# probability of true positive
p_d_m = np.linspace(0.01, 1, 100)
# probability of false positive
p_d_nm = 0.070
# prior probability of m
p_m = 0.008

# Calculate posterior
p_m_d, oddsr = post(p_d_m, p_d_nm, p_m)
# Plot
fig, ax = plt.subplots(1, 1, figsize=(5, 4))
ax.plot(p_d_m, p_m_d)
ax.set_xlabel('P(D | M)', fontsize=12)
ax.set_ylabel('P(M | D)', fontsize=12)
plt.show()
<Figure size 500x400 with 1 Axes>
# Vary false positive probabtlity
# probability of true positive
p_d_m = 0.9
# probability of false positive
p_d_nm = 10**np.linspace(-4, 0, 100)
# prior probability of m
p_m = 0.008

# Calculate posterior
p_m_d, oddsr = post(p_d_m, p_d_nm, p_m)
# Plot
fig, ax = plt.subplots(1, 1, figsize=(5, 4))
ax.plot(np.log10(p_d_nm), p_m_d)
ax.set_xlabel('log[P(D | M\')]', fontsize=12)
ax.set_ylabel('P(M | D)', fontsize=12)
plt.show()
<Figure size 500x400 with 1 Axes>