{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Decision Analysis" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Think Bayes, Second Edition\n", "\n", "Copyright 2020 Allen B. Downey\n", "\n", "License: [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:41.807708Z", "iopub.status.busy": "2021-04-16T19:35:41.807149Z", "iopub.status.idle": "2021-04-16T19:35:41.810352Z", "shell.execute_reply": "2021-04-16T19:35:41.809718Z" }, "tags": [] }, "outputs": [], "source": [ "# If we're running on Colab, install empiricaldist\n", "# https://pypi.org/project/empiricaldist/\n", "\n", "import sys\n", "IN_COLAB = 'google.colab' in sys.modules\n", "\n", "if IN_COLAB:\n", " !pip install empiricaldist" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:41.814888Z", "iopub.status.busy": "2021-04-16T19:35:41.814343Z", "iopub.status.idle": "2021-04-16T19:35:41.816295Z", "shell.execute_reply": "2021-04-16T19:35:41.816736Z" }, "tags": [] }, "outputs": [], "source": [ "# Get utils.py\n", "\n", "from os.path import basename, exists\n", "\n", "def download(url):\n", " filename = basename(url)\n", " if not exists(filename):\n", " from urllib.request import urlretrieve\n", " local, _ = urlretrieve(url, filename)\n", " print('Downloaded ' + local)\n", " \n", "download('https://github.com/AllenDowney/ThinkBayes2/raw/master/soln/utils.py')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:41.820085Z", "iopub.status.busy": "2021-04-16T19:35:41.819538Z", "iopub.status.idle": "2021-04-16T19:35:42.501987Z", "shell.execute_reply": "2021-04-16T19:35:42.501513Z" }, "tags": [] }, "outputs": [], "source": [ "from utils import set_pyplot_params\n", "set_pyplot_params()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This chapter presents a problem inspired by the game show *The Price is Right*.\n", "It is a silly example, but it demonstrates a useful process called Bayesian [decision analysis](https://en.wikipedia.org/wiki/Decision_analysis).\n", "\n", "As in previous examples, we'll use data and prior distribution to compute a posterior distribution; then we'll use the posterior distribution to choose an optimal strategy in a game that involves bidding.\n", "\n", "As part of the solution, we will use kernel density estimation (KDE) to estimate the prior distribution, and a normal distribution to compute the likelihood of the data.\n", "\n", "And at the end of the chapter, I pose a related problem you can solve as an exercise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Price Is Right Problem\n", "\n", "On November 1, 2007, contestants named Letia and Nathaniel appeared on *The Price is Right*, an American television game show. They competed in a game called \"The Showcase\", where the objective is to guess the price of a collection of prizes. The contestant who comes closest to the actual price, without going over, wins the prizes.\n", "\n", "Nathaniel went first. His showcase included a dishwasher, a wine cabinet, a laptop computer, and a car. He bid \\\\$26,000.\n", "\n", "Letia's showcase included a pinball machine, a video arcade game, a pool table, and a cruise of the Bahamas. She bid \\\\$21,500.\n", "\n", "The actual price of Nathaniel's showcase was \\\\$25,347. His bid was too high, so he lost.\n", "\n", "The actual price of Letia's showcase was \\\\$21,578. \n", "\n", "She was only off by \\\\$78, so she won her showcase and, because her bid was off by less than 250, she also won Nathaniel's showcase." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a Bayesian thinker, this scenario suggests several questions:\n", "\n", "1. Before seeing the prizes, what prior beliefs should the contestants have about the price of the showcase?\n", "\n", "2. After seeing the prizes, how should the contestants update those beliefs?\n", "\n", "3. Based on the posterior distribution, what should the contestants bid?\n", "\n", "The third question demonstrates a common use of Bayesian methods: decision analysis.\n", "\n", "This problem is inspired by [an example](https://nbviewer.jupyter.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter5_LossFunctions/Ch5_LossFunctions_PyMC3.ipynb) in Cameron Davidson-Pilon's book, [*Probablistic Programming and Bayesian Methods for Hackers*](http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Prior\n", "\n", "To choose a prior distribution of prices, we can take advantage of data from previous episodes. Fortunately, [fans of the show keep detailed records](https://web.archive.org/web/20121107204942/http://www.tpirsummaries.8m.com/). \n", "\n", "For this example, I downloaded files containing the price of each showcase from the 2011 and 2012 seasons and the bids offered by the contestants." ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "The following cells load the data files." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.507523Z", "iopub.status.busy": "2021-04-16T19:35:42.506995Z", "iopub.status.idle": "2021-04-16T19:35:42.508609Z", "shell.execute_reply": "2021-04-16T19:35:42.509016Z" }, "tags": [] }, "outputs": [], "source": [ "# Load the data files\n", "\n", "download('https://raw.githubusercontent.com/AllenDowney/ThinkBayes2/master/data/showcases.2011.csv')\n", "download('https://raw.githubusercontent.com/AllenDowney/ThinkBayes2/master/data/showcases.2012.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function reads the data and cleans it up a little." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.513237Z", "iopub.status.busy": "2021-04-16T19:35:42.512673Z", "iopub.status.idle": "2021-04-16T19:35:42.515256Z", "shell.execute_reply": "2021-04-16T19:35:42.514810Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "def read_data(filename):\n", " \"\"\"Read the showcase price data.\"\"\"\n", " df = pd.read_csv(filename, index_col=0, skiprows=[1])\n", " return df.dropna().transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I'll read both files and concatenate them." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.519025Z", "iopub.status.busy": "2021-04-16T19:35:42.518583Z", "iopub.status.idle": "2021-04-16T19:35:42.547366Z", "shell.execute_reply": "2021-04-16T19:35:42.546804Z" } }, "outputs": [], "source": [ "df2011 = read_data('showcases.2011.csv')\n", "df2012 = read_data('showcases.2012.csv')\n", "\n", "df = pd.concat([df2011, df2012], ignore_index=True)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.550926Z", "iopub.status.busy": "2021-04-16T19:35:42.550422Z", "iopub.status.idle": "2021-04-16T19:35:42.553032Z", "shell.execute_reply": "2021-04-16T19:35:42.552623Z" }, "tags": [] }, "outputs": [], "source": [ "print(df2011.shape, df2012.shape, df.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the dataset looks like:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.558946Z", "iopub.status.busy": "2021-04-16T19:35:42.557889Z", "iopub.status.idle": "2021-04-16T19:35:42.570199Z", "shell.execute_reply": "2021-04-16T19:35:42.570579Z" } }, "outputs": [], "source": [ "df.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first two columns, `Showcase 1` and `Showcase 2`, are the values of the showcases in dollars.\n", "The next two columns are the bids the contestants made.\n", "The last two columns are the differences between the actual values and the bids." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kernel Density Estimation\n", "\n", "This dataset contains the prices for 313 previous showcases, which we can think of as a sample from the population of possible prices.\n", "\n", "We can use this sample to estimate the prior distribution of showcase prices. One way to do that is kernel density estimation (KDE), which uses the sample to estimate a smooth distribution. If you are not familiar with KDE, you can [read about it here](https://mathisonian.github.io/kde).\n", "\n", "SciPy provides `gaussian_kde`, which takes a sample and returns an object that represents the estimated distribution.\n", "\n", "The following function takes `sample`, makes a KDE, evaluates it at a given sequence of quantities, `qs`, and returns the result as a normalized PMF." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.574533Z", "iopub.status.busy": "2021-04-16T19:35:42.574043Z", "iopub.status.idle": "2021-04-16T19:35:42.576062Z", "shell.execute_reply": "2021-04-16T19:35:42.575582Z" } }, "outputs": [], "source": [ "from scipy.stats import gaussian_kde\n", "from empiricaldist import Pmf\n", "\n", "def kde_from_sample(sample, qs):\n", " \"\"\"Make a kernel density estimate from a sample.\"\"\"\n", " kde = gaussian_kde(sample)\n", " ps = kde(qs)\n", " pmf = Pmf(ps, qs)\n", " pmf.normalize()\n", " return pmf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use it to estimate the distribution of values for Showcase 1:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.584733Z", "iopub.status.busy": "2021-04-16T19:35:42.580411Z", "iopub.status.idle": "2021-04-16T19:35:42.587005Z", "shell.execute_reply": "2021-04-16T19:35:42.586599Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "qs = np.linspace(0, 80000, 81)\n", "prior1 = kde_from_sample(df['Showcase 1'], qs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what it looks like:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.590496Z", "iopub.status.busy": "2021-04-16T19:35:42.590066Z", "iopub.status.idle": "2021-04-16T19:35:42.592375Z", "shell.execute_reply": "2021-04-16T19:35:42.591868Z" }, "tags": [] }, "outputs": [], "source": [ "from utils import decorate\n", "\n", "def decorate_value(title=''):\n", " decorate(xlabel='Showcase value ($)',\n", " ylabel='PMF',\n", " title=title)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.596416Z", "iopub.status.busy": "2021-04-16T19:35:42.595823Z", "iopub.status.idle": "2021-04-16T19:35:42.800967Z", "shell.execute_reply": "2021-04-16T19:35:42.801311Z" }, "tags": [] }, "outputs": [], "source": [ "prior1.plot(label='Prior 1')\n", "decorate_value('Prior distribution of showcase value')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Use this function to make a `Pmf` that represents the prior distribution for Showcase 2, and plot it." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.807216Z", "iopub.status.busy": "2021-04-16T19:35:42.806692Z", "iopub.status.idle": "2021-04-16T19:35:42.809224Z", "shell.execute_reply": "2021-04-16T19:35:42.808685Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:42.856760Z", "iopub.status.busy": "2021-04-16T19:35:42.856256Z", "iopub.status.idle": "2021-04-16T19:35:43.013351Z", "shell.execute_reply": "2021-04-16T19:35:43.013757Z" }, "scrolled": true }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Distribution of Error\n", "\n", "To update these priors, we have to answer these questions:\n", "\n", "* What data should we consider and how should we quantify it?\n", "\n", "* Can we compute a likelihood function; that is, for each hypothetical price, can we compute the conditional likelihood of the data?\n", "\n", "To answer these questions, I will model each contestant as a price-guessing instrument with known error characteristics. \n", "In this model, when the contestant sees the prizes, they guess the price of each prize and add up the prices.\n", "Let's call this total `guess`.\n", "\n", "Now the question we have to answer is, \"If the actual price is `price`, what is the likelihood that the contestant's guess would be `guess`?\"\n", "\n", "Equivalently, if we define `error = guess - price`, we can ask, \"What is the likelihood that the contestant's guess is off by `error`?\"\n", "\n", "To answer this question, I'll use the historical data again. \n", "For each showcase in the dataset, let's look at the difference between the contestant's bid and the actual price:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.018111Z", "iopub.status.busy": "2021-04-16T19:35:43.017611Z", "iopub.status.idle": "2021-04-16T19:35:43.019631Z", "shell.execute_reply": "2021-04-16T19:35:43.019982Z" } }, "outputs": [], "source": [ "sample_diff1 = df['Bid 1'] - df['Showcase 1']\n", "sample_diff2 = df['Bid 2'] - df['Showcase 2']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To visualize the distribution of these differences, we can use KDE again." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.027071Z", "iopub.status.busy": "2021-04-16T19:35:43.026529Z", "iopub.status.idle": "2021-04-16T19:35:43.028680Z", "shell.execute_reply": "2021-04-16T19:35:43.029157Z" } }, "outputs": [], "source": [ "qs = np.linspace(-40000, 20000, 61)\n", "kde_diff1 = kde_from_sample(sample_diff1, qs)\n", "kde_diff2 = kde_from_sample(sample_diff2, qs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what these distributions look like:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.075959Z", "iopub.status.busy": "2021-04-16T19:35:43.048711Z", "iopub.status.idle": "2021-04-16T19:35:43.205035Z", "shell.execute_reply": "2021-04-16T19:35:43.205620Z" }, "tags": [] }, "outputs": [], "source": [ "kde_diff1.plot(label='Diff 1', color='C8')\n", "kde_diff2.plot(label='Diff 2', color='C4')\n", "\n", "decorate(xlabel='Difference in value ($)',\n", " ylabel='PMF',\n", " title='Difference between bid and actual value')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like the bids are too low more often than too high, which makes sense. Remember that under the rules of the game, you lose if you overbid, so contestants probably underbid to some degree deliberately.\n", "\n", "For example, if they guess that the value of the showcase is \\\\$40,000, they might bid \\\\$36,000 to avoid going over." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like these distributions are well modeled by a normal distribution, so we can summarize them with their mean and standard deviation.\n", "\n", "For example, here is the mean and standard deviation of `Diff` for Player 1." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.209840Z", "iopub.status.busy": "2021-04-16T19:35:43.209325Z", "iopub.status.idle": "2021-04-16T19:35:43.211530Z", "shell.execute_reply": "2021-04-16T19:35:43.211881Z" } }, "outputs": [], "source": [ "mean_diff1 = sample_diff1.mean()\n", "std_diff1 = sample_diff1.std()\n", "\n", "print(mean_diff1, std_diff1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use these differences to model the contestant's distribution of errors.\n", "This step is a little tricky because we don't actually know the contestant's guesses; we only know what they bid.\n", "\n", "So we have to make some assumptions:\n", "\n", "* I'll assume that contestants underbid because they are being strategic, and that on average their guesses are accurate. In other words, the mean of their errors is 0.\n", "\n", "* But I'll assume that the spread of the differences reflects the actual spread of their errors. So, I'll use the standard deviation of the differences as the standard deviation of their errors.\n", "\n", "Based on these assumptions, I'll make a normal distribution with parameters 0 and `std_diff1`.\n", "\n", "SciPy provides an object called `norm` that represents a normal distribution with the given mean and standard deviation." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.215986Z", "iopub.status.busy": "2021-04-16T19:35:43.215405Z", "iopub.status.idle": "2021-04-16T19:35:43.217589Z", "shell.execute_reply": "2021-04-16T19:35:43.217195Z" } }, "outputs": [], "source": [ "from scipy.stats import norm\n", "\n", "error_dist1 = norm(0, std_diff1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is an object that provides `pdf`, which evaluates the probability density function of the normal distribution.\n", "\n", "For example, here is the probability density of `error=-100`, based on the distribution of errors for Player 1." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.221442Z", "iopub.status.busy": "2021-04-16T19:35:43.220947Z", "iopub.status.idle": "2021-04-16T19:35:43.223380Z", "shell.execute_reply": "2021-04-16T19:35:43.223743Z" } }, "outputs": [], "source": [ "error = -100\n", "error_dist1.pdf(error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By itself, this number doesn't mean very much, because probability densities are not probabilities. But they are proportional to probabilities, so we can use them as likelihoods in a Bayesian update, as we'll see in the next section." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update\n", "\n", "Suppose you are Player 1. You see the prizes in your showcase and your guess for the total price is \\\\$23,000.\n", "\n", "From your guess I will subtract away each hypothetical price in the prior distribution; the result is your error under each hypothesis." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.226913Z", "iopub.status.busy": "2021-04-16T19:35:43.226469Z", "iopub.status.idle": "2021-04-16T19:35:43.228074Z", "shell.execute_reply": "2021-04-16T19:35:43.228429Z" } }, "outputs": [], "source": [ "guess1 = 23000\n", "error1 = guess1 - prior1.qs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now suppose we know, based on past performance, that your estimation error is well modeled by `error_dist1`.\n", "Under that assumption we can compute the likelihood of your error under each hypothesis." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.231953Z", "iopub.status.busy": "2021-04-16T19:35:43.231397Z", "iopub.status.idle": "2021-04-16T19:35:43.233519Z", "shell.execute_reply": "2021-04-16T19:35:43.233990Z" } }, "outputs": [], "source": [ "likelihood1 = error_dist1.pdf(error1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is an array of likelihoods, which we can use to update the prior." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.239353Z", "iopub.status.busy": "2021-04-16T19:35:43.238671Z", "iopub.status.idle": "2021-04-16T19:35:43.241477Z", "shell.execute_reply": "2021-04-16T19:35:43.241931Z" }, "tags": [] }, "outputs": [], "source": [ "posterior1 = prior1 * likelihood1\n", "posterior1.normalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the posterior distribution looks like:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.288160Z", "iopub.status.busy": "2021-04-16T19:35:43.287654Z", "iopub.status.idle": "2021-04-16T19:35:43.431834Z", "shell.execute_reply": "2021-04-16T19:35:43.432442Z" }, "tags": [] }, "outputs": [], "source": [ "prior1.plot(color='C5', label='Prior 1')\n", "posterior1.plot(color='C4', label='Posterior 1')\n", "\n", "decorate_value('Prior and posterior distribution of showcase value')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because your initial guess is in the lower end of the range, the posterior distribution has shifted to the left. We can compute the posterior mean to see by how much." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.436831Z", "iopub.status.busy": "2021-04-16T19:35:43.436115Z", "iopub.status.idle": "2021-04-16T19:35:43.440717Z", "shell.execute_reply": "2021-04-16T19:35:43.441327Z" } }, "outputs": [], "source": [ "prior1.mean(), posterior1.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before you saw the prizes, you expected to see a showcase with a value close to \\\\$30,000.\n", "After making a guess of \\\\$23,000, you updated the prior distribution.\n", "Based on the combination of the prior and your guess, you now expect the actual price to be about \\\\$26,000." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Now suppose you are Player 2. When you see your showcase, you guess that the total price is \\\\$38,000.\n", "\n", "Use `diff2` to construct a normal distribution that represents the distribution of your estimation errors.\n", "\n", "Compute the likelihood of your guess for each actual price and use it to update `prior2`.\n", "\n", "Plot the posterior distribution and compute the posterior mean. Based on the prior and your guess, what do you expect the actual price of the showcase to be?" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.446313Z", "iopub.status.busy": "2021-04-16T19:35:43.445597Z", "iopub.status.idle": "2021-04-16T19:35:43.448186Z", "shell.execute_reply": "2021-04-16T19:35:43.448776Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.453527Z", "iopub.status.busy": "2021-04-16T19:35:43.452814Z", "iopub.status.idle": "2021-04-16T19:35:43.454765Z", "shell.execute_reply": "2021-04-16T19:35:43.455306Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.459870Z", "iopub.status.busy": "2021-04-16T19:35:43.459110Z", "iopub.status.idle": "2021-04-16T19:35:43.461269Z", "shell.execute_reply": "2021-04-16T19:35:43.461881Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.468130Z", "iopub.status.busy": "2021-04-16T19:35:43.467318Z", "iopub.status.idle": "2021-04-16T19:35:43.472187Z", "shell.execute_reply": "2021-04-16T19:35:43.471584Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.549426Z", "iopub.status.busy": "2021-04-16T19:35:43.506440Z", "iopub.status.idle": "2021-04-16T19:35:43.768369Z", "shell.execute_reply": "2021-04-16T19:35:43.769420Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.773670Z", "iopub.status.busy": "2021-04-16T19:35:43.772931Z", "iopub.status.idle": "2021-04-16T19:35:43.776721Z", "shell.execute_reply": "2021-04-16T19:35:43.777489Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Probability of Winning\n", "\n", "Now that we have a posterior distribution for each player, let's think about strategy.\n", "\n", "First, from the point of view of Player 1, let's compute the probability that Player 2 overbids. To keep it simple, I'll use only the performance of past players, ignoring the value of the showcase. \n", "\n", "The following function takes a sequence of past bids and returns the fraction that overbid." ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.782090Z", "iopub.status.busy": "2021-04-16T19:35:43.781438Z", "iopub.status.idle": "2021-04-16T19:35:43.785129Z", "shell.execute_reply": "2021-04-16T19:35:43.785964Z" } }, "outputs": [], "source": [ "def prob_overbid(sample_diff):\n", " \"\"\"Compute the probability of an overbid.\"\"\"\n", " return np.mean(sample_diff > 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's an estimate for the probability that Player 2 overbids." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.791906Z", "iopub.status.busy": "2021-04-16T19:35:43.791267Z", "iopub.status.idle": "2021-04-16T19:35:43.798095Z", "shell.execute_reply": "2021-04-16T19:35:43.797335Z" } }, "outputs": [], "source": [ "prob_overbid(sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now suppose Player 1 underbids by \\\\$5000.\n", "What is the probability that Player 2 underbids by more?\n", "\n", "The following function uses past performance to estimate the probability that a player underbids by more than a given amount, `diff`:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.803290Z", "iopub.status.busy": "2021-04-16T19:35:43.802451Z", "iopub.status.idle": "2021-04-16T19:35:43.807092Z", "shell.execute_reply": "2021-04-16T19:35:43.806213Z" } }, "outputs": [], "source": [ "def prob_worse_than(diff, sample_diff):\n", " \"\"\"Probability opponent diff is worse than given diff.\"\"\"\n", " return np.mean(sample_diff < diff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the probability that Player 2 underbids by more than \\\\$5000." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.812801Z", "iopub.status.busy": "2021-04-16T19:35:43.812084Z", "iopub.status.idle": "2021-04-16T19:35:43.820076Z", "shell.execute_reply": "2021-04-16T19:35:43.819320Z" } }, "outputs": [], "source": [ "prob_worse_than(-5000, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's the probability they underbid by more than \\\\$10,000." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.826928Z", "iopub.status.busy": "2021-04-16T19:35:43.826246Z", "iopub.status.idle": "2021-04-16T19:35:43.830095Z", "shell.execute_reply": "2021-04-16T19:35:43.830762Z" } }, "outputs": [], "source": [ "prob_worse_than(-10000, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can combine these functions to compute the probability that Player 1 wins, given the difference between their bid and the actual price:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.837850Z", "iopub.status.busy": "2021-04-16T19:35:43.837006Z", "iopub.status.idle": "2021-04-16T19:35:43.840521Z", "shell.execute_reply": "2021-04-16T19:35:43.839477Z" } }, "outputs": [], "source": [ "def compute_prob_win(diff, sample_diff):\n", " \"\"\"Probability of winning for a given diff.\"\"\"\n", " # if you overbid you lose\n", " if diff > 0:\n", " return 0\n", " \n", " # if the opponent overbids, you win\n", " p1 = prob_overbid(sample_diff)\n", " \n", " # or of their bid is worse than yours, you win\n", " p2 = prob_worse_than(diff, sample_diff)\n", " \n", " # p1 and p2 are mutually exclusive, so we can add them\n", " return p1 + p2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the probability that you win, given that you underbid by \\\\$5000." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.846964Z", "iopub.status.busy": "2021-04-16T19:35:43.846152Z", "iopub.status.idle": "2021-04-16T19:35:43.848940Z", "shell.execute_reply": "2021-04-16T19:35:43.849568Z" } }, "outputs": [], "source": [ "compute_prob_win(-5000, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's look at the probability of winning for a range of possible differences." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.900383Z", "iopub.status.busy": "2021-04-16T19:35:43.880078Z", "iopub.status.idle": "2021-04-16T19:35:43.945743Z", "shell.execute_reply": "2021-04-16T19:35:43.945174Z" } }, "outputs": [], "source": [ "xs = np.linspace(-30000, 5000, 121)\n", "ys = [compute_prob_win(x, sample_diff2) \n", " for x in xs]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what it looks like:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:43.983597Z", "iopub.status.busy": "2021-04-16T19:35:43.976147Z", "iopub.status.idle": "2021-04-16T19:35:44.200342Z", "shell.execute_reply": "2021-04-16T19:35:44.199432Z" }, "tags": [] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "plt.plot(xs, ys)\n", "\n", "decorate(xlabel='Difference between bid and actual price ($)',\n", " ylabel='Probability of winning',\n", " title='Player 1')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you underbid by \\\\$30,000, the chance of winning is about 30%, which is mostly the chance your opponent overbids.\n", "\n", "As your bids gets closer to the actual price, your chance of winning approaches 1.\n", "\n", "And, of course, if you overbid, you lose (even if your opponent also overbids)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Run the same analysis from the point of view of Player 2. Using the sample of differences from Player 1, compute:\n", "\n", "1. The probability that Player 1 overbids.\n", "\n", "2. The probability that Player 1 underbids by more than \\\\$5000.\n", "\n", "3. The probability that Player 2 wins, given that they underbid by \\\\$5000.\n", "\n", "Then plot the probability that Player 2 wins for a range of possible differences between their bid and the actual price." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.207596Z", "iopub.status.busy": "2021-04-16T19:35:44.206660Z", "iopub.status.idle": "2021-04-16T19:35:44.210043Z", "shell.execute_reply": "2021-04-16T19:35:44.210868Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.218842Z", "iopub.status.busy": "2021-04-16T19:35:44.217621Z", "iopub.status.idle": "2021-04-16T19:35:44.221106Z", "shell.execute_reply": "2021-04-16T19:35:44.222054Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.230669Z", "iopub.status.busy": "2021-04-16T19:35:44.229367Z", "iopub.status.idle": "2021-04-16T19:35:44.233645Z", "shell.execute_reply": "2021-04-16T19:35:44.234317Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.252935Z", "iopub.status.busy": "2021-04-16T19:35:44.252459Z", "iopub.status.idle": "2021-04-16T19:35:44.313662Z", "shell.execute_reply": "2021-04-16T19:35:44.314024Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.357956Z", "iopub.status.busy": "2021-04-16T19:35:44.339391Z", "iopub.status.idle": "2021-04-16T19:35:44.564128Z", "shell.execute_reply": "2021-04-16T19:35:44.563691Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Decision Analysis\n", "\n", "In the previous section we computed the probability of winning, given that we have underbid by a particular amount.\n", "\n", "In reality the contestants don't know how much they have underbid by, because they don't know the actual price.\n", "\n", "But they do have a posterior distribution that represents their beliefs about the actual price, and they can use that to estimate their probability of winning with a given bid.\n", "\n", "The following function takes a possible bid, a posterior distribution of actual prices, and a sample of differences for the opponent.\n", "\n", "It loops through the hypothetical prices in the posterior distribution and, for each price,\n", "\n", "1. Computes the difference between the bid and the hypothetical price,\n", "\n", "2. Computes the probability that the player wins, given that difference, and\n", "\n", "3. Adds up the weighted sum of the probabilities, where the weights are the probabilities in the posterior distribution. " ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.570768Z", "iopub.status.busy": "2021-04-16T19:35:44.570139Z", "iopub.status.idle": "2021-04-16T19:35:44.574093Z", "shell.execute_reply": "2021-04-16T19:35:44.573672Z" } }, "outputs": [], "source": [ "def total_prob_win(bid, posterior, sample_diff):\n", " \"\"\"Computes the total probability of winning with a given bid.\n", "\n", " bid: your bid\n", " posterior: Pmf of showcase value\n", " sample_diff: sequence of differences for the opponent\n", " \n", " returns: probability of winning\n", " \"\"\"\n", " total = 0\n", " for price, prob in posterior.items():\n", " diff = bid - price\n", " total += prob * compute_prob_win(diff, sample_diff)\n", " return total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This loop implements the law of total probability:\n", "\n", "$$P(win) = \\sum_{price} P(price) ~ P(win ~|~ price)$$\n", "\n", "Here's the probability that Player 1 wins, based on a bid of \\\\$25,000 and the posterior distribution `posterior1`." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.628806Z", "iopub.status.busy": "2021-04-16T19:35:44.612939Z", "iopub.status.idle": "2021-04-16T19:35:44.632948Z", "shell.execute_reply": "2021-04-16T19:35:44.633353Z" } }, "outputs": [], "source": [ "total_prob_win(25000, posterior1, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can loop through a series of possible bids and compute the probability of winning for each one." ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:44.708685Z", "iopub.status.busy": "2021-04-16T19:35:44.672661Z", "iopub.status.idle": "2021-04-16T19:35:46.270662Z", "shell.execute_reply": "2021-04-16T19:35:46.270147Z" } }, "outputs": [], "source": [ "bids = posterior1.qs\n", "\n", "probs = [total_prob_win(bid, posterior1, sample_diff2) \n", " for bid in bids]\n", "\n", "prob_win_series = pd.Series(probs, index=bids)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the results." ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:46.306239Z", "iopub.status.busy": "2021-04-16T19:35:46.291839Z", "iopub.status.idle": "2021-04-16T19:35:46.422364Z", "shell.execute_reply": "2021-04-16T19:35:46.421956Z" }, "tags": [] }, "outputs": [], "source": [ "prob_win_series.plot(label='Player 1', color='C1')\n", "\n", "decorate(xlabel='Bid ($)',\n", " ylabel='Probability of winning',\n", " title='Optimal bid: probability of winning')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's the bid that maximizes Player 1's chance of winning." ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:46.425882Z", "iopub.status.busy": "2021-04-16T19:35:46.425408Z", "iopub.status.idle": "2021-04-16T19:35:46.430103Z", "shell.execute_reply": "2021-04-16T19:35:46.429712Z" } }, "outputs": [], "source": [ "prob_win_series.idxmax()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:46.433880Z", "iopub.status.busy": "2021-04-16T19:35:46.433042Z", "iopub.status.idle": "2021-04-16T19:35:46.437889Z", "shell.execute_reply": "2021-04-16T19:35:46.437462Z" } }, "outputs": [], "source": [ "prob_win_series.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that your guess was \\\\$23,000.\n", "Using your guess to compute the posterior distribution, the posterior mean is about \\\\$26,000.\n", "But the bid that maximizes your chance of winning is \\\\$21,000." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Do the same analysis for Player 2." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:46.495866Z", "iopub.status.busy": "2021-04-16T19:35:46.460137Z", "iopub.status.idle": "2021-04-16T19:35:47.898014Z", "shell.execute_reply": "2021-04-16T19:35:47.897569Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:47.916760Z", "iopub.status.busy": "2021-04-16T19:35:47.915987Z", "iopub.status.idle": "2021-04-16T19:35:48.073976Z", "shell.execute_reply": "2021-04-16T19:35:48.073607Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.077656Z", "iopub.status.busy": "2021-04-16T19:35:48.077009Z", "iopub.status.idle": "2021-04-16T19:35:48.079401Z", "shell.execute_reply": "2021-04-16T19:35:48.079754Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.083673Z", "iopub.status.busy": "2021-04-16T19:35:48.082935Z", "iopub.status.idle": "2021-04-16T19:35:48.086036Z", "shell.execute_reply": "2021-04-16T19:35:48.085566Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Maximizing Expected Gain\n", "\n", "In the previous section we computed the bid that maximizes your chance of winning.\n", "And if that's your goal, the bid we computed is optimal.\n", "\n", "But winning isn't everything.\n", "Remember that if your bid is off by \\\\$250 or less, you win both showcases.\n", "So it might be a good idea to increase your bid a little: it increases the chance you overbid and lose, but it also increases the chance of winning both showcases.\n", "\n", "Let's see how that works out.\n", "The following function computes how much you will win, on average, given your bid, the actual price, and a sample of errors for your opponent." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.089830Z", "iopub.status.busy": "2021-04-16T19:35:48.089389Z", "iopub.status.idle": "2021-04-16T19:35:48.091415Z", "shell.execute_reply": "2021-04-16T19:35:48.091026Z" } }, "outputs": [], "source": [ "def compute_gain(bid, price, sample_diff):\n", " \"\"\"Compute expected gain given a bid and actual price.\"\"\"\n", " diff = bid - price\n", " prob = compute_prob_win(diff, sample_diff)\n", "\n", " # if you are within 250 dollars, you win both showcases\n", " if -250 <= diff <= 0:\n", " return 2 * price * prob\n", " else:\n", " return price * prob" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, if the actual price is \\\\$35000 \n", "and you bid \\\\$30000, \n", "you will win about \\\\$23,600 worth of prizes on average, taking into account your probability of losing, winning one showcase, or winning both." ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.095957Z", "iopub.status.busy": "2021-04-16T19:35:48.095151Z", "iopub.status.idle": "2021-04-16T19:35:48.098364Z", "shell.execute_reply": "2021-04-16T19:35:48.097922Z" } }, "outputs": [], "source": [ "compute_gain(30000, 35000, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In reality we don't know the actual price, but we have a posterior distribution that represents what we know about it.\n", "By averaging over the prices and probabilities in the posterior distribution, we can compute the expected gain for a particular bid.\n", "\n", "In this context, \"expected\" means the average over the possible showcase values, weighted by their probabilities." ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.102421Z", "iopub.status.busy": "2021-04-16T19:35:48.101903Z", "iopub.status.idle": "2021-04-16T19:35:48.104181Z", "shell.execute_reply": "2021-04-16T19:35:48.103665Z" } }, "outputs": [], "source": [ "def expected_gain(bid, posterior, sample_diff):\n", " \"\"\"Compute the expected gain of a given bid.\"\"\"\n", " total = 0\n", " for price, prob in posterior.items():\n", " total += prob * compute_gain(bid, price, sample_diff)\n", " return total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the posterior we computed earlier, based on a guess of \\\\$23,000, the expected gain for a bid of \\\\$21,000 is about \\\\$16,900." ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.137026Z", "iopub.status.busy": "2021-04-16T19:35:48.136553Z", "iopub.status.idle": "2021-04-16T19:35:48.139335Z", "shell.execute_reply": "2021-04-16T19:35:48.138880Z" } }, "outputs": [], "source": [ "expected_gain(21000, posterior1, sample_diff2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But can we do any better? \n", "\n", "To find out, we can loop through a range of bids and find the one that maximizes expected gain." ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:48.212459Z", "iopub.status.busy": "2021-04-16T19:35:48.176771Z", "iopub.status.idle": "2021-04-16T19:35:49.578032Z", "shell.execute_reply": "2021-04-16T19:35:49.577538Z" } }, "outputs": [], "source": [ "bids = posterior1.qs\n", "\n", "gains = [expected_gain(bid, posterior1, sample_diff2) for bid in bids]\n", "\n", "expected_gain_series = pd.Series(gains, index=bids)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the results." ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:49.595312Z", "iopub.status.busy": "2021-04-16T19:35:49.594806Z", "iopub.status.idle": "2021-04-16T19:35:49.758820Z", "shell.execute_reply": "2021-04-16T19:35:49.759161Z" }, "tags": [] }, "outputs": [], "source": [ "expected_gain_series.plot(label='Player 1', color='C2')\n", "\n", "decorate(xlabel='Bid ($)',\n", " ylabel='Expected gain ($)',\n", " title='Optimal bid: expected gain')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the optimal bid." ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:49.763145Z", "iopub.status.busy": "2021-04-16T19:35:49.762475Z", "iopub.status.idle": "2021-04-16T19:35:49.764913Z", "shell.execute_reply": "2021-04-16T19:35:49.765309Z" } }, "outputs": [], "source": [ "expected_gain_series.idxmax()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With that bid, the expected gain is about \\\\$17,400." ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:49.768943Z", "iopub.status.busy": "2021-04-16T19:35:49.768405Z", "iopub.status.idle": "2021-04-16T19:35:49.771158Z", "shell.execute_reply": "2021-04-16T19:35:49.770667Z" } }, "outputs": [], "source": [ "expected_gain_series.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that your initial guess was \\\\$23,000.\n", "The bid that maximizes the chance of winning is \\\\$21,000.\n", "And the bid that maximizes your expected gain is \\\\$22,000." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Do the same analysis for Player 2." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:49.833753Z", "iopub.status.busy": "2021-04-16T19:35:49.808430Z", "iopub.status.idle": "2021-04-16T19:35:51.251753Z", "shell.execute_reply": "2021-04-16T19:35:51.252169Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.336820Z", "iopub.status.busy": "2021-04-16T19:35:51.336306Z", "iopub.status.idle": "2021-04-16T19:35:51.527770Z", "shell.execute_reply": "2021-04-16T19:35:51.528265Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.532762Z", "iopub.status.busy": "2021-04-16T19:35:51.532102Z", "iopub.status.idle": "2021-04-16T19:35:51.535667Z", "shell.execute_reply": "2021-04-16T19:35:51.535248Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.555414Z", "iopub.status.busy": "2021-04-16T19:35:51.554109Z", "iopub.status.idle": "2021-04-16T19:35:51.557359Z", "shell.execute_reply": "2021-04-16T19:35:51.554778Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "There's a lot going on this this chapter, so let's review the steps:\n", "\n", "1. First we used KDE and data from past shows to estimate prior distributions for the values of the showcases.\n", "\n", "2. Then we used bids from past shows to model the distribution of errors as a normal distribution.\n", "\n", "3. We did a Bayesian update using the distribution of errors to compute the likelihood of the data.\n", "\n", "4. We used the posterior distribution for the value of the showcase to compute the probability of winning for each possible bid, and identified the bid that maximizes the chance of winning.\n", "\n", "5. Finally, we used probability of winning to compute the expected gain for each possible bid, and identified the bid that maximizes expected gain.\n", "\n", "Incidentally, this example demonstrates the hazard of using the word \"optimal\" without specifying what you are optimizing.\n", "The bid that maximizes the chance of winning is not generally the same as the bid that maximizes expected gain." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Discussion\n", "\n", "When people discuss the pros and cons of Bayesian estimation, as contrasted with classical methods sometimes called \"frequentist\", they often claim that in many cases Bayesian methods and frequentist methods produce the same results.\n", "\n", "In my opinion, this claim is mistaken because Bayesian and frequentist method produce different *kinds* of results:\n", "\n", "* The result of frequentist methods is usually a single value that is considered to be the best estimate (by one of several criteria) or an interval that quantifies the precision of the estimate.\n", "\n", "* The result of Bayesian methods is a posterior distribution that represents all possible outcomes and their probabilities." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Granted, you can use the posterior distribution to choose a \"best\" estimate or compute an interval.\n", "And in that case the result might be the same as the frequentist estimate.\n", "\n", "But doing so discards useful information and, in my opinion, eliminates the primary benefit of Bayesian methods: the posterior distribution is more useful than a single estimate, or even an interval." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The example in this chapter demonstrates the point.\n", "Using the entire posterior distribution, we can compute the bid that maximizes the probability of winning, or the bid that maximizes expected gain, even if the rules for computing the gain are complicated (and nonlinear).\n", "\n", "With a single estimate or an interval, we can't do that, even if they are \"optimal\" in some sense.\n", "In general, frequentist estimation provides little guidance for decision-making.\n", "\n", "If you hear someone say that Bayesian and frequentist methods produce the same results, you can be confident that they don't understand Bayesian methods." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** When I worked in Cambridge, Massachusetts, I usually took the subway to South Station and then a commuter train home to Needham. Because the subway was unpredictable, I left the office early enough that I could wait up to 15 minutes and still catch the commuter train.\n", "\n", "When I got to the subway stop, there were usually about 10 people waiting on the platform. If there were fewer than that, I figured I just missed a train, so I expected to wait a little longer than usual. And if there there more than that, I expected another train soon.\n", "\n", "But if there were a *lot* more than 10 passengers waiting, I inferred that something was wrong, and I expected a long wait. In that case, I might leave and take a taxi.\n", "\n", "We can use Bayesian decision analysis to quantify the analysis I did intuitively. Given the number of passengers on the platform, how long should we expect to wait? And when should we give up and take a taxi?\n", "\n", "My analysis of this problem is in `redline.ipynb`, which is in the repository for this book. [Click here to run this notebook on Colab](https://colab.research.google.com/github/AllenDowney/ThinkBayes2/blob/master/notebooks/redline.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** This exercise is inspired by a true story. In 2001 I created [Green Tea Press](https://greenteapress.com) to publish my books, starting with *Think Python*. I ordered 100 copies from a short run printer and made the book available for sale through a distributor. \n", "\n", "After the first week, the distributor reported that 12 copies were sold. Based that report, I thought I would run out of copies in about 8 weeks, so I got ready to order more. My printer offered me a discount if I ordered more than 1000 copies, so I went a little crazy and ordered 2000. \n", "\n", "A few days later, my mother called to tell me that her *copies* of the book had arrived. Surprised, I asked how many. She said ten.\n", "\n", "It turned out I had sold only two books to non-relatives. And it took a lot longer than I expected to sell 2000 copies." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The details of this story are unique, but the general problem is something almost every retailer has to figure out. Based on past sales, how do you predict future sales? And based on those predictions, how do you decide how much to order and when?\n", "\n", "Often the cost of a bad decision is complicated. If you place a lot of small orders rather than one big one, your costs are likely to be higher. If you run out of inventory, you might lose customers. And if you order too much, you have to pay the various costs of holding inventory.\n", "\n", "So, let's solve a version of the problem I faced. It will take some work to set up the problem; the details are in the notebook for this chapter." ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Suppose you start selling books online. During the first week you sell 10 copies (and let's assume that none of the customers are your mother). During the second week you sell 9 copies.\n", "\n", "Assuming that the arrival of orders is a Poisson process, we can think of the weekly orders as samples from a Poisson distribution with an unknown rate.\n", "We can use orders from past weeks to estimate the parameter of this distribution, generate a predictive distribution for future weeks, and compute the order size that maximized expected profit.\n", "\n", "* Suppose the cost of printing the book is \\\\$5 per copy, \n", "\n", "* But if you order 100 or more, it's \\\\$4.50 per copy.\n", "\n", "* For every book you sell, you get \\\\$10.\n", "\n", "* But if you run out of books before the end of 8 weeks, you lose \\\\$50 in future sales for every week you are out of stock.\n", "\n", "* If you have books left over at the end of 8 weeks, you lose \\\\$2 in inventory costs per extra book.\n", "\n", "For example, suppose you get orders for 10 books per week, every week. If you order 60 books, \n", "\n", "* The total cost is \\\\$300. \n", "\n", "* You sell all 60 books, so you make \\\\$600. \n", "\n", "* But the book is out of stock for two weeks, so you lose \\\\$100 in future sales.\n", "\n", "In total, your profit is \\\\$200.\n", "\n", "If you order 100 books,\n", "\n", "* The total cost is \\\\$450.\n", "\n", "* You sell 80 books, so you make \\\\$800.\n", "\n", "* But you have 20 books left over at the end, so you lose \\\\$40.\n", "\n", "In total, your profit is \\\\$310.\n", "\n", "Combining these costs with your predictive distribution, how many books should you order to maximize your expected profit?" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "To get you started, the following functions compute profits and costs according to the specification of the problem:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.562554Z", "iopub.status.busy": "2021-04-16T19:35:51.561855Z", "iopub.status.idle": "2021-04-16T19:35:51.565805Z", "shell.execute_reply": "2021-04-16T19:35:51.566720Z" }, "tags": [] }, "outputs": [], "source": [ "def print_cost(printed):\n", " \"\"\"Compute print costs.\n", " \n", " printed: integer number printed\n", " \"\"\"\n", " if printed < 100:\n", " return printed * 5\n", " else:\n", " return printed * 4.5" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.572879Z", "iopub.status.busy": "2021-04-16T19:35:51.572142Z", "iopub.status.idle": "2021-04-16T19:35:51.582185Z", "shell.execute_reply": "2021-04-16T19:35:51.582596Z" }, "tags": [] }, "outputs": [], "source": [ "def total_income(printed, orders):\n", " \"\"\"Compute income.\n", " \n", " printed: integer number printed\n", " orders: sequence of integer number of books ordered\n", " \"\"\"\n", " sold = min(printed, np.sum(orders))\n", " return sold * 10" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.587177Z", "iopub.status.busy": "2021-04-16T19:35:51.586735Z", "iopub.status.idle": "2021-04-16T19:35:51.588667Z", "shell.execute_reply": "2021-04-16T19:35:51.589058Z" }, "tags": [] }, "outputs": [], "source": [ "def inventory_cost(printed, orders):\n", " \"\"\"Compute inventory costs.\n", " \n", " printed: integer number printed\n", " orders: sequence of integer number of books ordered\n", " \"\"\"\n", " excess = printed - np.sum(orders)\n", " if excess > 0:\n", " return excess * 2\n", " else:\n", " return 0" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.594274Z", "iopub.status.busy": "2021-04-16T19:35:51.593740Z", "iopub.status.idle": "2021-04-16T19:35:51.597400Z", "shell.execute_reply": "2021-04-16T19:35:51.596976Z" }, "tags": [] }, "outputs": [], "source": [ "def out_of_stock_cost(printed, orders):\n", " \"\"\"Compute out of stock costs.\n", " \n", " printed: integer number printed\n", " orders: sequence of integer number of books ordered\n", " \"\"\"\n", " weeks = len(orders)\n", " total_orders = np.cumsum(orders)\n", " for i, total in enumerate(total_orders):\n", " if total > printed:\n", " return (weeks-i) * 50\n", " return 0" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.601347Z", "iopub.status.busy": "2021-04-16T19:35:51.600787Z", "iopub.status.idle": "2021-04-16T19:35:51.604170Z", "shell.execute_reply": "2021-04-16T19:35:51.604606Z" }, "tags": [] }, "outputs": [], "source": [ "def compute_profit(printed, orders):\n", " \"\"\"Compute profit.\n", " \n", " printed: integer number printed\n", " orders: sequence of integer number of books ordered\n", " \"\"\"\n", " return (total_income(printed, orders) -\n", " print_cost(printed)-\n", " out_of_stock_cost(printed, orders) -\n", " inventory_cost(printed, orders))" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "To test these functions, suppose we get exactly 10 orders per week for eight weeks:" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.611079Z", "iopub.status.busy": "2021-04-16T19:35:51.610241Z", "iopub.status.idle": "2021-04-16T19:35:51.616176Z", "shell.execute_reply": "2021-04-16T19:35:51.616554Z" }, "tags": [] }, "outputs": [], "source": [ "always_10 = [10] * 8\n", "always_10" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "If you print 60 books, your net profit is \\\\$200, as in the example." ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.620756Z", "iopub.status.busy": "2021-04-16T19:35:51.620229Z", "iopub.status.idle": "2021-04-16T19:35:51.622764Z", "shell.execute_reply": "2021-04-16T19:35:51.623275Z" }, "tags": [] }, "outputs": [], "source": [ "compute_profit(60, always_10)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "If you print 100 books, your net profit is \\\\$310." ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.628975Z", "iopub.status.busy": "2021-04-16T19:35:51.628140Z", "iopub.status.idle": "2021-04-16T19:35:51.631017Z", "shell.execute_reply": "2021-04-16T19:35:51.631678Z" }, "tags": [] }, "outputs": [], "source": [ "compute_profit(100, always_10)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Of course, in the context of the problem you don't know how many books will be ordered in any given week. You don't even know the average rate of orders. However, given the data and some assumptions about the prior, you can compute the distribution of the rate of orders.\n", "\n", "You'll have a chance to do that, but to demonstrate the decision analysis part of the problem, I'll start with the arbitrary assumption that order rates come from a gamma distribution with mean 9.\n", "\n", "Here's a `Pmf` that represents this distribution." ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.638252Z", "iopub.status.busy": "2021-04-16T19:35:51.637600Z", "iopub.status.idle": "2021-04-16T19:35:51.640800Z", "shell.execute_reply": "2021-04-16T19:35:51.641451Z" }, "tags": [] }, "outputs": [], "source": [ "from scipy.stats import gamma\n", "\n", "alpha = 9\n", "qs = np.linspace(0, 25, 101)\n", "ps = gamma.pdf(qs, alpha)\n", "pmf = Pmf(ps, qs)\n", "pmf.normalize()\n", "pmf.mean()" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "And here's what it looks like:" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.658650Z", "iopub.status.busy": "2021-04-16T19:35:51.658221Z", "iopub.status.idle": "2021-04-16T19:35:51.986329Z", "shell.execute_reply": "2021-04-16T19:35:51.985935Z" }, "tags": [] }, "outputs": [], "source": [ "pmf.plot(color='C1')\n", "decorate(xlabel=r'Book ordering rate ($\\lambda$)',\n", " ylabel='PMF')" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Now, we *could* generate a predictive distribution for the number of books ordered in a given week, but in this example we have to deal with a complicated cost function. In particular, `out_of_stock_cost` depends on the sequence of orders.\n", "\n", "So, rather than generate a predictive distribution, I suggest we run simulations. I'll demonstrate the steps.\n", "\n", "First, from our hypothetical distribution of rates, we can draw a random sample of 1000 values. " ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.990537Z", "iopub.status.busy": "2021-04-16T19:35:51.989846Z", "iopub.status.idle": "2021-04-16T19:35:51.993533Z", "shell.execute_reply": "2021-04-16T19:35:51.993896Z" }, "tags": [] }, "outputs": [], "source": [ "rates = pmf.choice(1000)\n", "np.mean(rates)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "For each possible rate, we can generate a sequence of 8 orders." ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:51.998261Z", "iopub.status.busy": "2021-04-16T19:35:51.997702Z", "iopub.status.idle": "2021-04-16T19:35:52.002977Z", "shell.execute_reply": "2021-04-16T19:35:52.003508Z" }, "tags": [] }, "outputs": [], "source": [ "np.random.seed(17)\n", "order_array = np.random.poisson(rates, size=(8, 1000)).transpose()\n", "order_array[:5, :]" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Each row of this array is a hypothetical sequence of orders based on a different hypothetical order rate.\n", "\n", "Now, if you tell me how many books you printed, I can compute your expected profits, averaged over these 1000 possible sequences." ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:52.007370Z", "iopub.status.busy": "2021-04-16T19:35:52.006916Z", "iopub.status.idle": "2021-04-16T19:35:52.008586Z", "shell.execute_reply": "2021-04-16T19:35:52.008935Z" }, "tags": [] }, "outputs": [], "source": [ "def compute_expected_profits(printed, order_array):\n", " \"\"\"Compute profits averaged over a sample of orders.\n", " \n", " printed: number printed\n", " order_array: one row per sample, one column per week\n", " \"\"\"\n", " profits = [compute_profit(printed, orders)\n", " for orders in order_array]\n", " return np.mean(profits)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "For example, here are the expected profits if you order 70, 80, or 90 books." ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:52.016373Z", "iopub.status.busy": "2021-04-16T19:35:52.015963Z", "iopub.status.idle": "2021-04-16T19:35:52.035494Z", "shell.execute_reply": "2021-04-16T19:35:52.035057Z" }, "tags": [] }, "outputs": [], "source": [ "compute_expected_profits(70, order_array)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:52.073335Z", "iopub.status.busy": "2021-04-16T19:35:52.072683Z", "iopub.status.idle": "2021-04-16T19:35:52.076415Z", "shell.execute_reply": "2021-04-16T19:35:52.076779Z" }, "tags": [] }, "outputs": [], "source": [ "compute_expected_profits(80, order_array)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:52.098735Z", "iopub.status.busy": "2021-04-16T19:35:52.098012Z", "iopub.status.idle": "2021-04-16T19:35:52.100689Z", "shell.execute_reply": "2021-04-16T19:35:52.100277Z" }, "tags": [] }, "outputs": [], "source": [ "compute_expected_profits(90, order_array)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Now, let's sweep through a range of values and compute expected profits as a function of the number of books you print." ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:52.151466Z", "iopub.status.busy": "2021-04-16T19:35:52.110313Z", "iopub.status.idle": "2021-04-16T19:35:53.044324Z", "shell.execute_reply": "2021-04-16T19:35:53.043782Z" }, "tags": [] }, "outputs": [], "source": [ "printed_array = np.arange(70, 110)\n", "t = [compute_expected_profits(printed, order_array)\n", " for printed in printed_array]\n", "expected_profits = pd.Series(t, printed_array)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.067959Z", "iopub.status.busy": "2021-04-16T19:35:53.061574Z", "iopub.status.idle": "2021-04-16T19:35:53.181791Z", "shell.execute_reply": "2021-04-16T19:35:53.181406Z" }, "tags": [] }, "outputs": [], "source": [ "expected_profits.plot(label='')\n", "\n", "decorate(xlabel='Number of books printed',\n", " ylabel='Expected profit ($)')" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Here is the optimal order and the expected profit." ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.185628Z", "iopub.status.busy": "2021-04-16T19:35:53.185129Z", "iopub.status.idle": "2021-04-16T19:35:53.189282Z", "shell.execute_reply": "2021-04-16T19:35:53.189626Z" }, "tags": [] }, "outputs": [], "source": [ "expected_profits.idxmax(), expected_profits.max()" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Now it's your turn. Choose a prior that you think is reasonable, update it with the data you are given, and then use the posterior distribution to do the analysis I just demonstrated." ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.194510Z", "iopub.status.busy": "2021-04-16T19:35:53.194020Z", "iopub.status.idle": "2021-04-16T19:35:53.198181Z", "shell.execute_reply": "2021-04-16T19:35:53.198701Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.216932Z", "iopub.status.busy": "2021-04-16T19:35:53.213964Z", "iopub.status.idle": "2021-04-16T19:35:53.323589Z", "shell.execute_reply": "2021-04-16T19:35:53.324119Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.327897Z", "iopub.status.busy": "2021-04-16T19:35:53.327424Z", "iopub.status.idle": "2021-04-16T19:35:53.330290Z", "shell.execute_reply": "2021-04-16T19:35:53.330803Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.335171Z", "iopub.status.busy": "2021-04-16T19:35:53.334695Z", "iopub.status.idle": "2021-04-16T19:35:53.338131Z", "shell.execute_reply": "2021-04-16T19:35:53.337723Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.342520Z", "iopub.status.busy": "2021-04-16T19:35:53.342028Z", "iopub.status.idle": "2021-04-16T19:35:53.344891Z", "shell.execute_reply": "2021-04-16T19:35:53.345436Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.348958Z", "iopub.status.busy": "2021-04-16T19:35:53.348514Z", "iopub.status.idle": "2021-04-16T19:35:53.352690Z", "shell.execute_reply": "2021-04-16T19:35:53.353204Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.356962Z", "iopub.status.busy": "2021-04-16T19:35:53.356103Z", "iopub.status.idle": "2021-04-16T19:35:53.359918Z", "shell.execute_reply": "2021-04-16T19:35:53.359487Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.363099Z", "iopub.status.busy": "2021-04-16T19:35:53.362692Z", "iopub.status.idle": "2021-04-16T19:35:53.368315Z", "shell.execute_reply": "2021-04-16T19:35:53.367822Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:53.428215Z", "iopub.status.busy": "2021-04-16T19:35:53.402097Z", "iopub.status.idle": "2021-04-16T19:35:54.443135Z", "shell.execute_reply": "2021-04-16T19:35:54.444074Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:54.467573Z", "iopub.status.busy": "2021-04-16T19:35:54.463557Z", "iopub.status.idle": "2021-04-16T19:35:54.727439Z", "shell.execute_reply": "2021-04-16T19:35:54.728002Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:54.734792Z", "iopub.status.busy": "2021-04-16T19:35:54.733641Z", "iopub.status.idle": "2021-04-16T19:35:54.738456Z", "shell.execute_reply": "2021-04-16T19:35:54.739144Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }