Hello dear reader! Welcome to the first post of a series of awesome and fun probability for machine learning posts: Bayes Theorem Explained!

This post assumes you have someÂ **basic knowledge of probability and statistics**. If you donâ€™t, do not be afraid, I have gathered aÂ **list of the best resources**Â I could find to introduce you to these subjects, so that you can read this post, understand it, andÂ **enjoy it to its fullest**.

In it, we will talk about one of the most famousÂ and utilised theorems of probability theory:**Â Bayesâ€™ Theorem**. Never heard about it?Â **Then you are in for a treat!**Â Know what it is already?Â **Then read on to consolidate your knowledge**Â with an easy, everyday example, so that you too can explain it in simple terms to others. Lets proceed to get Bayes Theorem explained with easy examples.

In further **following postsÂ **we will learn about someÂ **simplifications of Bayeâ€™s theorem**Â that are more practical, and aboutÂ **other probabilistic approaches**Â toÂ machine learning likeÂ ** Hidden Markov Models**.

## Introduction to probability:

In this section I have listed** three very good **and concise (the two first mainly, the third one is a bit more extensive) **sources to learn the basics of probability** that you will need to understand this post. Donâ€™t be afraid, the concepts are very simple and with a quick read you will most definitely understand them.

**If you already have a grasp of basic probability, feel free to skip this section.**

**Medium post with plain concise definitions**Â of the main terms of probability needed to understand this post and many others with some illustrative and easy examples.**Fun little introduction to probability inÂ**and the main with a mysterious but easy example where each of the main terms of probability are introduced.*Machine Learning***Harvardâ€™s Statistics 110 course**.Â This is a larger resource, in case you donâ€™t only want to learn the fundamentals, but dive deeper into the wonderful world of statistics.**Probability for the enthusiastic beginner**: A great book which we have reviewed in our site.**Bayesian statistics the fun way:**another awesome book which we have reviewed, with great, easy examples to teach you all the fundamentals of bayesian statistics.**Our great section of Statistics and Probability online courses**. Check it out!

Okay, now you are ready to continue on with the rest of the post, **sit back, relax, and enjoy.**

## Bayesâ€™ Theorem Explained Simply:

### Who was Bayes?

**Thomas Bayes**Â (1701 â€” 1761) was anÂ **English theologian and mathematicianÂ **that belonged to theÂ **Royal Society**Â (the oldest national scientific society in the world and the leading national organisation for the promotion of scientific research in Britain), where other eminent individuals have enrolled, like Newton, Darwin or Faraday.

He developed one of the most important theorems of probability, which coined his name:Â **Bayesâ€™ Theorem, or the Theorem of Conditional Probability, also known as Bayes rule or Bayes Formula.**

### Bayes theorem explained from the beginning: Conditional Probability

To explain this theorem, we will use a very simple example. Imagine you have been diagnosed withÂ **a very rare disease**,Â which onlyÂ **affects 0.1% of the population**; that is, 1 in every 1000 persons.

**The testÂ **you have taken to check for the diseaseÂ **correctly classifies 99% of the people who have the disease**, incorrectly classifying healthy individuals with a 1% chance.

I am doomed! Is this disease fatal doctor?

Is what most people would say.Â **However, after this test, what are the chances we have of actually suffering the disease?**

99% for sure! I better get my stuff in order.

Upon this thought, Bayesâ€™ mentality ought to prevail, as it is actually **very far off from reality. Lets use Bayesâ€™ Theorem to gain some perspective.**

**Bayesâ€™ Theorem**, or as I have called it before, theÂ **Theorem of Conditional Probability**, is used for calculating the **probability of a hypothesis (H) being true**Â (ie. having the disease)Â **given that a certain eventÂ (E) has happenedÂ **(being diagnosed positive of this disease in the test).Â This calculation is described using the following formulation:

The term on theÂ **left of the equal sign P(H |E )**is theÂ **probability of having the disease (H) given that we have been diagnosed positive (E)Â **in a test for such disease, which is what we actually want to calculate.Â TheÂ **vertical bars (|)**Â in a probability term denote a conditional probability (ie, the probability of A given B would beÂ ** P(A|B)**).

The left term of the numerator on the right sideÂ ** P(E|H)Â **is the probability of the event, given that the hypothesis is true. In our example this would be theÂ

**probability of being diagnosed positive in the test, given that we have the disease**.

The term next to it;Â ** P(H)Â **is theÂ

**prior probability of the hypothesis**Â before any event has taken place.Â In this case it would be the probability of having the disease before any test has been taken.

