# 12.4: Bayes Theorem - Mathematics

In this section we concentrate on the more complex conditional probability problems we began looking at in the last section.

Example 19

Suppose a certain disease has an incidence rate of 0.1% (that is, it afflicts 0.1% of the population). A test has been devised to detect this disease. The test does not produce false negatives (that is, anyone who has the disease will test positive for it), but the false positive rate is 5% (that is, about 5% of people who take the test will test positive, even though they do not have the disease). Suppose a randomly selected person takes the test and tests positive. What is the probability that this person actually has the disease?

Solution

There are two ways to approach the solution to this problem. One involves an important result in probability theory called Bayes' theorem. We will discuss this theorem a bit later, but for now we will use an alternative and, we hope, much more intuitive approach.

Let's break down the information in the problem piece by piece.

Suppose a certain disease has an incidence rate of 0.1% (that is, it afflicts 0.1% of the population). The percentage 0.1% can be converted to a decimal number by moving the decimal place two places to the left, to get 0.001. In turn, 0.001 can be rewritten as a fraction: 1/1000. This tells us that about 1 in every 1000 people has the disease. (If we wanted we could write P(disease)=0.001.)

A test has been devised to detect this disease. The test does not produce false negatives (that is, anyone who has the disease will test positive for it). This part is fairly straightforward: everyone who has the disease will test positive, or alternatively everyone who tests negative does not have the disease. (We could also say P(positive | disease)=1.)

The false positive rate is 5% (that is, about 5% of people who take the test will test positive, even though they do not have the disease). This is even more straightforward. Another way of looking at it is that of every 100 people who are tested and do not have the disease, 5 will test positive even though they do not have the disease. (We could also say that (P)(positive | no disease)=0.05.)

Suppose a randomly selected person takes the test and tests positive. What is the probability that this person actually has the disease? Here we want to compute (P)(disease|positive). We already know that (P)(positive|disease)=1, but remember that conditional probabilities are not equal if the conditions are switched.

Rather than thinking in terms of all these probabilities we have developed, let's create a hypothetical situation and apply the facts as set out above. First, suppose we randomly select 1000 people and administer the test. How many do we expect to have the disease? Since about 1/1000 of all people are afflicted with the disease, (frac{1}{1000}) of 1000 people is 1. (Now you know why we chose 1000.) Only 1 of 1000 test subjects actually has the disease; the other 999 do not.

We also know that 5% of all people who do not have the disease will test positive. There are 999 disease-free people, so we would expect ((0.05)(999)=49.95) (so, about 50) people to test positive who do not have the disease.

