BL2.ru

Law of total probability and Bayes' theorem calculator with a probability tree

Enter hypotheses H1…Hn with their probabilities and the probability of event A under each hypothesis. The calculator checks that the hypothesis probabilities sum to 1, finds P(A) by the law of total probability and P(Hi∣A) by Bayes' theorem, and shows the steps and a probability tree.

№HypothesisP(Hi)P(A∣Hi)
1
2
ΣP(Hi) = 0 %
Percent (30 or 30 %), decimals (0.3) and fractions (3/10 or 1/3) are accepted. The hypotheses must be mutually exclusive and exhaustive: the sum of P(Hi) is 1.

The law of total probability

Let hypotheses H1, H2, …, Hn be mutually exclusive and exhaustive (exactly one of them occurs), so P(H1) + … + P(Hn) = 1. Then the probability of an event A that can only happen together with one of the hypotheses is
P(A) = P(H1)·P(A∣H1) + P(H2)·P(A∣H2) + … + P(Hn)·P(A∣Hn).

In words: follow every branch of the tree that ends in A, multiply the probabilities along the branch, and add the results.

Bayes' theorem

If A has already happened, the probability that hypothesis Hi was the cause is
P(Hi∣A) = P(Hi)·P(A∣Hi) / P(A).

P(Hi) is the prior probability (before the observation) and P(Hi∣A) is the posterior (after learning that A occurred). The denominator is the total probability from the first formula, so all P(Hi∣A) add up to 1.

How to solve a problem

  1. List the hypotheses: "the part comes from factory 1", "the patient is sick". Check that they are mutually exclusive and sum to 1.
  2. Define the event A: "the part is defective", "the test is positive".
  3. Write down P(A∣Hi) for every hypothesis: the defect rate of the factory, the sensitivity of the test.
  4. If the question is "how likely is the event", use the law of total probability. If it is "how likely is hypothesis Hi given that the event happened", use Bayes' theorem.

Example: three factories and defects

Factory 1 makes 50% of the parts, factory 2 makes 30% and factory 3 makes 20%. The defect rates are 1%, 2% and 5%. A part is picked at random. The probability that it is defective is 0.5·0.01 + 0.3·0.02 + 0.2·0.05 = 0.005 + 0.006 + 0.01 = 0.021, that is 2.1%. If the part is defective, it comes from factory 3 with probability 0.01/0.021 ≈ 47.6%, although that factory makes only a fifth of the output: it produces the most defects.

The false positive paradox

1% of people have a disease. The test detects the disease in 99% of sick people (sensitivity) and correctly returns negative for 95% of healthy people (specificity). A person tests positive. How likely is it that they are sick? Intuition says about 95%, but Bayes' theorem gives only about 16.7%.

Count it out for 10,000 people. 100 are sick and the test finds 99 of them. 9,900 are healthy, and 5% of them test positive by mistake: 495 people. That makes 99 + 495 = 594 positive results, and only 99 of those are truly sick, so 99/594 ≈ 16.7%. Healthy people are so numerous that even a small false positive rate yields more positive results than there are true patients. This is why one positive screening for a rare disease calls for a retest, not a diagnosis. If the person has symptoms or belongs to a risk group, the prior is higher and the same test is far more convincing.

What the calculator shows

  • A check that the hypothesis probabilities sum to 1, plus a "Complete to 1" button for the last hypothesis.
  • P(A) and every P(Hi∣A) with a step-by-step solution and a table.
  • A probability tree with P(Hi), P(A∣Hi) and 1 − P(A∣Hi) written on the branches.
  • A link to the calculation: the data is stored in the address, so it is easy to share.

FAQ

What is the difference between the law of total probability and Bayes' theorem?
Total probability answers "how often does event A happen overall", going from causes to effects. Bayes' theorem goes the other way: the event has already happened and we find the probability of each cause.
What if the hypothesis probabilities do not sum to 1?
Either a possible hypothesis is missing or there is a data error. Add the missing hypothesis, for example "other", and use the "Complete to 1" button.
Why is the probability of disease low after a positive result of an accurate test?
Because the disease is rare: healthy people are far more numerous, and their false positives outnumber the true ones. This is the false positive paradox, explained in the article below.
Can I enter probabilities as fractions?
Yes. Fractions such as 1/3, decimals such as 0.25 and percentages such as 25% are all accepted. In Percent mode a bare number is read as a percentage; in Decimals and fractions mode it is read as a share of one.
How many hypotheses can I enter?
From two to ten. That covers school and university problems; the tree is drawn for any number of hypotheses.