# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# + [markdown] toc="true"
# # Table of Contents
#  <p><div class="lev1"><a href="#Probability"><span class="toc-item-num">1&nbsp;&nbsp;</span>Probability</a></div><div class="lev1"><a href="#Coin"><span class="toc-item-num">2&nbsp;&nbsp;</span>Coin</a></div><div class="lev2"><a href="#Fair-Coin"><span class="toc-item-num">2.1&nbsp;&nbsp;</span>Fair Coin</a></div><div class="lev2"><a href="#Loaded-Coin"><span class="toc-item-num">2.2&nbsp;&nbsp;</span>Loaded Coin</a></div><div class="lev1"><a href="#Two-Flips"><span class="toc-item-num">3&nbsp;&nbsp;</span>Two Flips</a></div><div class="lev1"><a href="#One-Head"><span class="toc-item-num">4&nbsp;&nbsp;</span>One Head</a></div><div class="lev1"><a href="#One-of-Three"><span class="toc-item-num">5&nbsp;&nbsp;</span>One of Three</a></div><div class="lev2"><a href="#Fair-Coin"><span class="toc-item-num">5.1&nbsp;&nbsp;</span>Fair Coin</a></div><div class="lev2"><a href="#Loaded-Coin"><span class="toc-item-num">5.2&nbsp;&nbsp;</span>Loaded Coin</a></div><div class="lev1"><a href="#Even-Roll"><span class="toc-item-num">6&nbsp;&nbsp;</span>Even Roll</a></div><div class="lev1"><a href="#Doubles"><span class="toc-item-num">7&nbsp;&nbsp;</span>Doubles</a></div><div class="lev1"><a href="#Summary"><span class="toc-item-num">8&nbsp;&nbsp;</span>Summary</a></div>
# -

# # Probability

# - Probability is the opposite of statistics.
# - Put differently, in statistics we are given data and try to infer possible causes that relate to the data, whereas in probability we are given the description of the causes and we'd like to predict the data.
# - The reason why we now study probability and not statistics is because it gives us a language to describe the relationship between data and the underlying causes.

# <img  src="images/Screen Shot 2016-04-26 at 9.09.01 PM.png"/>

# # Coin

# ## Fair Coin

# In a fair coin, the chances are 50%:
# - P(HEADS) = 0.5
# - P(TAILS) = 0.5

# ## Loaded Coin

# A loaded coin the one that comes up with one of the two much more frequently than the other.
# - P(HEADS) = 1 = 100%
# - P(TAILS) = 0 = 0%
# - P(HEADS) + P(TAILS) = 1
# - P(A) = 1 - P(not A)

# <img  src="images/Screen Shot 2016-04-26 at 9.19.10 PM.png"/>

# # Two Flips

# What is the probability of if 2 flips are heads?
# - The answer is 0.25.
# - I will derive it for you using something called a truth table. In a truth table, you draw out every possible outcome of the experiment that you conducted.
# - Another way to look at this is the probability of heads followed by heads is the product. What are the chances of the first outcome to be heads multiplied by the probability of the second outcome to be heads?

# <img  src="images/Screen Shot 2016-04-26 at 9.22.13 PM.png"/>

# <img  src="images/Screen Shot 2016-04-26 at 9.27.52 PM.png"/>

# # One Head

# Suppose we flip our coin twice. What we care about is that exactly one of the two things is heads, and thereby exactly the other one is tails. For a fair coin, what do you think the probability would be that if I flip it twice we would see heads exactly once?
# - P(exactly one H) = 0.5
#
# Given that, we now have to associate a truth table with the question we're asking. So where exactly is, in the outcome, heads represented once?
# - And, yes, it's in the second case and in the third case. The extreme cases of heads, heads and tails, tails don't satisfy this condition. So the trick now has been to take the 0.25 probability of these two cases and add them up, which gives us 0.25 + 0.25 = 0.5. This is the number which is correct for this inquiry.

# <img  src="images/Screen Shot 2016-04-26 at 10.21.17 PM.png"/>

# # One of Three

# ## Fair Coin

