Sensitivity, specificity, likelihood ratios and predictive values of a test
stats_diagnostic_accuracy
Works out sensitivity, specificity, likelihood ratios and diagnostic odds ratio from a 2 x 2 table.
- Runs
- Held: built, not on the server yet
- Level
- Tested, not reference-checked
Why it is held. No fault has been recorded. The checks this server requires before it lists a library tool are not finished for every method this tool uses. It is built, and the full check and confirmation this server requires of a library tool are not yet complete for every method it uses. Full check: not read from the source.
What it is
Returns sensitivity, specificity, likelihood ratios and diagnostic odds ratio from a 2 × 2 table of test counts. Use when a test's counts against a reference standard are known; intervals are Wilson or Clopper–Pearson, Katz and Woolf, and PPV and NPV are given at a prevalence you state. Not for association tests or risk ratios between groups: use `stats_contingency_table`. PPV and NPV are withheld without a stated prevalence (a proportion); a prevalence of 0 or 1, and Clopper–Pearson above 10^5 subjects per row, are refused.
Field: Statistics and design.
What it does
You give
truePositive(required): True positives: diseased subjects the test called positive. Must be at least 0.falsePositive(required): False positives: non-diseased subjects the test called positive. Must be at least 0.falseNegative(required): False negatives: diseased subjects the test called negative. Must be at least 0.trueNegative(required): True negatives: non-diseased subjects the test called negative. Must be at least 0.interval(required): The interval for sensitivity and specificity: 'wilson' (score, no continuity correction) or 'clopper-pearson' (exact).level(required): Confidence level of every interval: 0.9, 0.95 or 0.99 (or 90, 95, 99 %). Must be above 0 and below 1.
and 1 more, listed below
No request is shown while this tool is held.
Limits
- Research statistics and experimental-design arithmetic on the data, margins, prevalence and settings you supply.
- Not for diagnosis or treatment decisions and not a regulatory analysis.
- It does not choose a design, a margin, a cut-off or a sample size for you.
- PPV and NPV hold only at the prevalence you state; not a diagnostic authority.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.
It will not answer when
- the stated prevalence is 0 or 1, where one predictive value is zero over zero.
- all four counts are zero.
- interval is 'clopper-pearson' and TP + FN or TN + FP is above 10^5 (one hundred thousand), the largest row the outside program checked Clopper–Pearson at; its time grows linearly with the count (D0273B-F1); checked after NO_SUBJECTS.
Every input, with its units7 inputs
truePositive(required): True positives: diseased subjects the test called positive. Must be at least 0.falsePositive(required): False positives: non-diseased subjects the test called positive. Must be at least 0.falseNegative(required): False negatives: diseased subjects the test called negative. Must be at least 0.trueNegative(required): True negatives: non-diseased subjects the test called negative. Must be at least 0.interval(required): The interval for sensitivity and specificity: 'wilson' (score, no continuity correction) or 'clopper-pearson' (exact).level(required): Confidence level of every interval: 0.9, 0.95 or 0.99 (or 90, 95, 99 %). Must be above 0 and below 1.prevalence: The prevalence at which PPV and NPV are wanted, a proportion you state. Omit it and PPV and NPV are withheld. Must be at least 0 and at most 1. Optional.
Every refusal, with its code3 refusals
| Code | When |
|---|---|
PREVALENCE_AT_BOUND | the stated prevalence is 0 or 1, where one predictive value is zero over zero. |
NO_SUBJECTS | all four counts are zero. |
CLOPPER_PEARSON_ROW_ABOVE_RANGE | interval is 'clopper-pearson' and TP + FN or TN + FP is above 10^5 (one hundred thousand), the largest row the outside program checked Clopper–Pearson at; its time grows linearly with the count (D0273B-F1); checked after NO_SUBJECTS. |
The calculation
The method's equations, constants and worked example.
Equation 1
| Symbol | Meaning |
|---|---|
TP | true positives (a count) |
FN | false negatives (a count) |
Se | sensitivity |
Source 1.
Equation 2
| Symbol | Meaning |
|---|---|
TN | true negatives (a count) |
FP | false positives (a count) |
Sp | specificity |
Source 1.
Equation 3
| Symbol | Meaning |
|---|---|
LR+ | positive likelihood ratio |
LR− | negative likelihood ratio |
Source 1.
Equation 4
| Symbol | Meaning |
|---|---|
p | prevalence stated by the caller |
PPV | positive predictive value |
NPV | negative predictive value |
Source 1.
Two readings: source 1 can be read as PPV = p·Se / [p·Se + (1 − p)(1 − Sp)], Bayes' theorem with the false-positive rate or as p·Sen / [p·Sen + (1 − p)·Spe], as the source prints it. This project takes PPV = p·Se / [p·Se + (1 − p)(1 − Sp)], Bayes' theorem with the false-positive rate, because the printed denominator contradicts the source's own PPV = a/(a + b): at the sample prevalence it gives a/(a + d), where Bayes' form gives a/(a + b); NPV is printed in Bayes' form (found by the second implementation's agent, 24 November 2026). Taking p·Sen / [p·Sen + (1 − p)·Spe], as the source prints it would give a different result.
Equation 5
Wilson (no continuity correction): (p̂ + z²/(2n) ± z·√(p̂(1 − p̂)/n + z²/(4n²))) / (1 + z²/n)
z = Φ⁻¹(1 − (1 − level)/2) = Φ⁻¹((1 + level)/2)
| Symbol | Meaning |
|---|---|
p̂ | Se or Sp |
n | TP + FN or TN + FP (a count) |
level | confidence level |
z | the standard normal quantile Φ⁻¹((1 + level)/2) |
Φ⁻¹ | the standard normal quantile function |
Source 2.
Equation 6
Clopper–Pearson: lower = B⁻¹((1 − level)/2
x, n − x + 1), upper = B⁻¹((1 + level)/2
x + 1, n − x), the lower bound 0 at x = 0 and the upper 1 at x = n
| Symbol | Meaning |
|---|---|
n | TP + FN or TN + FP (a count) |
x | TP or TN (a count) |
level | confidence level |
B⁻¹(q; a, b) | the q quantile of the beta distribution with shape parameters a and b |
Source 3.
Equation 7
for an arranged table [[a, b], [c, d]], RR = (a/(a + b)) / (c/(c + d)), the ratio of the first row's proportion to the second's
LR+ = RR of [[TP, FN], [FP, TN]] and LR− = RR of [[FN, TP], [TN, FP]], each with the interval exp(log RR ± z·√(1/a − 1/(a + b) + 1/c − 1/(c + d))) (Katz, two independent proportions)
DOR = TP·TN/(FN·FP) with exp(log DOR ± z·√(1/TP + 1/FN + 1/FP + 1/TN)) (Woolf)
| Symbol | Meaning |
|---|---|
a, b, c, d | the arranged table's cells (a count) |
RR | relative risk |
DOR | diagnostic odds ratio |
z | the standard normal quantile Φ⁻¹((1 + level)/2), level the confidence level |
Source 3.
Conventions
- interval = wilson: Sensitivity and specificity, each a proportion (unit 1), with Wilson's score interval without continuity correction at the stated level (R prop.test(correct = FALSE)).
- interval = clopper-pearson: Sensitivity and specificity, each a proportion (unit 1), with the Clopper–Pearson interval from beta quantiles at the stated level (R binom.test); 0 at x = 0 and 1 at x = n.
A definition, not a model: the equation states what the quantity means. A different convention gives a different number.
Worked example
Worked example: one case of stats-diagnostic-accuracy@1.1.0, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.
Inputs
- truePositive = 90
- falsePositive = 10
- falseNegative = 10
- trueNegative = 90
- interval = wilson
- level = 0.95
- prevalence = 0.01
Steps
- se = 0.9, from truePositive, falseNegative; by equation 1; gives sensitivity.estimate.
- sp = 0.9, from trueNegative, falsePositive; by equation 2; gives specificity.estimate.
- lr-positive = 9, from se, sp; by equation 3; gives likelihoodRatioPositive.estimate.
- ppv = 0.083333333, from se, sp, prevalence; by equation 4; gives positivePredictiveValue.estimate.
The working, as the case writes it:
Se = 90/100
Sp = 90/100
LR+ = 0.9/0.1 = 9
LR- = 0.1/0.9 = 1/9
DOR = 90·90/(10·10) = 81
PPV = 0.009/(0.009 + 0.099) = 1/12
NPV = 0.891/(0.001 + 0.891) = 891/892
Answer
sensitivity.estimate = 0.9
specificity.estimate = 0.9
likelihoodRatioPositive.estimate = 9
positivePredictiveValue.estimate = 0.083333333
Where these values came from: an outside program, statsmodels 0.15.0; exact arithmetic; worked by hand in stored reference values §3.4; intervals from statsmodels.
At build the tool returned this answer within the tolerance the file states: sensitivity, specificity, PPV and NPV 1e-15 absolute; their interval limits 1e-12 absolute against statsmodels; likelihood ratios and the diagnostic odds ratio 1e-14 relative, their interval limits 1e-12 relative, each absolute below 1e-6; levels, counts, methods, statuses and codes exact.
Sources
Hajian-Tilaki K. Receiver operating characteristic (ROC) curve analysis for medical diagnostic test evaluation. Caspian J Intern Med 2013;4(2):627-635
Used for: Sensitivity, specificity, likelihood ratios, diagnostic odds ratio and predictive values from a 2 × 2 table. At: section section on sensitivity and specificity, 2 × 2 table (a = TP, c = FN); section section on sensitivity and specificity (d = TN, b = FP); section section on likelihood ratios; section section on predictive values, the Bayes form.
Read in full, 10 November 2026NIST/SEMATECH e-Handbook of Statistical Methods, §7.2.4.1 Confidence intervals (for a proportion)
Used for: Sensitivity, specificity, likelihood ratios, diagnostic odds ratio and predictive values from a 2 × 2 table. At: section 7.2.4.1, Formulas for the confidence intervals (the Wilson method, after Wilson 1927).
Read in full, 24 November 2026statsmodels 0.14.4 (Seabold S, Perktold J et al.), stored reference values (proportion_confint, method 'beta') and stored reference values (Table2x2.log_riskratio_se, log_oddsratio_se and their confint methods)
Used for: Sensitivity, specificity, likelihood ratios, diagnostic odds ratio and predictive values from a 2 × 2 table. At: section stored reference values lines 261-270 (SHA-256 b039f25cf95166fcc882a9fd47f72b7ab90013bfda9823aa7acf3c2950978ed9); stored reference values log_riskratio_se, log_oddsratio_se, log_riskratio_confint, log_oddsratio_confint (SHA-256 af2e5455df459abc9ced79186e16fecf38344cc630aeec1ade9e6e65b488090f).
Read in full, 24 November 2026
Cited, not read
Clopper CJ, Pearson ES 1934, Biometrika 26:404-413; Woolf B 1955, Ann Hum Genet 19:251-253; Katz D et al. 1978, Biometrics 34:469-474
Used for: Sensitivity, specificity, likelihood ratios, diagnostic odds ratio and predictive values from a 2 × 2 table. At: section clopperPearson; Clopper and Pearson 1934 not read, as statsmodels 0.14.4 stored reference values lines 261-270 computes it; section relativeRiskKatz and oddsRatioWoolf; Katz 1978 and Woolf 1955 not read, as statsmodels 0.14.4 Table2x2.log_riskratio_se and log_oddsratio_se compute them.
Woolf 1955 (Wiley) answered HTTP 403 and Katz 1978 (JSTOR) a client challenge to the raising session on 14 November 2026; Morris and Gardner 1988 (BMJ 296:1313, PMC2545775), which prints both log intervals, answered a reCAPTCHA page at PMC's PDF and Europe PMC's render on 24 November 2026; Clopper and Pearson 1934 (Biometrika, Oxford) not served openly
Not read; source 4 read in its place
How it is checked
- Level
Tested, not reference-checked.
- Checked against
An outside program, statsmodels, 0.15.0; a published dataset, Hajian-Tilaki 2013 Table 2; statsmodels, Caspian J Intern Med 4:627-635; statsmodels 0.15.0; a reference program this project wrote itself, second implementation (Python 3.13), 2026-11-24, mpmath 1.4.1 at 60 significant digits; every root bracketed to a relative width below 1e-45; each output rounded once to the nearest double; not an outside program.
- Cases
No case set is linked to this version.
- Not checked
The coverage of the intervals at small counts (the relative-risk interval's coverage is unchecked); the primary papers behind the imported intervals (Wilson 1927, Clopper and Pearson 1934, Woolf 1955, Katz et al. 1978) are not read: Wilson rests on the NIST/SEMATECH e-Handbook, the other three on statsmodels 0.14.4's code; Clopper–Pearson is refused above 10^5 subjects per row (D0273B-F1: its time grows linearly with the count); no interval is given for PPV or NPV; the caller's counts and the independence of the diseased and non-diseased samples.
- Open
No fault has been recorded. The checks this server requires before it lists a library tool are not finished for every method this tool uses. It is built, and the full check and confirmation this server requires of a library tool are not yet complete for every method it uses. Full check: not read from the source.
What was not checked, in fullfrom the method records
- The coverage of the intervals at small counts (the relative-risk interval's coverage is unchecked); the primary papers behind the imported intervals (Wilson 1927, Clopper and Pearson 1934, Woolf 1955, Katz et al.
- 1978) are not read: Wilson rests on the NIST/SEMATECH e-Handbook, the other three on statsmodels 0.14.4's code; Clopper–Pearson is refused above 10^5 subjects per row (D0273B-F1: its time grows linearly with the count); no interval is given for PPV or NPV; the caller's counts and the independence of the diseased and non-diseased samples.
- The tool assumes every subject received the reference standard whatever the test result (no partial verification) and that the reference standard is correct; a table biased either way cannot be detected from its counts.
Changes to its methods0 changes
No change is logged for these method versions.