# Bayes' theorem

Template:Bayesian statistics In probability theory and statistics, Bayes' theorem (alternatively Bayes' law or Bayes' rule) relates current probability to prior probability. It is important in the mathematical manipulation of conditional probabilities. Bayes' rule can be derived from more basic axioms of probability, specifically conditional probability.

When applied, the probabilities involved in Bayes' theorem may have any of a number of probability interpretations. In one of these interpretations, the theorem is used directly as part of a particular approach to statistical inference. ln particular, with the Bayesian interpretation of probability, the theorem expresses how a subjective degree of belief should rationally change to account for evidence: this is Bayesian inference, which is fundamental to Bayesian statistics. However, Bayes's theorem has applications in a wide range of calculations involving probabilities, not just in Bayesian inference.

Bayes's theorem is named after Rev. Thomas Bayes (Template:IPAc-en; 1701–1761), who first showed how to use new evidence to update beliefs. Bayes' unpublished manuscript was significantly edited by Richard Price before it was posthumously read at the Royal Society. He edited Bayes's major work An Essay towards solving a Problem in the Doctrine of Chances (1763), which appeared in Philosophical Transactions, and contains Bayes' Theorem, one of the fundamental results of probability theory. Price wrote an introduction to the paper which provides some of the philosophical basis of Bayesian statistics. In 1765 he was elected a Fellow of the Royal Society in recognition of his work on the legacy of Bayes. It was further developed by Pierre-Simon Laplace, who first published the modern formulation in his 1812 Théorie analytique des probabilités.

Sir Harold Jeffreys put Bayes' algorithm and Laplace's formulation on an axiomatic basis. Jeffreys wrote that Bayes's theorem "is to the theory of probability what Pythagoras's theorem is to geometry".

## Statement of theorem

Bayes' theorem is stated mathematically as the following equation:

$P(A|B)={\frac {P(B|A)\,P(A)}{P(B)}}\cdot$ Here A and B are events,

## Introductory example

The entire output of a factory is produced on three machines. The three machines account for 20%, 30%, and 50% of the output, respectively. The fraction of defective items produced is this: for the first machine, 5%; for the second machine, 3%; for the third machine, 1%. If an item is chosen at random from the total output and is found to be defective, what is the probability that it was produced by the third machine?

A solution is as follows. Let Ai denote the event that a randomly chosen item was made by the ith machine (for i = 1,2,3). Let B denote the event that a randomly chosen item is defective. Then, we are given the following information:

P(A1) = 0.2,    P(A2) = 0.3,    P(A3) = 0.5.

If the item was made by machine A1, then the probability that it is defective is 0.05; that is, P(B|A1) = 0.05. Overall, we have

P(B|A1) = 0.05,    P(B|A2) = 0.03,    P(B|A3) = 0.01.

To answer the original question, we first find P(B). That may be done thusly:

P(B) = Σi P(B|Ai)P(Ai) = (0.05)(0.2) + (0.03)(0.3) + (0.01)(0.5) = 0.024.

Hence 2.4% of the total output of the factory is defective.

We are given that B has occurred, and we want to calculate the conditional probability of A3. By Bayes' theorem,

$P(A_{3}|B)={\frac {P(B|A_{3})P(A_{3})}{P(B)}}={\frac {(0.01)(0.50)}{0.024}}={\frac {5}{24}}$ .

Given that the item is defective, the probability that it was made by the third machine is only 5/24. Although machine 3 produces half of the total output, it produces a much smaller fraction of the defective items. Hence the knowledge that the item selected was defective enables us to replace the prior probability P(A3) = 1/2 by the smaller posterior probability P(A3|B) = 5/24.

## Interpretations A geometric visualisation of Bayes's theorem. In the table, the values ax, ay, bx and by give the relative weights of each corresponding condition and case. The figures denote the cells of the table involved in each metric, the probability being the fraction of each figure that is shaded. This shows that P(A|X) P(X) = P(X|A) P(A) i.e. P(A|X) = P(X|A) P(A) / P(X). Similar reasoning can be used to show that P(B|X) = P(X|B) P(B) / P(X) etc.

The interpretation of Bayes' theorem depends on the interpretation of probability ascribed to the terms. The two main interpretations are described below.

### Bayesian interpretation

In the Bayesian (or epistemological) interpretation, probability measures a degree of belief. Bayes's theorem then links the degree of belief in a proposition before and after accounting for evidence. For example, suppose it is believed with 50% certainty that a coin is twice as likely to land heads than tails. If the coin is flipped a number of times and the outcomes observed, that degree of belief may rise, fall or remain the same depending on the results.

For proposition A and evidence B,

• P(A), the prior, is the initial degree of belief in A.
• P(A|B), the posterior, is the degree of belief having accounted for B.
• the quotient P(B|A)/P(B) represents the support B provides for A.

For more on the application of Bayes's theorem under the Bayesian interpretation of probability, see Bayesian inference.

### Frequentist interpretation

