HomeStatistics & Probability › Bayes' Theorem Calculator

Bayes' Theorem Calculator

Applies Bayes' theorem to a prior probability P(A) and the likelihoods P(B|A) and P(B|not A) to obtain the posterior P(A|B), the marginal P(B), the complementary posteriors and the likelihood ratio (Bayes factor).

When to use

You know how likely an evidence B is under a hypothesis A and under its negation and want the updated probability of A after observing B (test results, spam filtering, diagnosis, fault detection).

Do not use when: The inputs are sensitivity, specificity and prevalence in percent and you want predictive values per 1000 people (use diagnostic-test), or the events are simply independent (use probability-of-events).

Formula

P(A|B) = P(B|A)·P(A) / P(B), with P(B) = P(B|A)·P(A) + P(B|¬A)·(1 − P(A)); P(A|¬B) = (1 − P(B|A))·P(A) / (1 − P(B)); odds form: posterior odds = prior odds × P(B|A)/P(B|¬A)

Inputs

ParameterTypeUnitRequiredDescription
probability_anumberyesPrior probability of the hypothesis A, as a fraction between 0 and 1. Range: > 0
probability_b_given_anumberyesProbability of observing the evidence B when A is true (e.g. sensitivity as a fraction). Range: ≥ 0, ≤ 1
probability_b_given_not_anumberyesProbability of observing B when A is false (e.g. false-positive rate = 1 − specificity). Range: ≥ 0, ≤ 1

Outputs

OutputTypeUnitDescription
probability_a_given_bnumberProbability of A after observing B.
probability_bnumberMarginal probability of the evidence: P(B|A)·P(A) + P(B|¬A)·(1 − P(A)).
probability_not_a_given_bnumber1 − P(A|B).
probability_a_given_not_bnumberProbability of A when B is not observed (omitted when P(B) = 1).
likelihood_rationumberP(B|A) / P(B|¬A): how much B favours A (omitted when P(B|¬A) = 0).
prior_oddsnumberP(A) / (1 − P(A)).
posterior_oddsnumberprior_odds × likelihood_ratio = P(A|B) / P(¬A|B) (omitted when P(A|B) = 1).

Example

Drug test: 0.5 % prevalence, 99 % sensitivity, 1 % false positives (Wikipedia): {"probability_a":0.005,"probability_b_given_a":0.99,"probability_b_given_not_a":0.01}{"probability_a_given_b":0.332215,"probability_b":0.0149,"probability_not_a_given_b":0.667785,"probability_a_given_not_b":0.000051,"likelihood_ratio":99,"prior_odds":0.005025,"posterior_odds":0.497487}

P(A) = 0.3, P(B|A) = 0.8, P(B|not A) = 0.2: {"probability_a":0.3,"probability_b_given_a":0.8,"probability_b_given_not_a":0.2}{"probability_a_given_b":0.631579,"probability_b":0.38,"probability_a_given_not_b":0.096774,"likelihood_ratio":4,"prior_odds":0.428571,"posterior_odds":1.714286}

GET https://tttkmbb.com/api/v1/calculate/bayes-theorem?probability_a=0.005&probability_b_given_a=0.99&probability_b_given_not_a=0.01

Machine access

Sources

FAQ

Why is the posterior so low with a 99 % accurate test?

When A is rare, the many false positives among the non-A majority outnumber the true positives: with 0.5 % prevalence about two thirds of positive results are false. This is the base-rate effect.

Can I enter percentages?

No, all inputs are probabilities between 0 and 1 (0.05 for 5 %). The diagnostic-test calculator accepts percentages.

Related calculators