# What is the probability that exactly 1 of those 3 flips comes up heads?
#
# **Answer**
# - We will derive it through the truth table. Now there's eight possible cases. Flip one can come of heads or tail; same for flip two, heads, tail, heads, tail; and the same for flip three and if you look at this every possible combination is represented.
# - For example, these are heads, tail, tail. Now each of those outcomes has the same probability of an eighth, because it's eight cases. So 8 x 1/8 sums up to 1. 
# - In how many cases do we have exactly one H? It turns out that it's true for only three cases. The H could be in the first position, in the second position, or in the third position. So three out of eight cases have a single H. Each of those carries a probability so we sum those cases up to carry a total of 3/8 of a probability. These are the same as 0.375.

# <img  src="images/Screen Shot 2016-04-26 at 10.24.48 PM.png"/>

# ## Loaded Coin

# Here is a challenging question. I'll give you a loaded coin--the probability for H is 0.6. I expect this will take you awhile on a piece of paper to really calculate this probability over here. But you can do exactly the same thing. You go through the truth table. You apply the multiplication I showed you before to calculate the probability of each outcome; they're not the same anymore. H, H, H is clearly more likely than T, T, T. And when you've done this, add the corresponding figures up, and tell me what the answer is.
# - P(H) = 0.6

# **Truth table in python**
# - http://stackoverflow.com/questions/29548744/creating-a-truth-table-for-any-expression-in-python

# +
from itertools import product
from numpy import prod

heads = 0.6
tails = 0.4

truth_table = list(product((heads, tails), repeat=3))
for x in truth_table:
    print x, prod(x)
# -

result = []
for x in truth_table:
    if x.count(0.6) == 1:
        print x, prod(x)
        result.append(prod(x))
print sum(result)

# <img  src="images/Screen Shot 2016-04-26 at 10.45.24 PM.png"/>

# # Even Roll

# Now I am throwing dice. The difference between dice and coins is that there are now 6 possible outcomes. Let me just draw them, and say it's a fair die, which means each of the different sides comes up with a probability over 6 for any of the numbers you can plug in over here. What do you think the probability is the die comes up with an even number? I'm going to write this as the outcome of the die is even. And you can once again use a truth table to calculate that number.
#
# **Answer**
# - In truth table-speak, there are 6 outcomes, 1 to 6. Each has the same probability over six. Half of those numbers are even--2, 4, and 6, so if we add those up, we get 3 1/6--the same as a half. The outcomes is 0.5.

# <img  src="images/Screen Shot 2016-04-26 at 10.56.24 PM.png"/>

# # Doubles

# Suppose we throw a fair die twice. What do you think the probability of a double is? Double means both outcomes are identical with the same number regardless of what that number is. The actually an important number because in many games involving two dice, have different rules when these come up with the same number. So, it might be important to know what the probability is.
#
# **Answer**
# - Now the truth table will have 36 different entries, six for the first throw times six for the second throw, and there isn't enough space on this tablet to draw all the 36 entries. So, let me just draw the ones that really matter, one-one, two-two, and so on all the way to a six-six.
# - So, each one of those is a probability of 1/6 for the first outcome times 1/6 for the second, which gives me 1/36, and the same logic applies everywhere.
# - So, for all of these six outcomes, I have 1/36 of a chance this outcome would materialize. Adding them all up gives me 1/6, why? Because, I get 6 times 36 and I can simply this back to 1/6 that's just the same as 0.16667. So, 1/6 times, you will get a double

# <img  src="images/Screen Shot 2016-04-26 at 11.29.17 PM.png"/>

# # Summary

# - You learned about probability of an event, such as the outcome of a coin flip.
# - You learned that the probability of the opposite event is 1 minus the probability of the event.
# - And you learned about the probability of a composite event, which was in the form P * P * P. Technically speaking, this thing over here is called independence, which means nothing else, but that the outcome of the second coin flip didn't really depend on the outcome of the first coin flip.
#
# In our next unit, we will talk about dependence, where there are bizarre dependencies between different outcomes. But for the time being, you really managed to get a very basic understanding of probability.

# <img  src="images/Screen Shot 2016-04-26 at 11.34.21 PM.png"/>

StarcoderdataJupyterScriptsDedupFiltered