Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
397 changes: 397 additions & 0 deletions your-code/.ipynb_checkpoints/main-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,397 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Intro to Bayesian Statistics Lab\n",
"\n",
"Complete the following set of exercises to solidify your knowledge of Bayesian statistics and Bayesian data analysis."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def bayes(priori, verosimilitud): \n",
" marginal=sum(np.multiply(priori, verosimilitud))\n",
" posteriori=np.divide(np.multiply(priori, verosimilitud), marginal)\n",
" return posteriori\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Cookie Problem\n",
"\n",
"Suppose we have two bowls of cookies. Bowl 1 contains 30 vanilla cookies and 10 chocolate cookies. Bowl 2 contains 20 of each. You randomly pick one cookie out of one of the bowls, and it is vanilla. Use Bayes Theorem to calculate the probability that the vanilla cookie you picked came from Bowl 1?"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 0.75*0.5/(0.75*0.5+0.5*0.5)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the probability that it came from Bowl 2?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 0.5*0.5/(0.75*0.5+0.5*0.5)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What if the cookie you had picked was chocolate? What are the probabilities that the chocolate cookie came from Bowl 1 and Bowl 2 respectively?"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bowl1 0.3333333333333333 \n",
"Bowl2 0.6666666666666667\n"
]
}
],
"source": [
"p = 0.25*0.5/(0.25*0.5+0.5*0.5)\n",
"print('Bowl1',p,'\\nBowl2',1-p)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Candy Problem\n",
"\n",
"Suppose you have two bags of candies:\n",
"\n",
"- In Bag 1, the mix of colors is:\n",
" - Brown - 30%\n",
" - Yellow - 20%\n",
" - Red - 20%\n",
" - Green - 10%\n",
" - Orange - 10%\n",
" - Tan - 10%\n",
" \n",
"- In Bag 2, the mix of colors is:\n",
" - Blue - 24%\n",
" - Green - 20%\n",
" - Orange - 16%\n",
" - Yellow - 14%\n",
" - Red - 13%\n",
" - Brown - 13%\n",
" \n",
"Not knowing which bag is which, you randomly draw one candy from each bag. One is yellow and one is green. What is the probability that the yellow one came from the Bag 1?\n",
"\n",
"*Hint: For the likelihoods, you will need to multiply the probabilities of drawing yellow from one bag and green from the other bag and vice versa.*"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5882352941176471"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 0.2*0.5/(0.2*0.5+0.14*0.5)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the probability that the yellow candy came from Bag 2?"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.411764705882353"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 0.14*0.5/(0.2*0.5+0.14*0.5)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What are the probabilities that the green one came from Bag 1 and Bag 2 respectively?"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bag1 0.3333333333333333 \n",
"Bab2 0.6666666666666667\n"
]
}
],
"source": [
"p = 0.1*0.5/(0.1*0.5+0.2*0.5)\n",
"print('Bag1',p,'\\nBab2',1-p)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Monty Hall Problem\n",
"\n",
"Suppose you are a contestant on the popular game show *Let's Make a Deal*. The host of the show (Monty Hall) presents you with three doors - Door A, Door B, and Door C. He tells you that there is a sports car behind one of them and if you choose the correct one, you win the car!\n",
"\n",
"You select Door A, but then Monty makes things a little more interesting. He opens Door B to reveal that there is no sports car behind it and asks you if you would like to stick with your choice of Door A or switch your choice to Door C. Given this new information, what are the probabilities of you winning the car if you stick with Door A versus if you switch to Door C?"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3333333333333333"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pg2=1/2\n",
"p2=1/3\n",
"pg=(1/3*1/2)+(1/3*0)+(1/3*1)\n",
"\n",
"p_g_2=(pg2*p2)/pg\n",
"p_g_2\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Bayesian Analysis \n",
"\n",
"Suppose you work for a landscaping company, and they want to advertise their service online. They create an ad and sit back waiting for the money to roll in. On the first day, the ad sends 100 visitors to the site and 14 of them sign up for landscaping services. Create a generative model to come up with the posterior distribution and produce a visualization of what the posterior distribution would look like given the observed data."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<AxesSubplot:>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYA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cH1EbO3HviRJ79evM/eLgKOZ+Vxm/i+wF9jep31Jkk4Smdn7jUZ8CrgiM/+6eX0j8MeZeXvbMruAXc3L3wOe6XlHVp8NwC8H3YkVzPrMzdrMr2p9zsvMj5xqxsCulsnMCWBiUPtfiSLiYGaODrofK5X1mZu1md9arE+/hmWmgM1tr89t2iRJy6Bf4f4zYEtEnB8RHwSuA/b1aV+SpJP0ZVgmM2ci4nbgR8BpwD2Z+VQ/9lWMw1Tzsz5zszbzW3P16csfVCVJg+UnVCWpIMNdkgoy3JfBQrdiiIg/i4jHI2Km+YxA+7wdEfFs89ixfL1ePl3W5+2IONQ8Sv7RvoP6fC4ijkTEExGxPyLOa5vn8TN/feoeP5npo48PZv+g/HPgo8AHgf8Atp60zDDwB8B9wKfa2s8Bnmue1zfT6wf9nlZKfZp504N+DyugPtuA32qm/wb4rsfPwvWpfvx45t5/C96KITMnM/MJ4J2T1r0ceDgzX8nME8DDwBXL0ell1E191oJO6vNIZr7ZvDzA7OdKwOMHmLc+pRnu/bcJeLHt9UtNW7/XXS26fY+/GREHI+JARFzT056tDIutz63AD5e47mrUTX2g8PHjl3VotTsvM6ci4qPAv0XE4cz8+aA7NQgR8VfAKPDng+7LSjRHfcoeP5659183t2JYC7dx6Oo9ZuZU8/wc0AIu7GXnVoCO6hMRfwF8EfjLzHxrMeuuct3Up/TxY7j3Xze3YvgRcFlErI+I9cBlTVslS65PU5czmukNwCXAkb71dDAWrE9EXAh8g9ngOt42y+OHuetT/vgZ9F9018IDuAr4L2b/qv/Fpu1vmT3YAP6I2bHCN4BfAU+1rXsLcLR53Dzo97KS6gP8CXCY2SskDgO3Dvq9DKg+PwaOAYeaxz6Pn4XrU/348fYDklSQwzKSVJDhLkkFGe6SVJDhLkkFGe6SVJDhLkkFGe6SVND/AfjX4wf2n+T/AAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"def generative_model(proba_compra):\n",
" compraron = np.random.binomial(100, proba_compra)\n",
" return compraron\n",
"\n",
"n_draws = 100_000\n",
"prior = pd.Series(np.random.uniform(0,1,size=n_draws))\n",
"\n",
"compraron = list()\n",
"for equipo in prior:\n",
" compraron.append(generative_model(equipo))\n",
" \n",
"posteriori = prior[list(map(lambda x:x ==14, compraron))]\n",
"posteriori.hist();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Produce a set of descriptive statistics for the posterior distribution."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 1060.000000\n",
"mean 0.146737\n",
"std 0.035323\n",
"min 0.064836\n",
"25% 0.121435\n",
"50% 0.143215\n",
"75% 0.169303\n",
"max 0.276941\n",
"dtype: float64"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"posteriori.describe()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the 90% credible interval range?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the Maximum Likelihood Estimate?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading