Bayes' Theorem
Bayes can do magic!
Ever wondered how computers learn about people?
Example:
An internet search for "movie automatic shoe laces" brings up "Back to the future"
Has the search engine watched the movie? No, but it knows from lots of other searches what people are probably looking for.
And it calculates that probability using Bayes' Theorem.
Bayes' Theorem is a way of finding a probability when we know certain other probabilities.
The formula is:
P(AB) = \frac{P(A) P(BA)}{P(B)}
Which tells us:  how often A happens given that B happens, written P(AB),  
When we know:  how often B happens given that A happens, written P(BA)  
and how likely A is on its own, written P(A)  
and how likely B is on its own, written P(B) 
Let us say P(Fire) means how often there is fire, and P(Smoke) means how often we see smoke, then:
P(FireSmoke) means how often there is fire when we can see smoke
P(SmokeFire) means how often we can see smoke when there is fire
So the formula kind of tells us "forwards" P(FireSmoke) when we know "backwards" P(SmokeFire)
Example:

dangerous fires are rare (1%)
 but smoke is fairly common (10%) due to barbecues,
 and 90% of dangerous fires make smoke
We can then discover the probability of dangerous Fire when there is Smoke:
So it is still worth checking out any smoke to be sure.
Example: Picnic Day
You are planning a picnic today, but the morning is cloudy
 Oh no! 50% of all rainy days start off cloudy!
 But cloudy mornings are common (about 40% of days start cloudy)
 And this is usually a dry month (only 3 of 30 days tend to be rainy, or 10%)
What is the chance of rain during the day?
We will use Rain to mean rain during the day, and Cloud to mean cloudy morning.
The chance of Rain given Cloud is written P(RainCloud)
So let's put that in the formula:
P(RainCloud) = \frac{P(Rain) P(CloudRain)}{P(Cloud)}
 P(Rain) is Probability of Rain = 10%
 P(CloudRain) is Probability of Cloud, given that Rain happens = 50%
 P(Cloud) is Probability of Cloud = 40%
P(RainCloud) = \frac{0.1 x 0.5}{0.4} = .125
Or a 12.5% chance of rain. Not too bad, let's have a picnic!
Just 4 Numbers
Imagine 100 people at a party, and you tally how many wear pink or not, and if a man or not, and get these numbers:
Bayes' Theorem is based off just those 4 numbers!
Let us do some totals:
And calculate some probabilities:
 the probability of being a man is P(Man) = \frac{40}{100} = 0.4
 the probability of wearing pink is P(Pink) = \frac{25}{100} = 0.25
 the probability that a man wears pink is P(PinkMan) = \frac{5}{40} = 0.125
 the probability that a person wearing pink is a man P(ManPink) = ...
And then the puppy arrives! Such a cute puppy.
But all your data is ripped up! Only 3 values survive:
 P(Man) = 0.4,
 P(Pink) = 0.25 and
 P(PinkMan) = 0.125
Can you discover P(ManPink) ?
Imagine a pinkwearing guest leaves money behind ... was it a man? We can answer this question using Bayes' Theorem:
P(ManPink) = \frac{P(Man) P(PinkMan)}{P(Pink)}
P(ManPink) = \frac{0.4 × 0.125}{0.25} = 0.2
Note: if we still had the raw data we could calculate directly \frac{5}{25} = 0.2Being General
Why does it work?
Let us replace the numbers with letters:
Now let us look at probabilities. So we take some ratios:
 the overall probability of "A" is P(A) = \frac{s+t}{s+t+u+v}
 the probability of "B given A" is P(BA) = \frac{s}{s+t}
And then multiply them together like this:
Now let us do that again but use P(B) and P(AB):
Both ways get the same result of \frac{s}{s+t+u+v}
So we can see that:
P(B) P(AB) = P(A) P(BA)
Nice and symmetrical isn't it?
It actually has to be symmetrical as we can swap rows and columns and get the same topleft corner.
And it is also Bayes Formula ... just divide both sides by P(B):
P(AB) = \frac{P(A) P(BA)}{P(B)}
Remembering
First think "AB AB AB" then remember to group it like: "AB = A BA / B"
P(AB) = \frac{P(A) P(BA)}{P(B)}
Cat Allergy?
One of the famous uses for Bayes Theorem is False Positives and False Negatives.
For those we have two possible cases for "A", such as Pass/Fail (or Yes/No etc)
Example: Allergy or Not?
Hunter says she is itchy. There is a test for Allergy to Cats, but this test is not always right:
 For people that really do have the allergy, the test says "Yes" 80% of the time
 For people that do not have the allergy, the test says "Yes" 10% of the time ("false positive")
If 1% of the population have the allergy, and Hunter's test says "Yes", what are the chances that Hunter really has the allergy?
We want to know the chance of having the allergy when test says "Yes", written P(AllergyYes)
Let's get our formula:
P(AllergyYes) = \frac{P(Allergy) P(YesAllergy)}{P(Yes)}
 P(Allergy) is Probability of Allergy = 1%
 P(YesAllergy) is Probability of test saying "Yes" for people with allergy = 80%
 P(Yes) is Probability of test saying "Yes" (to anyone) = ??%
Oh no! We don't know what the general chance of the test saying "Yes" is ...
... but we can calculate it by adding up those with, and those without the allergy:
 1% have the allergy, and the test says "Yes" to 80% of them
 99% do not have the allergy and the test says "Yes" to 10% of them
Let's add that up:
P(Yes) = 1% × 80% + 99% × 10% = 10.7%
Which means that about 10.7% of the population will get a "Yes" result.
So now we can complete our formula:
P(AllergyYes) = \frac{1% × 80%}{10.7%} = 7.48%
P(AllergyYes) = about 7%
This is the same result we got on False Positives and False Negatives.
In fact we can write a special version of the Bayes' formula just for things like this:
P(AB) = \frac{P(A)P(BA)}{ P(A)P(BA) + P(not A)P(Bnot A)}
"A" With Three (or more) Cases
We just saw "A" with two cases (A and not A), which we took care of in the bottom line.
When "A" has 3 or more cases we include them all in the bottom line:
P(A1B) = \frac{P(A1)P(BA1)}{ P(A1)P(BA1) + P(A2)P(BA2) + P(A3)P(BA3) + ...etc}
Example: The Art Competition has entries from three painters: Pam, Pia and Pablo
 Pam put in 15 paintings, 4% of her works have won First Prize.
 Pia put in 5 paintings, 6% of her works have won First Prize.
 Pablo put in 10 paintings, 3% of his works have won First Prize.
What is the chance that Pam will win First Prize?
P(PamFirst) = \frac{P(Pam)P(FirstPam)}{ P(Pam)P(FirstPam) + P(Pia)P(FirstPia) + P(Pablo)P(FirstPablo)}
Put in the values:
Multiply all by 30 (makes calculation easier):
A good chance!
Pam isn't the most successful artist, but she did put in lots of entries.
Now, back to Search Engines.
Search Engines take this idea and scale it up a lot (plus some other tricks).
It makes them look like they can read your mind!
It can also be used for mail filters, music recommendation services and more.