{ "cells": [ { "cell_type": "markdown", "id": "74172198", "metadata": {}, "source": [ "# Fasit lab 11" ] }, { "cell_type": "markdown", "id": "62aa0786", "metadata": {}, "source": [ "1.) Suppose you have two coins in a box. One is a normal coin with heads on one side and tails on the other, and one is a trick coin with heads on both sides. You choose a coin at random and see that one of the sides is heads. What is the probability that you chose the trick coin?\n", "\n", "(From [Think Bayes](https://colab.research.google.com/github/AllenDowney/ThinkBayes2/blob/master/notebooks/chap02.ipynb#scrollTo=gkvvYRv4gQwH) By Allen Downey )" ] }, { "cell_type": "markdown", "id": "4baf915c", "metadata": {}, "source": [ "## 1." ] }, { "cell_type": "code", "execution_count": 3, "id": "e28069ca", "metadata": {}, "outputs": [], "source": [ "from fractions import Fraction\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 10, "id": "80f9c06e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
prior
fair1/2
trick1/2
\n", "
" ], "text/plain": [ " prior\n", "fair 1/2\n", "trick 1/2" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coinTable = pd.DataFrame(index=['fair', 'trick'])\n", "coinTable[\"prior\"] = Fraction(1, 2) #prior probability is that you have 1/2 chance of choosing the trick coin\n", "coinTable" ] }, { "cell_type": "markdown", "id": "498a9e5c", "metadata": {}, "source": [ "For our likelihoods we have to consider probability of our data (seeing heads) given each hypothesis: \n", " \n", "- If we chose our fair coin, then the probability of getting heads would be 1/2\n", "- if we chose our trick coin, then the probability of getting heads would be 1" ] }, { "cell_type": "code", "execution_count": 12, "id": "69792a10", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
priorlikelihood
fair1/21/2
trick1/21
\n", "
" ], "text/plain": [ " prior likelihood\n", "fair 1/2 1/2\n", "trick 1/2 1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coinTable[\"likelihood\"] = Fraction(1, 2), 1\n", "coinTable\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "fd38d2eb", "metadata": {}, "outputs": [], "source": [ "def update(table):\n", " \"\"\"Compute the posterior probabilities.\"\"\"\n", " table['unnorm'] = table['prior'] * table['likelihood']\n", " prob_data = table['unnorm'].sum()\n", " table['posterior'] = table['unnorm'] / prob_data\n", " return prob_data" ] }, { "cell_type": "code", "execution_count": 15, "id": "ab7fa638", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
priorlikelihoodunnormposterior
fair1/21/21/41/3
trick1/211/22/3
\n", "
" ], "text/plain": [ " prior likelihood unnorm posterior\n", "fair 1/2 1/2 1/4 1/3\n", "trick 1/2 1 1/2 2/3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update(coinTable)\n", "coinTable" ] }, { "cell_type": "markdown", "id": "e1c67c9d", "metadata": {}, "source": [ "The probability that we chose the trick coin is 2/3" ] }, { "cell_type": "markdown", "id": "c7699866", "metadata": {}, "source": [ "## 2\n", "\n", "There are many variations of the Monty Hall problem.\n", "For example, suppose Monty always chooses Door 2 if he can, and only chooses Door 3 if he has to (because the car is behind Door 2).\n", "If you choose Door 1 and Monty opens Door 2, what is the probability the car is behind Door 3?\n", "If you choose Door 1 and Monty opens Door 3, what is the probability the car is behind Door 2?\n", "\n", "(From [Think Bayes](https://colab.research.google.com/github/AllenDowney/ThinkBayes2/blob/master/notebooks/chap02.ipynb#scrollTo=gkvvYRv4gQwH) By Allen Downey)" ] }, { "cell_type": "markdown", "id": "267a751b", "metadata": {}, "source": [ "## Solution\n", "\n", "Our prior is the same" ] }, { "cell_type": "code", "execution_count": 17, "id": "5069e54a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
prior
Door 11/3
Door 21/3
Door 31/3
\n", "
" ], "text/plain": [ " prior\n", "Door 1 1/3\n", "Door 2 1/3\n", "Door 3 1/3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])\n", "MHtable['prior'] = Fraction(1, 3)\n", "MHtable" ] }, { "cell_type": "markdown", "id": "e939e6a0", "metadata": {}, "source": [ "But now our likelihood is different. We go through the possible hypothesis: " ] }, { "cell_type": "markdown", "id": "6729ed32", "metadata": {}, "source": [ "### First scenario: You choose door 1, monty opens door 2\n", "\n", "- If the car is behind door 1, then monty chooses door 2 (hecause he always does if he can), then probability of monty choosing door 2 is 1\n", "- if the car is behind door 2, then monty would not open door 2, thus the probability of him choosing door 2 is 0\n", "- if the car is behind door 3, then monty would open door 2, so again the probability is 1" ] }, { "cell_type": "code", "execution_count": 18, "id": "8f60da58", "metadata": {}, "outputs": [], "source": [ "MHtable[\"likelihood\"] = 1,0,1" ] }, { "cell_type": "code", "execution_count": 19, "id": "0c0ac208", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(2, 3)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update(MHtable)" ] }, { "cell_type": "code", "execution_count": 20, "id": "5b519e71", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
priorlikelihoodunnormposterior
Door 11/311/31/2
Door 21/3000
Door 31/311/31/2
\n", "
" ], "text/plain": [ " prior likelihood unnorm posterior\n", "Door 1 1/3 1 1/3 1/2\n", "Door 2 1/3 0 0 0\n", "Door 3 1/3 1 1/3 1/2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MHtable" ] }, { "cell_type": "markdown", "id": "301183df", "metadata": {}, "source": [ "So in this case, we really do have a 50/50 chance" ] }, { "cell_type": "markdown", "id": "f96a5b58", "metadata": {}, "source": [ "### Second scenario: You choose door 1, monty opens door 3\n", "\n", "- If the car is behind door 1, then monty would only choose door 3 if he had to (the car is behind door 2), so this has 0 probability\n", "\n", "- If the car is behind door 2, then monty would choose door 3 with probability 1\n", "\n", "- If the car is behind door 3, then monty would not open door 3, so 0 probability" ] }, { "cell_type": "code", "execution_count": 24, "id": "d8605732", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(1, 3)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])\n", "MHtable['prior'] = Fraction(1, 3)\n", "MHtable['likelihood'] = 0,1,0\n", "update(MHtable)" ] }, { "cell_type": "code", "execution_count": 25, "id": "274fd73a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
priorlikelihoodunnormposterior
Door 11/3000
Door 21/311/31
Door 31/3000
\n", "
" ], "text/plain": [ " prior likelihood unnorm posterior\n", "Door 1 1/3 0 0 0\n", "Door 2 1/3 1 1/3 1\n", "Door 3 1/3 0 0 0" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MHtable" ] }, { "cell_type": "markdown", "id": "a2978379", "metadata": {}, "source": [ "In this case, we are 100 percent certain that car is behind door 2" ] }, { "cell_type": "markdown", "id": "2bccd0f9", "metadata": {}, "source": [ "## 3 Decision Analysis: 9.8 (ROS Exercise 9.8, p.129)\n", "\n", "Simulation for decision analysis: An experiment is performed to measure the efficacy of a television advertising program. The result is an estimate that each minute spent on a national advertising program will increase sales by € 500 000 (you can assume no variable costs for this product), and this estimate has a standard error of € 200 000. Assume the uncertainty in the treatment effect can be approximated by a normal distribution. Suppose ads cost € 300 000 per minute. \n", "\n", "- What is the expected net gain for purchasing 20 minutes of ads? \n", "\n", "- What is the probability that the net gain is negative?\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "2ce1fd2d", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 31, "id": "dcc5fe74", "metadata": {}, "outputs": [], "source": [ "nsim = 10000\n", "salesPerMin = np.random.normal(500000,200000, nsim)\n", "\n", "netGain = salesPerMin - 300000 \n" ] }, { "cell_type": "code", "execution_count": 32, "id": "4464f7e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "201621.25464952042" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netGain.mean()" ] }, { "cell_type": "markdown", "id": "48fbb8a5", "metadata": {}, "source": [ "mean netGain of ~ 200 000" ] }, { "cell_type": "code", "execution_count": 39, "id": "6e00e369", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1578" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netGain[netGain<0].size/nsim" ] }, { "cell_type": "markdown", "id": "90a65c1b", "metadata": {}, "source": [ "There is an approximately 15% chance that you get a net loss" ] }, { "cell_type": "code", "execution_count": null, "id": "5c477d2b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.12 ('base')", "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.12" }, "vscode": { "interpreter": { "hash": "2fb43c38bed9d5b7166100f7f30792a5c812b5092643be43839dfc9177fc92e6" } } }, "nbformat": 4, "nbformat_minor": 5 }