Now back to the original question, computing P(disease|positive). There are 51 people who test positive in our example (the one unfortunate person who actually has the disease, plus the 50 people who tested positive but don't). Only one of these people has the disease, so

P(disease | positive) (approx frac{1}{51} approx 0.0196)

or less than 2%. Does this surprise you? This means that of all people who test positive, over 98% do not have the disease.

The answer we got was slightly approximate, since we rounded 49.95 to 50. We could redo the problem with 100,000 test subjects, 100 of whom would have the disease and ((0.05)(99,900)=4995) test positive but do not have the disease, so the exact probability of having the disease if you test positive is

P(disease | positive) (approx frac{100}{5095} approx 0.0196)

which is pretty much the same answer.

But back to the surprising result. Of all people who test positive, over 98% do not have the disease. If your guess for the probability a person who tests positive has the disease was wildly different from the right answer (2%), don't feel bad. The exact same problem was posed to doctors and medical students at the Harvard Medical School 25 years ago and the results revealed in a 1978 New England Journal of Medicine article. Only about 18% of the participants got the right answer. Most of the rest thought the answer was closer to 95% (perhaps they were misled by the false positive rate of 5%).

So at least you should feel a little better that a bunch of doctors didn't get the right answer either (assuming you thought the answer was much higher). But the significance of this finding and similar results from other studies in the intervening years lies not in making math students feel better but in the possibly catastrophic consequences it might have for patient care. If a doctor thinks the chances that a positive test result nearly guarantees that a patient has a disease, they might begin an unnecessary and possibly harmful treatment regimen on a healthy patient. Or worse, as in the early days of the AIDS crisis when being HIV-positive was often equated with a death sentence, the patient might take a drastic action and commit suicide.

As we have seen in this hypothetical example, the most responsible course of action for treating a patient who tests positive would be to counsel the patient that they most likely do not have the disease and to order further, more reliable, tests to verify the diagnosis.

One of the reasons that the doctors and medical students in the study did so poorly is that such problems, when presented in the types of statistics courses that medical students often take, are solved by use of Bayes' theorem, which is stated as follows:

Bayes’ Theorem

(P(A | B)=frac{P(A) P(B | A)}{P(A) P(B | A)+P(ar{A}) P(B | ar{A})})

In our earlier example, this translates to

(P( ext { disease } | ext { positive })=frac{P( ext { disease }) P( ext { positive } | ext { disease })}{P( ext { disease }) P( ext { positive } | ext { disease })+P( ext { no disease }) P( ext { positive } | ext { no disease })})

Plugging in the numbers gives

(P( ext { disease } | ext { positive })=frac{(0.001)(1)}{(0.001)(1)+(0.999)(0.05)} approx 0.0196)

which is exactly the same answer as our original solution.

The problem is that you (or the typical medical student, or even the typical math professor) are much more likely to be able to remember the original solution than to remember Bayes' theorem. Psychologists, such as Gerd Gigerenzer, author of Calculated Risks: How to Know When Numbers Deceive You, have advocated that the method involved in the original solution (which Gigerenzer calls the method of "natural frequencies") be employed in place of Bayes' Theorem. Gigerenzer performed a study and found that those educated in the natural frequency method were able to recall it far longer than those who were taught Bayes' theorem. When one considers the possible life-and-death consequences associated with such calculations it seems wise to heed his advice.

Example 20

A certain disease has an incidence rate of 2%. If the false negative rate is 10% and the false positive rate is 1%, compute the probability that a person who tests positive actually has the disease.

Solution

Imagine 10,000 people who are tested. Of these 10,000, 200 will have the disease; 10% of them, or 20, will test negative and the remaining 180 will test positive. Of the 9800 who do not have the disease, 98 will test positive. So of the 278 total people who test positive, 180 will have the disease. Thus

(P( ext { disease } | ext { positive })=frac{180}{278} approx 0.647)

so about 65% of the people who test positive will have the disease.

Using Bayes theorem directly would give the same result:

(P( ext { disease } | ext { positive })=frac{(0.02)(0.90)}{(0.02)(0.90)+(0.98)(0.01)}=frac{0.018}{0.0278} approx 0.647)

Try it Now 5

A certain disease has an incidence rate of 0.5%. If there are no false negatives and if the false positive rate is 3%, compute the probability that a person who tests positive actually has the disease.

Out of 100,000 people, 500 would have the disease. Of those, all 500 would test positive. Of the 99,500 without the disease, 2,985 would falsely test positive and the other 96,515 would test negative.

(mathrm{P}( ext { disease } | ext { positive })=frac{500}{500+2985}=frac{500}{3485} approx 14.3 \%)

## Model Comparison and Hierarchical Modeling

### 10.6 Extreme sensitivity to prior distribution

In many realistic applications of Bayesian model comparison , the theoretical emphasis is on the difference between the models' likelihood functions. For example, one theory predicts planetary motions based on elliptical orbits around the sun, and another theory predicts planetary motions based on circular cycles and epicycles around the earth. The two models involve very different parameters. In these sorts of models, the form of the prior distribution on the parameters is not a focus, and is often an afterthought. But, when doing Bayesian model comparison, the form of the prior is crucial because the Bayes factor integrates the likelihood function weighted by the prior distribution.

As we have seen repeatedly, Bayesian model comparison involves marginalizing across the prior distribution in each model. Therefore, the posterior probabilities of the models, and the Bayes factors, can be extremely sensitive to the choice of prior distribution. If the prior distribution happens to place a lot of probability mass where the likelihood distribution peaks, then the marginal likelihood (i.e., p(D|m)) will be large. But if the prior distribution happens to place little probability mass where the likelihood distribution is, then the marginal likelihood will be small. The sensitivity of Bayes factors to prior distributions is well known in the literature (e.g., Kass & Raftery, 1995 Liu & Aitkin, 2008 Vanpaemel, 2010 ).

When doing Bayesian model comparison, different forms of vague priors can yield very different Bayes factors. As an example, consider again the must-be-fair versus anything's-possible models of the previous section. The must-be-fair model was characterized as a beta prior with shape parameters of a = 500 and b = 500 (i.e., mode ω = 0.5 and concentration κ = 1000). The anything's-possible model was defined as a beta prior with shape parameters of a = 1 and b = 1. Suppose we have data with z = 65 and N = 100. Then the Bayes factor is

> z=65 N=100 pD(z,N,a=500,b=500)/pD(z,N,a=1,b=1)

This means that the anything's-possible model is favored. But why did we choose those particular shape parameter values for the anything's-possible model? It was merely that intuition suggested a uniform distribution. On the contrary, many mathematical statisticians recommend a different form of prior to make it uninformative according to a particular mathematical criterion ( Lee & Webb, 2005 Zhu & Lu, 2004 ). The recommended prior is the so-called Haldane prior, which uses shape constants that are very close to zero, such as a = b = 0.01. (See Figure 6.1 , p. 128, for an example of a beta distribution with shape parameters less than 1.) Using a Haldane prior to express the anything's-possible model, the Bayes factor is

> z=65 N=100 pD(z,N,a=500,b=500)/pD(z,N,a=0.01,b=0.01)

This means that the must-be-fair model is favored. Notice that we reversed the Bayes factor merely by changing from a “vague” beta(θ|1, 1) prior to a “vague” beta(θ|.01, .01) prior.

Unlike Bayesian model comparison, when doing Bayesian estimation of continuous parameters within models and using realistically large amounts of data, the posterior distribution on the continuous parameters is typically robust against changes in vague priors. It does not matter if the prior is extremely vague or only a little vague (and yes, what I meant by “extremely vague” and “only a little vague” is vague, but the point is that it doesn't matter).

As an example, consider the two versions of the anything's-possible model, using either a “vague” beta(θ|1,1) prior or a “vague” beta(θ.01,.01) prior. Using the data z = 65 and N = 100, we can compute the posterior distribution on θ. Starting with the beta (θ|1, 1) yields a beta(θ|66, 36) posterior, which has a 95% HDI from 0.554 to 0.738. (The HDI was computed by using the HDIofICDF function that comes with the utilities program accompanying this book.) Starting with the beta(θ.01,.01) yields a beta(θ|65.01,35.01) posterior, which has a 95% HDI from 0.556 to 0.742. The HDIs are virtually identical. In particular, for either prior, the posterior distribution rules out θ = 0.5, which is to say that the must-be-fair hypothesis is not among the credible values. For additional discussion and related examples, see Kruschke (2011a) and Section 12.2 of this book.

#### 10.6.1 Priors of different models should be equally informed

We have established that seemingly innocuous changes in the vagueness of a vague prior can dramatically change a model's marginal likelihood, and hence its Bayes factor in comparison with other models. What can be done to ameliorate the problem? One useful approach is to inform the priors of all models with a small set of representative data (the same for all models). The idea is that even a small set of data overwhelms any vague prior, resulting in a new distribution of parameters that is at least “in the ballpark” of reasonable parameter values for that model. This puts the models on an equal playing field going into the model comparison.

Where do the data come from, that will act as the small representative set for informing the priors of the models? They could come from previous research. They could be fictional but representative of previous research, as long as the audience of the analysis agrees that the fictional data are valid. Or, the data could be a small percentage of the data from the research at hand. For example, a random 10% of the data could inform the priors of the models, and the remaining 90% used for computing the Bayes factor in the model comparison. In any case, the data used for informing the priors should be representative of real data and large enough in quantity to usefully overwhelm any reasonable vague prior. Exactly what that means will depend on the details of the model, but the following simple example illustrates the idea.

Recall, from the previous section, the comparison of the must-be-fair model and the anything's-possible model. When z = 65 with N = 100, the Bayes factor changed dramatically depending on whether the “vague” anything's-possible model used a beta(θ|1,1) prior or a beta(θ.01,.01) prior. Now let's compute the Bayes factors after informing both models with just 10% of the data. Suppose that the 10% subset has 6 heads in 10 flips, so the remaining 90% of the data has z = 65 − 6 and N = 100 − 10.

Suppose we start with beta(θ|1,1) for the prior of the anything's-possible model. We inform it, and the must-be-fair model, with the 10% subset. Therefore, the anything's possible model becomes a beta(θ|1 + 6,1 +10 − 6) prior, and the must-be-fair model becomes a beta(θ|500 + 6, 500 + 10 — 6) prior. The Bayes factor is

> z=65-6 N=100-10 pD(z,N,a=500+6,b=500+10-6)/pD(z,N,a=1+6,b=1+10-6)

Now let's instead start with beta(θ|.01, .01) for the anything's-possible model. The Bayes factor using the weakly informed priors is

> z=65-6 N=100-10 pD(z,N,a=500+6,b=500+10-6)/pD(z,N,a=0.01+6,b=0.01+10-6)

Thus, the Bayes factor has barely changed at all. With the two models equally informed by a small amount of representative data, the Bayes factor is stable.

The idea of using a small amount of training data to inform the priors for model comparison has been discussed at length in the literature and is an active research topic. A selective overview was provided by J. O. Berger and Pericchi (2001) , who discussed conventional default priors (e.g., Jeffreys, 1961 ), “intrinsic” Bayes factors (e.g., J. O. Berger & Pericchi, 1996 ), and “fractional” Bayes factors (e.g., O'Hagan, 1995, 1997 ), among others.

## Bayesian Statistics

### Proof of Bayes's Theorem and Its Extension

Bayes's Theorem is easily proven by observing that:

Given that p(A, B) is equivalent to p(B, A), the left-hand sides of Eqs. (3) and (5) can be set equal, and we obtain:

Dividing both sides by p(A) yields Bayes's theorem. In this representation the theorem is unquestionable. However, Bayesian statisticians replace B with “parameter” or “hypothesis” and A with “data” so that the theorem appears as:

The denominator of the right-hand side of this equation is the marginal probability of the data (often called a normalizing constant), which is an average of the probability of the data under all possible parameter values (θ). In a continuous parameter space (S):

In a discrete parameter space, the marginal probability of the data is:

Since the denominator typically does not provide us with any information about the parameter, Bayes's theorem is often reduced to:

In Bayesian language, this expression says that the posterior probability for a parameter is proportional to the likelihood function for the data (or the sampling density for the data) multiplied by the prior probability for the parameter. The posterior is so called because it is our estimate of the probability for the parameter after having observed additional data the prior is so called because it represents our belief about the probability for the parameter before observing data.

To provide an example of the applicability of Bayes's theorem, I demonstrate the theorem on data about prostate cancer. Suppose a 30-year-old male tests positive on the standard test for prostate cancer. Suppose also that the test has a 90% accuracy rate for people in that age group, meaning that it will give a positive test result to positive cases 90% of the time. Suppose, however, the test also produces false positive results 10% of the time that is, among noncases 10% will receive a positive test. Obviously, the question of interest is whether, given the positive test result, the individual in fact has prostate cancer, which can be expressed as p(p.c.∣ test+). However, we know p(test + ∣ p.c.), and we can obtain prostate cancer incidence rates for age 30, p(p.c.) (here, I use an approximate rate for people under 45). We can substitute the known information into Bayes's formula:

In this case, we have the following:

Simplifying reveals that the actual (posterior) probability of having cancer at age 30, given a positive test, is 0.0001. Certainly our posterior probability for having cancer is greater than our prior probability, but this posterior probability is still quite small, revealing the shortcoming of a test with a modest false positive rate applied to a low-risk population.

## Contents

Bayes' theorem is stated mathematically as the following equation: 

### Proof Edit

#### For events Edit

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

where P ( A ∩ B ) is the joint probability of both A and B being true. Because

#### For continuous random variables Edit

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

### Drug testing Edit

Suppose, a particular test for whether someone has been using cannabis is 90% sensitive, meaning the true positive rate (TPR)=0.90. Therefore it leads to 90% true positive results (correct identification of drug use) for cannabis users.

The test is also 80% specific, meaning true negative rate (TNR)=0.80. Therefore the test correctly identifies 80% of non-use for non-users, but also generates 20% false positives, or false positive rate (FPR)=0.20, for non-users.

Assuming 0.05 prevalence, meaning 5% of people use cannabis, what is the probability that a random person who tests positive is really a cannabis user?

The Positive predictive value (PPV) of a test is the proportion of persons who are actually positive out of all those testing positive, and can be calculated from a sample as:

PPV = True positive / Tested positive

The fact that P ( Positive ) = P ( Positive ∣ User ) P ( User ) + P ( Positive ∣ Non-user ) P ( Non-user ) >)=P(< ext>mid < ext>)P(< ext>)+P(< ext>mid < ext>)P(< ext>)> is a direct application of the Law of Total Probability. In this case, it says that the probability that someone tests positive is the probability that a user tests positive, times the probability of being a user, plus the probability that a non-user tests positive, times the probability of being a non-user.

This is true because the classifications user and non-user form a partition of a set, namely the set of people who take the drug test. This combined with the definition of conditional probability results in the above statement.

Even if someone tests positive, the probability they are a cannabis user is only 19%, because in this group only 5% of people are users, most positives are false positives coming from the remaining 95%.

If 1,000 people were tested:

• 950 are non-users and 190 of them give false positive (0.20 × 950)
• 50 of them are users and 45 of them give true positive (0.90 × 50)

The 1,000 people thus yields 235 positive tests, of which only 45 are genuine drug users, about 19%. See Figure 1 for an illustration using a frequency box, and note how small the pink area of true positives is compared to the blue area of false positives.

#### Sensitivity or specificity Edit

The importance of specificity can be seen by showing that even if sensitivity is raised to 100% and specificity remains at 80%, the probability of someone testing positive really being a cannabis user only rises from 19% to 21%, but if the sensitivity is held at 90% and the specificity is increased to 95%, the probability rises to 49%.

### Cancer rate Edit

Even if 100% of patients with pancreatic cancer have a certain symptom, when someone has the same symptom, it does not mean that this person has a 100% chance of getting pancreatic cancer. Assume the incidence rate of pancreatic cancer is 1/100000, while 10/100000 healthy individuals have the same symptoms worldwide, the probability of having pancreatic cancer given the symptoms is only 9.1%, and the other 90.9% could be "false positives" (that is, falsely said to have cancer "positive" is a confusing term when, as here, the test gives bad news).

Based on incidence rate, the following table presents the corresponding numbers per 100,000 people.

Which can then be used to calculate the probability of having cancer when you have the symptoms:

### Defective item rate Edit

A factory produces an item using three machines—A, B, and C—which account for 20%, 30%, and 50% of its output, respectively. Of the items produced by machine A, 5% are defective similarly, 3% of machine B's items and 1% of machine C's are defective. If a randomly selected item is defective, what is the probability it was produced by machine C?

Once again, the answer can be reached without using the formula by applying the conditions to a hypothetical number of cases. For example, if the factory produces 1,000 items, 200 will be produced by Machine A, 300 by Machine B, and 500 by Machine C. Machine A will produce 5% × 200 = 10 defective items, Machine B 3% × 300 = 9, and Machine C 1% × 500 = 5, for a total of 24. Thus, the likelihood that a randomly selected defective item was produced by machine C is 5/24 (

This problem can also be solved using Bayes' theorem: Let Xi denote the event that a randomly chosen item was made by the i th machine (for i = A,B,C). Let Y denote the event that a randomly chosen item is defective. Then, we are given the following information:

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

To answer the original question, we first find P(Y). That can be done in the following way:

Hence, 2.4% of the total output is defective.

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

Given that the item is defective, the probability that it was made by machine C is 5/24. Although machine C 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(XC) = 1/2 by the smaller posterior probability P(XC | Y) = 5/24.

The interpretation of Bayes' rule depends on the interpretation of probability ascribed to the terms. The two main interpretations are described below. Figure 2 shows a geometric visualization similar to Figure 1. Gerd Gigerenzer and co-authors have pushed hard for teaching Bayes Rule this way, with special emphasis on teaching it to physicians.  An example is Will Kurt's webpage, "Bayes' Theorem with Lego," later turned into the book, Bayesian Statistics the Fun Way: Understanding Statistics and Probability with Star Wars, LEGO, and Rubber Ducks. Zhu and Gigerenzer found in 2006 that whereas 0% of 4th, 5th, and 6th-graders could solve word problems after being taught with formulas, 19%, 39%, and 53% could after being taught with frequency boxes, and that the learning was either thorough or zero. 

### Bayesian interpretation Edit

In the Bayesian (or epistemological) interpretation, probability measures a "degree of belief". Bayes' theorem 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 will probably rise or fall, but might even 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 after incorporating news that B is true.
• the quotient P(B | A) / P(B) represents the support B provides for A.

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

### Frequentist interpretation Edit

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 (the prior) and P(B) is the proportion with property B. P(B | A) is the proportion of outcomes with property B out of outcomes with property A, and P(A | B) is the proportion of those with A out of those with B (the posterior).

The role of Bayes' theorem is best visualized with tree diagrams such as Figure 3. The two diagrams partition the same outcomes by A and B in opposite orders, to obtain the inverse probabilities. Bayes' theorem links the different partitionings.

#### Example Edit

An entomologist spots what might, due to the pattern on its back, be a rare subspecies of beetle. A full 98% of the members of the rare subspecies have the pattern, so P(Pattern | Rare) = 98%. Only 5% of members of the common subspecies have the pattern. The rare subspecies is 0.1% of the total population. How likely is the beetle having the pattern to be rare: what is P(Rare | Pattern)?

From the extended form of Bayes' theorem (since any beetle is either rare or common),

### Events Edit

#### Simple form Edit

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

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' theorem then shows that the posterior probabilities are proportional to the numerator, so the last equation becomes:

In words, the posterior is proportional to the prior times the likelihood. 

If events A1, A2, . are mutually exclusive and exhaustive, i.e., one of them is certain to occur but no two can occur together, 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

Adding these two formulas we deduce that

1 = c ⋅ ( P ( B | A ) ⋅ P ( A ) + P ( B | ¬ A ) ⋅ P ( ¬ A ) ) ,

#### Alternative form Edit

Another form of Bayes' theorem for two competing statements or hypotheses is:

For an epistemological interpretation:

For proposition A and evidence or background B, 

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

#### Extended form Edit

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

In the special case where A is a binary variable:

### Random variables Edit

Consider a sample space Ω generated by two random variables X and Y. In principle, Bayes' 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' theorem must be formulated in terms of the relevant densities (see Derivation).

#### Simple form Edit

If X is continuous and Y is discrete,

If X is discrete and Y is continuous,

If both X and Y are continuous,

#### Extended form Edit

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:

### Bayes' rule Edit

is called the Bayes factor or likelihood ratio. The odds between two events is simply the ratio of the probabilities of the two events. Thus

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

### Propositional logic Edit

Bayes' theorem represents a generalisation of contraposition which in propositional logic can be expressed as:

The corresponding formula in terms of probability calculus is Bayes' theorem which in its expanded form is expressed as:

### Subjective logic Edit

Bayes' theorem represents a special case of conditional inversion in subjective logic expressed as:

¬ B S ) = ( ω B ∣ A S , ω B ∣ ¬ A S ) ϕ

Hence, the subjective Bayes' theorem represents a generalization of Bayes' theorem. 

### Conditioned version Edit

A conditioned version of the Bayes' theorem  results from the addition of a third event C on which all probabilities are conditioned:

#### Derivation Edit

P ( A ∩ B ∩ C ) = P ( A ∣ B ∩ C ) P ( B ∣ C ) P ( C )

P ( A ∩ B ∩ C ) = P ( B ∩ A ∩ C ) = P ( B ∣ A ∩ C ) P ( A ∣ C ) P ( C )

The desired result is obtained by identifying both expressions and solving for P ( A ∣ B ∩ C ) .

### Bayes' rule with 3 events Edit

In the case of 3 events - A, B, and C - it can be shown that:

Bayes' theorem is named after the Reverend Thomas Bayes ( / b eɪ z / c. 1701 – 1761), who first used conditional probability to provide an algorithm (his Proposition 9) that uses evidence to calculate limits on an unknown parameter, published as An Essay towards solving a Problem in the Doctrine of Chances (1763). He studied how to compute a distribution for the probability parameter of a binomial distribution (in modern terminology). On Bayes' death his family transferred his papers to his old friend, Richard Price (1723 – 1791) who over a period of two years significantly edited the unpublished manuscript, before sending it to a friend who read it aloud at the Royal Society on 23 December 1763.  [ page needed ] Price 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. Price wrote an introduction to the paper which provides some of the philosophical basis of Bayesian statistics and chose one of the two solutions offered by Bayes. In 1765, Price was elected a Fellow of the Royal Society in recognition of his work on the legacy of Bayes.   On 27 April a letter sent to his friend Benjamin Franklin was read out at the Royal Society, and later published, where Price applies this work to population and computing 'life-annuities'. 

Independently of Bayes, Pierre-Simon Laplace in 1774, and later in his 1812 Théorie analytique des probabilités, used conditional probability to formulate the relation of an updated posterior probability from a prior probability, given evidence. He reproduced and extended Bayes's results in 1774, apparently unaware of Bayes's work. [note 1]  The Bayesian interpretation of probability was developed mainly by Laplace. 

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

Stephen Stigler used a Bayesian argument to conclude that Bayes' theorem was discovered by Nicholas Saunderson, a blind English mathematician, some time before Bayes   that interpretation, however, 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. 

In genetics, Bayes' theorem can be used to calculate the probability of an individual having a specific genotype. Many people seek to approximate their chances of being affected by a genetic disease or their likelihood of being a carrier for a recessive gene of interest. A Bayesian analysis can be done based on family history or genetic testing, in order to predict whether an individual will develop a disease or pass one on to their children. Genetic testing and prediction is a common practice among couples who plan to have children but are concerned that they may both be carriers for a disease, especially within communities with low genetic variance. [ citation needed ]

The first step in Bayesian analysis for genetics is to propose mutually exclusive hypotheses: for a specific allele, an individual either is or is not a carrier. Next, four probabilities are calculated: Prior Probability (the likelihood of each hypothesis considering information such as family history or predictions based on Mendelian Inheritance), Conditional Probability (of a certain outcome), Joint Probability (product of the first two), and Posterior Probability (a weighted product calculated by dividing the Joint Probability for each hypothesis by the sum of both joint probabilities). This type of analysis can be done based purely on family history of a condition or in concert with genetic testing. [ citation needed ]

### Using pedigree to calculate probabilities Edit

Hypothesis Hypothesis 1: Patient is a carrier Hypothesis 2: Patient is not a carrier
Prior Probability 1/2 1/2
Conditional Probability that all four offspring will be unaffected (1/2) · (1/2) · (1/2) · (1/2) = 1/16 About 1
Joint Probability (1/2) · (1/16) = 1/32 (1/2) · 1 = 1/2
Posterior Probability (1/32) / (1/32 + 1/2) = 1/17 (1/2) / (1/32 + 1/2) = 16/17

Example of a Bayesian analysis table for a female individual's risk for a disease based on the knowledge that the disease is present in her siblings but not in her parents or any of her four children. Based solely on the status of the subject’s siblings and parents, she is equally likely to be a carrier as to be a non-carrier (this likelihood is denoted by the Prior Hypothesis). However, the probability that the subject’s four sons would all be unaffected is 1/16 (½·½·½·½) if she is a carrier, about 1 if she is a non-carrier (this is the Conditional Probability). The Joint Probability reconciles these two predictions by multiplying them together. The last line (the Posterior Probability) is calculated by dividing the Joint Probability for each hypothesis by the sum of both joint probabilities. 

### Using genetic test results Edit

Parental genetic testing can detect around 90% of known disease alleles in parents that can lead to carrier or affected status in their child. Cystic fibrosis is a heritable disease caused by an autosomal recessive mutation on the CFTR gene,  located on the q arm of chromosome 7. 

Bayesian analysis of a female patient with a family history of cystic fibrosis (CF), who has tested negative for CF, demonstrating how this method was used to determine her risk of having a child born with CF:

Because the patient is unaffected, she is either homozygous for the wild-type allele, or heterozygous. To establish prior probabilities, a Punnett square is used, based on the knowledge that neither parent was affected by the disease but both could have been carriers:

Homozygous for the wild-
type allele (a non-carrier)

Heterozygous (a CF carrier)

Homozygous for the wild-
type allele (a non-carrier)

Heterozygous (a CF carrier)

(affected by cystic fibrosis)

Given that the patient is unaffected, there are only three possibilities. Within these three, there are two scenarios in which the patient carries the mutant allele. Thus the prior probabilities are ⅔ and ⅓.

Next, the patient undergoes genetic testing and tests negative for cystic fibrosis. This test has a 90% detection rate, so the conditional probabilities of a negative test are 1/10 and 1. Finally, the joint and posterior probabilities are calculated as before.

Hypothesis Hypothesis 1: Patient is a carrier Hypothesis 2: Patient is not a carrier
Prior Probability 2/3 1/3
Conditional Probability of a negative test 1/10 1
Joint Probability 1/15 1/3
Posterior Probability 1/6 5/6

After carrying out the same analysis on the patient’s male partner (with a negative test result), the chances of their child being affected is equal to the product of the parents' respective posterior probabilities for being carriers times the chances that two carriers will produce an affected offspring (¼).

### Genetic testing done in parallel with other risk factor identification. Edit

Bayesian analysis can be done using phenotypic information associated with a genetic condition, and when combined with genetic testing this analysis becomes much more complicated. Cystic Fibrosis, for example, can be identified in a fetus through an ultrasound looking for an echogenic bowel, meaning one that appears brighter than normal on a scan2. This is not a foolproof test, as an echogenic bowel can be present in a perfectly healthy fetus. Parental genetic testing is very influential in this case, where a phenotypic facet can be overly influential in probability calculation. In the case of a fetus with an echogenic bowel, with a mother who has been tested and is known to be a CF carrier, the posterior probability that the fetus actually has the disease is very high (0.64). However, once the father has tested negative for CF, the posterior probability drops significantly (to 0.16). 

Risk factor calculation is a powerful tool in genetic counseling and reproductive planning, but it cannot be treated as the only important factor to consider. As above, incomplete testing can yield falsely high probability of carrier status, and testing can be financially inaccessible or unfeasible when a parent is not present.

## 8.3 Bayes’ Long Theorem

We had to apply the Law of Total Probability first, before we could solve the taxicab problem with Bayes’ theorem, to calculate the denominator. This is so common that you’ll often see Bayes’ theorem written with this calculation built in. That is, the denominator (p(B)) is expanded out using the Law of Total Probability.

Bayes’ Theorem (long version)

Notice how there’s some repetition in the numerator and the denominator. The term (p(A)p(B given A)) appears both above and below. So, when you’re doing a calculation with this formula, you can just do that bit once and then copy it in both the top and bottom. Then you just have to do the bottom-right term to complete the formula. Figure 8.5: A tree diagram for the long form of Bayes’ theorem. The definition of conditional probability tells us (p(A given B)) is the first leaf divided by the sum of the first and third leaves.

A tree diagram helps illuminate the long version of Bayes’ theorem. To calculate (p(A given B)) , the definition of conditional probability directs us to calculate (p(A wedge B)) and (p(B)) : [ p(A given B) = frac< p(A wedge B) >< p(B) >. ] Looking at the tree diagram in Figure 8.5, we see that this amounts to computing the first leaf for the numerator, and the sum of the first and third leaves for the denominator. Which yields the same formula as in the long form of Bayes’ theorem.

## Applications

You have a disease test, and the probability that you will get a positive test result given that you have the disease is really, really high in other words the test has a very high accuracy rate. The problem is that there is a probability that you will get a positive test result even if you do not have the disease. And that you can simply calculate from Bayes law. The big point is, is that these probabilities are not the same as the probability that you will get a positive result given the disease is not the same as the probability that you will have the disease given a positive result.

These are two different probability distributions. And what makes them so different is the probability of disease and the probability of a positive test result. So if the disease is rare, the probability of disease will be very, very small.

Disease testing: A = Have disease, B = Tested positive.

## Total Probability & Bayes’ Theorem

Next we derive the Law of Total Probability and Bayes’ theorem.

[ p(A) = p(A given B)p(B) + p(A given eg B)p( eg B). ]

Notice, the last line of this proof only makes sense if (p(B) > 0) and (p( eg B) > 0) . That’s the same as (0 < p(B) < 1) , which is why the theorem begins with the condition: “If (0 < p(B) < 1) …”.

Now for the first version of Bayes’ theorem:

And next the long version:

Bayes’ Theorem (long version)

## 12.4: Bayes Theorem - Mathematics

Bayes’ Theorem with Conditional Probability Understanding of probability is must for a data science professional. Solutions to many data science problems are often probabilistic in nature. Hence, a better understanding of probability will help you understand & implement these algorithms more efficiently.

In this article, I will focus on conditional probability. For beginners in probability, I would strongly recommend that you go through this article before proceeding further.

A predictive model can easily be understood as a statement of conditional probability. For example, the probability of a customer from segment A buying a product of category Z in next 10 days is 0.80. In other words, the probability of a customer buying product from Category Z, given that the customer is from Segment A is 0.80. 1.1 Union of Events

We can define an event © of getting a 4 or 6 when we roll a fair die. Here event C is a union of two events: In simple words we can say that we should consider the probability of (A ꓴ B) when we are interested in combined probability of two (or more) events

1.2 Intersection of Events We can now say that the shaded region is the probability of both events A and B occurring together.

1.3 Disjoint Events What if, you come across a case when any two particular events cannot occur at the same time
As you can see, there is no case for which event A & B can occur together. Such events are called disjoint event. To represent this using a Venn diagram:

Now that we are familiar with the terms Union, intersection and disjoint events, we can talk about independence of events.

2.Independent, Dependent & Exclusive Events

suppose we have two events — event A and event B.

If the occurrence of event A doesn’t affect the occurrence of event B, these events are called independent events.
Let’s see some examples of independent events.

Getting heads after tossing a coin AND getting a 5 on a throw of a fair die.
Choosing a marble from a jar AND getting heads after tossing a coin.
Choosing a 3 card from a deck of cards, replacing it, AND then choosing an ace as the second card.
Rolling a 4 on a fair die, AND then rolling a 1 on a second roll of the die.

In each of these cases the probability of outcome of the second event is not affected at all by the outcome of the first event.

2.1 Probability of independent events

In this case the probability of P (A ꓵ B) = P (A) * P (B)

2.2 Mutually exclusive and Exhaustive events

Mutually exclusive events are those events where two events cannot happen together.

The easiest example to understand this is the toss of a coin. Getting a head and a tail are mutually exclusive because we can either get heads or tails but never both at the same in a single coin toss.

A set of events is collectively exhaustive when the set should contain all the possible outcomes of the experiment. One of the events from the list must occur for sure when the experiment is performed.

For example, in a throw of a die, <1,2,3,4,5,6>is an exhaustive collection because, it encompasses the entire range of the possible outcomes.

Consider the outcomes “even” (2,4 or 6) and “not-6” (1,2,3,4, or 5) in a throw of a fair die. They are collectively exhaustive but not mutually exclusive.

2.3 Conditional Probability

Conditional probabilities arise naturally in the investigation of experiments where an outcome of a trial may affect the outcomes of the subsequent trials.

We try to calculate the probability of the second event (event B) given that the first event (event A) has already happened. If the probability of the event changes when we take the first event into consideration, we can safely say that the probability of event B is dependent of the occurrence of event A.

Let’s think of cases where this happens:

Drawing a second ace from a deck given we got the first ace
Finding the probability of having a disease given you were tested positive
Finding the probability of liking Harry Potter given we know the person likes fiction
And so on….

Here we can define, 2 events:

Event A is the probability of the event we’re trying to calculate.
Event B is the condition that we know or the event that has happened.
We can write the conditional probability as , the probability of the occurrence of event A given that B has already happened.

3. Bayes Theorem
The Bayes theorem describes the probability of an event based on the prior knowledge of the conditions that might be related to the event. If we know the conditional probability , we can use the bayes rule to find out the reverse probabilities .

The above statement is the general representation of the Bayes rule.

For the previous example — if we now wish to calculate the probability of having a pizza for lunch provided you had a bagel for breakfast would be = 0.7 * 0.5/0.6.

We can generalize the formula further.

If multiple events Ai form an exhaustive set with another event B.

We can write the equation as 5. Example of Bayes Theorem and Probability trees

Let’s take the example of the breast cancer patients. The patients were tested thrice before the oncologist concluded that they had cancer. The general belief is that 1.48 out of a 1000 people have breast cancer in the US at that particular time when this test was conducted. The patients were tested over multiple tests. Three sets of test were done and the patient was only diagnosed with cancer if she tested positive in all three of them.

Let’s examine the test in detail.

Sensitivity of the test (93%) — true positive Rate

Specificity of the test (99%) — true negative Rate

Let’s first compute the probability of having cancer given that the patient tested positive in the first test.

Sensitivity can be denoted as P (+ | cancer) = 0.93

Specificity can be denoted as P (- | no cancer)

Since we do not have any other information, we believe that the patient is a randomly sampled individual. Hence our prior belief is that there is a 0.148% probability of the patient having cancer.

The complement is that there is a 100–0.148% chance that the patient does not have CANCER. Similarly we can draw the below tree to denote the probabilities. Let’s now try to calculate the probability of having cancer given that he tested positive on the first test i.e. P (cancer|+) P (cancer and +) = P (cancer) * P (+) = 0.00148*0.93

P (no cancer and +) = P (no cancer) * P(+) = 0.99852*0.01

To calculate the probability of testing positive, the person can have cancer and test positive or he may not have cancer and still test positive. This means that there is a 12% chance that the patient has cancer given he tested positive in the first test. This is known as the posterior probability.

5.1 Bayes Updating

Let’s now try to calculate the probability of having cancer given the patient tested positive in the second test as well.

Now remember we will only do the second test if she tested positive in the first one. Therefore now the person is no longer a randomly sampled person but a specific case. We know something about her. Hence, the prior probabilities should change. We update the prior probability with the posterior from the previous test.

Nothing would change in the sensitivity and specificity of the test since we’re doing the same test again. Look at the probability tree below. Let’s calculate again the probability of having cancer given she tested positive in the second test.

P (cancer and +) = P(cancer) * P(+) = 0.12 * 0.93

P (no cancer and +) = P (no cancer) * P (+) = 0.88 * 0.01

To calculate the probability of testing positive, the person can have cancer and test positive or she may not have cancer and still test positive. Now we see, that a patient who tested positive in the test twice, has a 93% chance of having cancer.

6. Frequentist vs Bayesian Definitions of probability

A frequentist defines probability as an expected frequency of occurrence over large number of experiments.

P(event) = n/N, where n is the number of times event A occurs in N opportunities.

The Bayesian view of probability is related to degree of belief. It is a measure of the plausibility of an event given incomplete knowledge.

The frequentist believes that the population mean is real but unknowable and can only be estimated from the data. He knows the distribution of the sample mean and constructs a confidence interval centered at the sample mean. So the actual population mean is either in the confidence interval or not in it.

This is because he believes that the true mean is a single fixed value and does not have a distribution. So the frequentist says that 95% of similar intervals would contain the true mean, if each interval were constructed from a different random sample.

The Bayesian definition has a totally different view point. They use their beliefs to construct probabilities. They believe that certain values are more believable than others based on the data and our prior knowledge.

The Bayesian constructs a credible interval centered near the sample mean and totally affected by the prior beliefs about the mean. The Bayesian can therefore make statements about the population mean by using the probabilities.

There is a web page for the text: Link to Goldstein Click the "jump to" button. You will find multiple choice Quizzes with answers.

2- Chart showing how cancer compares with other causes of death at various ages (NY times July 2, 2002). It is correct to say that 1 in 8 women will bevelop breast cancer in her lifetime? How does this compare with the chart? What kind of probabilities are these numbers?

7- Article on overall risk of catastrophic failure of Shuttle New York Times 12/4/1993
Question: What is the probability of at least one failure in 50 flights? in 100 flights?

10- New York Times, 9/6/2000 article on Firestone tires
Some natural questions: Identify the data in terms of conditional probability
Can one calculate the probability of a fatal accident?

11- Article on Tamoxifen and endometrial cancers, NY times 9/8/00.
Can one deduce the probability that a women who had breast cancer and takes tamoxifen developes endometrial cancer?

12- Article from NY Times, 9/27/2000 From an article on Income and Poverty. The NY Times understands the difference between the median and average.

13- Article on Nuclear Wepons
What "calculation" did the the officials at the Strategic Air Command make that led them to target one facility with 69 nuclear missiles?

14-Graph from Feb. 20, 2001
A graph of "Dangerous drivers and the age spectrum" It may be instructive to interpert this as a Histogram.

15- Article on Raloxifene and Breast Cancer from Feb. 26 issue of Breast Cancer Research and Treatment
What type of probability are these numbers? Could one compute the probability a woman with osteoporosis from the general population will contract Breast Cancer?

16- Some census data from March 6 2001 NY Times
Explain these numbers in terms of probabilities.

An article from the April 28 edition of the NY times on Bayes' theorem and life. Interesting examples of applications of Bayes theorem as well as the controversy about its use.
17-Adding art to the Rigor of Statistical Science

18- An article from the June 8, 2004 edition of the NY times on the "Fat Epidemic". An example of how statistics can present data in two ways which seem to contradict each other.

19-A study reported in the NY times "Aspirin is seen as preventing breast tumors"
I marked the data with ******. About half the women in the study had breast cancer. Can you determine the probability a woman in the study has breast cancer if she takes aspirin? If she does not take aspirin?

20- NY times 12/12/2009
Application of Bayes' theorem to the mammogram controversy. The argument is exactly the same as the one for TB testing covered in class.

## 1.2 Posterior Predictive Distribution

The posterior predictive distribution is the the probability of observing new data ( (y^) ) given the posterior distribution of the model parameters after observing training data, (p( heta | y^)) . [ p(y^ | y^) = int p(y^ | heta) p( heta | y^),d heta . ag <1.1>]

Many tradition statistical or machine learning methods proceed by estimating a “best” value of the parameters using training data, and then predicting evaluating data using that parameter. For example, we could calculate the maximum a posteriori estimate of of ( heta) given the training data, [ hat < heta>= arg max_ < heta>p( heta | y^) , ] and then use that for the distribution of evaluation data, [ p(y^ | y^ approx p(y^ | hat< heta>) . ] However, this does not incorporate the uncertainty in the estimates of ( heta) . The full form of the posterior predictive distribution in Equation ag <1.1>incorporates the uncertainty about ( heta) into the distribution of (p(y^ | heta)) .