Skip to content

Commit

Permalink
Merge pull request #46 from AlexAndorra/add-questions-to-merged-nbs
Browse files Browse the repository at this point in the history
Add exercises questions to merged chapters
  • Loading branch information
aloctavodia authored Sep 2, 2019
2 parents a3e36de + 2930c73 commit 8f139c7
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 151 deletions.
131 changes: 97 additions & 34 deletions exercises/Chapter1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@
"metadata": {},
"outputs": [],
"source": [
"import arviz as az\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from scipy import stats\n",
"import arviz as az"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"\n",
"az.style.use('arviz-darkgrid')"
]
},
Expand All @@ -41,19 +35,30 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We do not know whether the brain really works in a Bayesian way, in an approximate Bayesian fashion, or maybe some evolutionary (more or less) optimized heuristics. Nevertheless, we know that we learn by exposing ourselves to data, examples and exercises - well, you may say that humans never learn, given our record as a species on subjects such as wars or economic systems that prioritize profit and not people's well-being... Anyway, I recommend you do the proposed exercises at the end of each chapter.\n",
"\n",
"*From the following expressions, which one corresponds to the sentence \"the probability of being sunny, given that it is the 9th of July of 1816\"?*\n",
"- p(sunny)\n",
"- p(sunny | July)\n",
"- p(sunny | 9th of July of 1816)\n",
"- p(9th of July of 1816 | sunny)\n",
"- p(sunny, 9th of July of 1816) / p(9th of July of 1816)\n",
"\n",
"\n",
"There are two statements that correspond to the *Probability of being sunny given that it is the 9th of July of 1816*\n",
"\n",
"1. p(sunny | 9th of July of 1816)\n",
"2. p(sunny| 9th of July of 1816) / p(9th of July of 1816)\n",
"2. p(sunny, 9th of July of 1816) / p(9th of July of 1816)\n",
"\n",
"For the second one recall the product rule (Equation 1.1)\n",
"\n",
"$$ p(A,B) = p(A|B)p(B)$$\n",
"$$ p(A,B) = p(A|B)p(B) $$\n",
"\n",
"A rearrangement of this formula yields\n",
"\n",
"$$ p(A|B) = \\frac{p(A,B)}{p(B)}$$\n",
"Replace A and B with \"sunny\" and \"9th of July of 1816\" to get the equivament formulation"
"$$ p(A|B) = \\frac{p(A, B)}{p(B)}$$\n",
"\n",
"Replace A and B with \"sunny\" and \"9th of July of 1816\" to get the equivament formulation."
]
},
{
Expand All @@ -68,22 +73,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's assume there are 6 billion humans in the galaxy and there is only 1 Pope, Pope Francis, at time of writing. If a human is randomly picked the chances of that human being the pope are 1 in 6 billion. In mathematical notation\n",
"*Show that the probability of choosing a human at random and picking the Pope is not the same as the probability of the Pope being human.*\n",
"\n",
"Let's assume there are 6 billion humans in the galaxy and there is only 1 Pope, Pope Francis, at the time of this writing. If a human is randomly picked the chances of that human being the pope are 1 in 6 billion. In mathematical notation\n",
"\n",
"$$ p(Pope | human) = \\frac{1}{6,000,000} $$\n",
"\n",
"Additionally I am very confident that the Pope is human, so much so that I make this statement. *Given a pope, I am 100% certain they are human*. \n",
"Written in math\n",
"$$ p(human | Pope) = 1$$\n",
"$$ p(human | Pope) = 1 $$\n",
"\n",
"*In the animated series Futurama, the (space) Pope is a reptile. How does this change your previous calculations?*\n",
"\n",
"In animated show Futurama the human pope has been replaced by a reptile pope. As is such.\n",
"Ok then:\n",
"\n",
"$$ p(Pope | human) = 0 $$\n",
"\n",
"And \n",
"\n",
"$$ p(human | Pope) = 0$$"
"$$ p(human | Pope) = 0 $$"
]
},
{
Expand All @@ -98,15 +106,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The priors in thie model are\n",
"*In the following definition of a probabilistic model, identify the prior and the likelihood:*\n",
"\n",
"\\begin{eqnarray}\n",
"y_i \\text{~} Normal(\\mu, \\sigma) \\newline\n",
"\\mu \\text{~} Normal(0,10) \\newline\n",
"\\sigma \\text{~} HalfNormal(25) \n",
"\\end{eqnarray}\n",
"\n",
"The priors in the model are\n",
"\n",
"\\begin{eqnarray}\n",
"\\mu \\text{~} Normal(0,10) \\newline\n",
"\\sigma \\text{~} HalfNormal(25) \n",
"\\end{eqnarray}\n",
"\n",
"The likelihood in our model is \n",
"$$ y_i \\text{~} Normal(\\mu, \\sigma)$$"
"$$ y_i \\text{~} Normal(\\mu, \\sigma) $$"
]
},
{
Expand All @@ -121,9 +137,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In the previous exquestion there are two parameters in the posterior $\\mu$ and $\\sigma$. In our coin flipping model we had one parameter $\\theta$.\n",
"*In the previous model, how many parameters will the posterior have? Compare it with the model for the coin-flipping problem.*\n",
"\n",
"In the previous question there are two parameters in the posterior, $\\mu$ and $\\sigma$. \n",
"\n",
"It may seem confusing that we also had *alpha* and *beta* as well, but remember, these were not parameters we were trying ot estimate, or in other words we really don't care about alpha and beta, they were just values for our prior distribution. What we really wanted was $\\theta$ to determine the fairness of the coin."
"In our coin flipping model we had one parameter, $\\theta$. It may seem confusing that we had $\\alpha$ and $\\beta$ as well, but remember, these were not parameters we were trying ot estimate. In other words we don't really care about $\\alpha$ and $\\beta$ - they were just values for our prior distribution. What we really wanted was $\\theta$, to determine the fairness of the coin."
]
},
{
Expand All @@ -138,6 +156,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"*Write Bayes' theorem for the model in question 3.*\n",
"\n",
"$$ p(\\mu, \\sigma | y) = \\frac{p(y| \\mu, \\sigma)p(\\mu)p(\\sigma)}{p(y)} $$"
]
},
Expand All @@ -153,32 +173,34 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Formalizing some of the statements into mathematical notatoin\n",
"*Let's suppose that we have two coins. When we toss the first coin, half the time it lands on tails and the other half on heads. The other coin is a loaded coin that always lands on heads. If we take one of the coins at random and get heads, what is the probability that this coin is the unfair one?*\n",
"\n",
"Formalizing some of the statements into mathematical notation:\n",
"\n",
"The probability of picking a coin at random, and getting either the biased or the fair coin is the same \n",
"The probability of picking a coin at random, and getting either the biased or fair coin is the same:\n",
"\n",
"$$p(Biased) = p(Fair) = .5$$\n",
"\n",
"The probabiity of getting a heads with a biased coin is 1,\n",
"The probability of getting heads with the biased coin is 1,\n",
"$$p(Heads | Biased) = 1$$\n",
"\n",
"The probability of getting a heads with the fair coin is .5\n",
"The probability of getting heads with the fair coin is .5\n",
"$$p(Heads | Fair) = .5$$\n",
"\n",
"Lastly remember that after picking a coin at **random** we have observed a heads. Therefore we can use Bayes rule to calculate the probability we picked the biased coin\n",
"Lastly, remember that after picking a coin at *random*, we observed heads. Therefore we can use Bayes rule to calculate the probability that we picked the biased coin:\n",
"\n",
"$$ p(Biased | Heads) = \\frac{p(Heads | Biased) p(Biased)}{p(Heads)} $$\n",
"\n",
"To solve this by hand we need to rewrite the denominator using the *Rule of Total Probability*\n",
"To solve this by hand we need to rewrite the denominator using the *Rule of Total Probability*:\n",
"\n",
"$$ p(Biased | Heads) = \\frac{p(Heads | Biased) p(Biased)}{p(Heads|Fair)*p(Fair) + p(Heads|Biased)*p(Biased)} $$\n",
"\n",
"We can use Python to do the math for us"
"We can use Python to do the math for us:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -187,7 +209,7 @@
"0.6666666666666666"
]
},
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -200,8 +222,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 7\n",
"***"
"## Questions 7 & 8\n",
"***\n",
"\n",
"*Modify the code that generated Figure 1.5, in order to add a dotted vertical line showing the observed rate of $\\frac{Heads}{Number-of-tosses} $. Compare the location of this line to the mode of the posteriors in each subplot.*\n",
"\n",
"*Try re-running this code using other priors (`beta_params`) and other data (`n_trials` and `data`).*"
]
},
{
Expand Down Expand Up @@ -260,8 +286,45 @@
" plt.yticks([])\n",
" \n",
" \n",
"plt.tight_layout()"
"plt.tight_layout();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 9\n",
"***\n",
"\n",
"*Go to the chapter's notebook and explore different parameters for the Gaussian, binomial and beta plots (figures 1.1, 1.3 and 1.4 from the chapter). Alternatively, you may want to plot a single distribution instead of a grid of distributions.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 10\n",
"***\n",
"\n",
"*Read about [Cromwell's rule](https://en.wikipedia.org/wiki/Cromwell%27s_rule) on Wikipedia.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 11\n",
"***\n",
"\n",
"*Read about [probabilities and the Dutch book](https://en.wikipedia.org/wiki/Dutch_book) on Wikipedia.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -280,7 +343,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 8f139c7

Please sign in to comment.