In the frequentist interpretation, probability measures a proportion of outcomes. For example, suppose an experiment is performed many times. P(A) is the proportion of outcomes with property A, and P(B) that with property B. P(B|A) is the proportion of outcomes with property B out of outcomes with property A, and P(A|B) the proportion of those with A out of those with B.

The role of Bayes's theorem is best visualized with tree diagrams, as shown to the right. The two diagrams partition the same outcomes by A and B in opposite orders, to obtain the inverse probabilities. Bayes's theorem serves as the link between these different partitionings.

## Forms

### Events

#### Simple form

For events A and B, provided that P(B) ≠ 0,

$P(A|B)={\frac {P(B|A)\,P(A)}{P(B)}}\cdot \,$ In many applications, for instance in Bayesian inference, the event B is fixed in the discussion, and we wish to consider the impact of its having been observed on our belief in various possible events A. In such a situation the denominator of the last expression, the probability of the given evidence B, is fixed; what we want to vary is A. Bayes's theorem then shows that the posterior probabilities are proportional to the numerator:

$P(A|B)\propto P(A)\cdot P(B|A)\$ (proportionality over A for given B).

In words: posterior is proportional to prior times likelihood (see Lee, 2012, Chapter 1).

If events A1, A2, ..., are mutually exclusive and exhaustive, i.e., one of them is certain to occur but no two can occur together, and we know their probabilities up to proportionality, then we can determine the proportionality constant by using the fact that their probabilities must add up to one. For instance, for a given event A, the event A itself and its complement ¬A are exclusive and exhaustive. Denoting the constant of proportionality by c we have

$P(A|B)=c\cdot P(A)\cdot P(B|A)\$ and $P(\neg A|B)=c\cdot P(\neg A)\cdot P(B|\neg A)\cdot$ Adding these two formulas we deduce that

$c={\frac {1}{P(A)\cdot P(B|A)+P(\neg A)\cdot P(B|\neg A)}}.$ #### Alternative form

Another form of Bayes's Theorem that is generally encountered when looking at two competing statements or hypotheses is:

$P(A|B)={\frac {P(B|A)\,P(A)}{P(B|A)P(A)+P(B|\neg A)P(\neg A)}}\cdot$ For an epistemological interpretation:

For proposition A and evidence or background B,

• P(A),the prior probability, is the initial degree of belief in A.
• P(-A), is the corresponding probability of the initial degree of belief against A: 1-P(A)=P(-A)
• P(B|A), the conditional probability or likelihood, is the degree of belief in B, given that the proposition A is true.
• P(B|-A), the conditional probability or likelihood, is the degree of belief in B, given that the proposition A is false.
• P(A|B), the posterior probability, is the probability for A after taking into account B for and against A.

#### Extended form

Often, for some partition {Aj} of the event space, the event space is given or conceptualized in terms of P(Aj) and P(B|Aj). It is then useful to compute P(B) using the law of total probability:

$P(B)={\sum _{j}P(B|A_{j})P(A_{j})},$ $\implies P(A_{i}|B)={\frac {P(B|A_{i})\,P(A_{i})}{\sum \limits _{j}P(B|A_{j})\,P(A_{j})}}\cdot$ In the special case where A is a binary variable:

$P(A|B)={\frac {P(B|A)\,P(A)}{P(B|A)P(A)+P(B|\neg A)P(\neg A)}}\cdot$ ### Random variables Diagram illustrating the meaning of Bayes's theorem as applied to an event space generated by continuous random variables X and Y. Note that there exists an instance of Bayes's theorem for each point in the domain. In practice, these instances might be parametrized by writing the specified probability densities as a function of x and y.

Consider a sample space Ω generated by two random variables X and Y. In principle, Bayes's theorem applies to the events A = {X = x} and B = {Y = y}. However, terms become 0 at points where either variable has finite probability density. To remain useful, Bayes's theorem may be formulated in terms of the relevant densities (see Derivation).

#### Simple form

If X is continuous and Y is discrete,

$f_{X}(x|Y=y)={\frac {P(Y=y|X=x)\,f_{X}(x)}{P(Y=y)}}.$ If X is discrete and Y is continuous,

$P(X=x|Y=y)={\frac {f_{Y}(y|X=x)\,P(X=x)}{f_{Y}(y)}}.$ If both X and Y are continuous,

$f_{X}(x|Y=y)={\frac {f_{Y}(y|X=x)\,f_{X}(x)}{f_{Y}(y)}}.$ #### Extended form

A continuous event space is often conceptualized in terms of the numerator terms. It is then useful to eliminate the denominator using the law of total probability. For fY(y), this becomes an integral:

$f_{Y}(y)=\int _{-\infty }^{\infty }f_{Y}(y|X=\xi )\,f_{X}(\xi )\,d\xi .$ ### Bayes's rule