Lastly, the term on the denominator;Â ** P(E)**Â is the probability of the event, that is, the probability of being diagnosed positive for the disease.

**Â This term can be further decomposed**Â into the sum of two smaller terms:Â having the disease and testing positive plus not having the disease and testing positive too.

In this formula** P(~H) **denotes the

**prior probability of not having the disease,**where

**means negation or not. The following picture describes each of the terms involved in the overall calculation of the conditional probability:**

*~*Remember, for us theÂ **hypothesis or assumption H is having the disease**,Â and the**Â event or evidence EÂ **is beingÂ **diagnosed positive**Â in the test for such disease.

If we use the **first formula** we saw (the complete formula for calculating the conditional probability of having the disease and being diagnosed positive), **decompose the denominator, and insert in the numbers**, we get the following calculation:

The 0,99 comes from the 99% probability of being diagnosed positive given that we have the disease, the 0,001 comes from the 1 in 1000 chance of having the disease, the 0,999 comes from the probability of not having this disease, and the last 0,01 comes from the probability of being diagnosed positive by the test even tough we do not have the disease. The final result of this calculation is:

**9%! There is only a 9% chance that we have the disease!**Â â€ś*How can this be?â€ť*Â you are probably asking yourself.Â **Magic?Â **No, my friends, it is not magic, it isÂ **just probability**: common sense applied to mathematics.

Like described in the bookÂ *Thinking, Fast and Slow** byÂ *Daniel Kahneman,Â the human mind is very bad at estimating and calculating probabilities, like it has been shown with the previous example, so we should always hold back our intuition, take a step back and use all the probability tools at our disposal.

Imagine now, that after being diagnosed positive on the first test, we decide to **take another test** with the same conditions, in a different clinic to double-check the results, and unluckily we get a positive diagnosis again, indicating that the second test also says that we have the disease.

**What is the actual probability of having the disease now?Â **Well, we canÂ use exactly the same formula as before, butÂ **replacing the initial prior probabilityÂ **(0.1% chance of having the disease)Â **with the posterior probability obtained the previous time**Â (9% chance after being diagnosed positive by the test once), and their complementary terms.

If we crunch the numbers we get:

Now we have a **much higher chance**,** 91%** of actually having the disease. As bad as it might look tough, after two positive tests it is still not completely certain that we have the disease. *Certainty seems to escape the world of probability.*

## TheÂ intuitionÂ behindÂ the theorem

The intuition behind this famous theorem is that we can never be fully certain of the world, as it is a changing being, change is in the nature of reality.However, something that we can do, which isÂ the fundamental principle behind this theorem, isÂ **update and improve our knowledge of reality as we get more and more data or evidence**.

**This can be illustrated with a very simple example.**Â Imagine the following situation: you are in the edge square shaped garden, sitting on a chair, looking outside the garden. On the opposite side, lies a servant that throws a initial blue ball inside the square. After that, he keeps throwing other yellow balls inside the square, and telling you where they land relatively to the initial blue ball.

As more and more yellow balls land, and you get informed of where they land relative to the first blue ball, you gradually increase your knowledge of where the blue ball might be, leaving out certain parts of the garden:Â **as we get more evidence (more yellow balls) we update our knowledge (the position of the blue ball).**

In the example above,Â **with only 3 yellow balls thrown**, we could already start to build a certain idea that the blue ball lies somewhere within the top left corner of the garden.

When Bayesâ€™ first formulated this theorem, **he didnâ€™t publish it at first, thinking it was nothing extraordinary,** and the papers where the theorem was formulated were found after his death.

Today Bayesâ€™ theorem is not onlyÂ **one of the bases of modern probability**, but a highly used tool in many intelligent systems likeÂ **spam filters**, and many other text and non text related problem solvers.

In the following post we will seeÂ **what these applications are**, and how Bayesâ€™ theorem and its variations can be applied to many real world use cases.

For more material on probability and statistics, check out the following page withÂ the best online CoursesÂ to learn about this wonderful topic!

Also, find some great material to start learning probability here:

- Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python: If you want to start coding with Python at the same time you deepen your statistical knowledge, this is a great book. Find a review here.
- Dr Nicâ€™s Maths and StatsÂ is a Youtube channel whose purpose is described in its name: teaching you about Maths and Statistics.
- One Thousand Exercises in Probability: Third Edition: A book which we have not reviewed yet but that comes packed with a lot of very useful exercises to learn probability.

That is all, we hope you liked this post on Bayes Theorem Explained. Have a great day and remember to follow us on twitter!

**Tags**: *Bayes Theorem Explained, Bayes Theorem for Machine Learning, Probability for Machine Learning, Bayes Formula.*