{{#invoke:main|main}} Bayes' rule is Bayes's theorem in odds form.

$O(A_{1}:A_{2}|B)=O(A_{1}:A_{2})\cdot \Lambda (A_{1}:A_{2}|B)$ where

$\Lambda (A_{1}:A_{2}|B)={\frac {P(B|A_{1})}{P(B|A_{2})}}$ is called the Bayes factor or likelihood ratio and the odds between two events is simply the ratio of the probabilities of the two events. Thus

$O(A_{1}:A_{2})={\frac {P(A_{1})}{P(A_{2})}}$ ,
$O(A_{1}:A_{2}|B)={\frac {P(A_{1}|B)}{P(A_{2}|B)}}$ ,

So the rule says that the posterior odds are the prior odds times the Bayes factor, or in other words, posterior is proportional to prior times likelihood.

## Derivation

### For events

Bayes's theorem may be derived from the definition of conditional probability:

$P(A|B)={\frac {P(A\cap B)}{P(B)}},{\text{ if }}P(B)\neq 0,\!$ $P(B|A)={\frac {P(A\cap B)}{P(A)}},{\text{ if }}P(A)\neq 0,\!$ $\Rightarrow P(A\cap B)=P(A|B)\,P(B)=P(B|A)\,P(A),\!$ $\Rightarrow P(A|B)={\frac {P(B|A)\,P(A)}{P(B)}},{\text{ if }}P(B)\neq 0.$ ### For random variables

For two continuous random variables X and Y, Bayes's theorem may be analogously derived from the definition of conditional density:

$f_{X}(x|Y=y)={\frac {f_{X,Y}(x,y)}{f_{Y}(y)}}$ $f_{Y}(y|X=x)={\frac {f_{X,Y}(x,y)}{f_{X}(x)}}$ $\Rightarrow f_{X}(x|Y=y)={\frac {f_{Y}(y|X=x)\,f_{X}(x)}{f_{Y}(y)}}.$ ## Examples

### Frequentist example Tree diagram illustrating frequentist example. R, C, P and P bar are the events representing rare, common, pattern and no pattern. Percentages in parentheses are calculated. Note that three independent values are given, so it is possible to calculate the inverse tree (see figure above).

An entomologist spots what might be a rare subspecies of beetle, due to the pattern on its back. In the rare subspecies, 98% have the pattern, or P(Pattern|Rare) = 98%. In the common subspecies, 5% have the pattern. The rare subspecies accounts for only 0.1% of the population. How likely is the beetle having the pattern to be rare, or what is P(Rare|Pattern)?

From the extended form of Bayes's theorem (since any beetle can be only rare or common),

### Drug testing Tree diagram illustrating drug testing example. U, U bar, "+" and "−" are the events representing user, non-user, positive result and negative result. Percentages in parentheses are calculated.

Suppose a drug test is 99% sensitive and 99% specific. That is, the test will produce 99% true positive results for drug users and 99% true negative results for non-drug users. Suppose that 0.5% of people are users of the drug. If a randomly selected individual tests positive, what is the probability he or she is a user?

{\begin{aligned}P({\text{User}}|{\text{+}})&={\frac {P({\text{+}}|{\text{User}})P({\text{User}})}{P({\text{+}}|{\text{User}})P({\text{User}})+P({\text{+}}|{\text{Non-user}})P({\text{Non-user}})}}\\[8pt]&={\frac {0.99\times 0.005}{0.99\times 0.005+0.01\times 0.995}}\\[8pt]&\approx 33.2\%\end{aligned}} Despite the apparent accuracy of the test, if an individual tests positive, it is more likely that they do not use the drug than that they do.

This surprising result arises because the number of non-users is very large compared to the number of users; thus the number of false positives (0.995%) outweighs the number of true positives (0.495%). To use concrete numbers, if 1000 individuals are tested, there are expected to be 995 non-users and 5 users. From the 995 non-users, 0.01 × 995 ≃ 10 false positives are expected. From the 5 users, 0.99 × 5 ≃ 5 true positives are expected. Out of 15 positive results, only 5, about 33%, are genuine.

Note: The importance of specificity can be illustrated by showing that even if sensitivity is 100% and specificity is at 99% the probability of the person being a drug user is ≈33% but if the specificity is changed to 99.5% and the sensitivity is dropped down to 99% the probability of the person being a drug user rises to 49.8%.

## History

Bayes's theorem was named after the Reverend Thomas Bayes (1701–61), who studied how to compute a distribution for the probability parameter of a binomial distribution (in modern terminology). His friend Richard Price edited and presented this work in 1763, after Bayes's death, as An Essay towards solving a Problem in the Doctrine of Chances. The French mathematician Pierre-Simon Laplace reproduced and extended Bayes's results in 1774, apparently quite unaware of Bayes's work. Stephen Stigler suggested in 1983 that Bayes's theorem was discovered by Nicholas Saunderson some time before Bayes. However, this interpretation has been disputed.

Martyn Hooper and Sharon McGrayne have argued that Richard Price's contribution was substantial:

By modern standards, we should refer to the Bayes–Price rule. Price discovered Bayes's work, recognized its importance, corrected it, contributed to the article, and found a use for it. The modern convention of employing Bayes's name alone is unfair but so entrenched that anything else makes little sense.