{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Poisson Processes" ] }, { "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:35.559580Z", "iopub.status.busy": "2021-04-16T19:35:35.558886Z", "iopub.status.idle": "2021-04-16T19:35:35.560949Z", "shell.execute_reply": "2021-04-16T19:35:35.561400Z" }, "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": 67, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:35.565677Z", "iopub.status.busy": "2021-04-16T19:35:35.564851Z", "iopub.status.idle": "2021-04-16T19:35:35.567533Z", "shell.execute_reply": "2021-04-16T19:35:35.566963Z" }, "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:35.573478Z", "iopub.status.busy": "2021-04-16T19:35:35.572460Z", "iopub.status.idle": "2021-04-16T19:35:36.312737Z", "shell.execute_reply": "2021-04-16T19:35:36.313087Z" }, "tags": [] }, "outputs": [], "source": [ "from utils import set_pyplot_params\n", "set_pyplot_params()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This chapter introduces the [Poisson process](https://en.wikipedia.org/wiki/Poisson_point_process), which is a model used to describe events that occur at random intervals.\n", "As an example of a Poisson process, we'll model goal-scoring in soccer, which is American English for the game everyone else calls \"football\".\n", "We'll use goals scored in a game to estimate the parameter of a Poisson process; then we'll use the posterior distribution to make predictions.\n", "\n", "And we'll solve The World Cup Problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The World Cup Problem\n", "\n", "In the 2018 FIFA World Cup final, France defeated Croatia 4 goals to 2. Based on this outcome:\n", "\n", "1. How confident should we be that France is the better team?\n", "\n", "2. If the same teams played again, what is the chance France would win again?\n", "\n", "To answer these questions, we have to make some modeling decisions.\n", "\n", "* First, I'll assume that for any team against another team there is some unknown goal-scoring rate, measured in goals per game, which I'll denote with the Python variable `lam` or the Greek letter $\\lambda$, pronounced \"lambda\".\n", "\n", "* Second, I'll assume that a goal is equally likely during any minute of a game. So, in a 90 minute game, the probability of scoring during any minute is $\\lambda/90$.\n", "\n", "* Third, I'll assume that a team never scores twice during the same minute.\n", "\n", "Of course, none of these assumptions is completely true in the real world, but I think they are reasonable simplifications.\n", "As George Box said, \"All models are wrong; some are useful.\"\n", "(https://en.wikipedia.org/wiki/All_models_are_wrong).\n", "\n", "In this case, the model is useful because if these assumptions are \n", "true, at least roughly, the number of goals scored in a game follows a Poisson distribution, at least roughly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Poisson Distribution\n", "\n", "If the number of goals scored in a game follows a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution) with a goal-scoring rate, $\\lambda$, the probability of scoring $k$ goals is\n", "\n", "$$\\lambda^k \\exp(-\\lambda) ~/~ k!$$\n", "\n", "for any non-negative value of $k$.\n", "\n", "SciPy provides a `poisson` object that represents a Poisson distribution.\n", "We can create one with $\\lambda=1.4$ like this:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.320352Z", "iopub.status.busy": "2021-04-16T19:35:36.319569Z", "iopub.status.idle": "2021-04-16T19:35:36.323543Z", "shell.execute_reply": "2021-04-16T19:35:36.324079Z" } }, "outputs": [], "source": [ "from scipy.stats import poisson\n", "\n", "lam = 1.4\n", "dist = poisson(lam)\n", "type(dist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is an object that represents a \"frozen\" random variable and provides `pmf`, which evaluates the probability mass function of the Poisson distribution." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.329080Z", "iopub.status.busy": "2021-04-16T19:35:36.328350Z", "iopub.status.idle": "2021-04-16T19:35:36.331701Z", "shell.execute_reply": "2021-04-16T19:35:36.332267Z" } }, "outputs": [], "source": [ "k = 4\n", "dist.pmf(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This result implies that if the average goal-scoring rate is 1.4 goals per game, the probability of scoring 4 goals in a game is about 4%.\n", "\n", "We'll use the following function to make a `Pmf` that represents a Poisson distribution." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.338086Z", "iopub.status.busy": "2021-04-16T19:35:36.337399Z", "iopub.status.idle": "2021-04-16T19:35:36.339498Z", "shell.execute_reply": "2021-04-16T19:35:36.338953Z" } }, "outputs": [], "source": [ "from empiricaldist import Pmf\n", "\n", "def make_poisson_pmf(lam, qs):\n", " \"\"\"Make a Pmf of a Poisson distribution.\"\"\"\n", " ps = poisson(lam).pmf(qs)\n", " pmf = Pmf(ps, qs)\n", " pmf.normalize()\n", " return pmf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`make_poisson_pmf` takes as parameters the goal-scoring rate, `lam`, and an array of quantities, `qs`, where it should evaluate the Poisson PMF. It returns a `Pmf` object.\n", "\n", "For example, here's the distribution of goals scored for `lam=1.4`, computed for values of `k` from 0 to 9." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.346180Z", "iopub.status.busy": "2021-04-16T19:35:36.345152Z", "iopub.status.idle": "2021-04-16T19:35:36.351746Z", "shell.execute_reply": "2021-04-16T19:35:36.351317Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "lam = 1.4\n", "goals = np.arange(10)\n", "pmf_goals = make_poisson_pmf(lam, goals)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's what it looks like." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.357426Z", "iopub.status.busy": "2021-04-16T19:35:36.356638Z", "iopub.status.idle": "2021-04-16T19:35:36.360496Z", "shell.execute_reply": "2021-04-16T19:35:36.360974Z" }, "tags": [] }, "outputs": [], "source": [ "from utils import decorate\n", "\n", "def decorate_goals(title=''):\n", " decorate(xlabel='Number of goals',\n", " ylabel='PMF',\n", " title=title)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.365999Z", "iopub.status.busy": "2021-04-16T19:35:36.365195Z", "iopub.status.idle": "2021-04-16T19:35:36.806363Z", "shell.execute_reply": "2021-04-16T19:35:36.805860Z" }, "tags": [] }, "outputs": [], "source": [ "pmf_goals.bar(label=r'Poisson distribution with $\\lambda=1.4$')\n", "\n", "decorate_goals('Distribution of goals scored')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most likely outcomes are 0, 1, and 2; higher values are possible but increasingly unlikely.\n", "Values above 7 are negligible.\n", "This distribution shows that if we know the goal scoring rate, we can predict the number of goals.\n", "\n", "Now let's turn it around: given a number of goals, what can we say about the goal-scoring rate?\n", "\n", "To answer that, we need to think about the prior distribution of `lam`, which represents the range of possible values and their probabilities before we see the score." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Gamma Distribution\n", "\n", "If you have ever seen a soccer game, you have some information about `lam`. In most games, teams score a few goals each. In rare cases, a team might score more than 5 goals, but they almost never score more than 10.\n", "\n", "Using [data from previous World Cups](https://www.statista.com/statistics/269031/goals-scored-per-game-at-the-fifa-world-cup-since-1930/), I estimate that each team scores about 1.4 goals per game, on average. So I'll set the mean of `lam` to be 1.4.\n", "\n", "For a good team against a bad one, we expect `lam` to be higher; for a bad team against a good one, we expect it to be lower." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To model the distribution of goal-scoring rates, I'll use a [gamma distribution](https://en.wikipedia.org/wiki/Gamma_distribution), which I chose because:\n", "\n", "1. The goal scoring rate is continuous and non-negative, and the gamma distribution is appropriate for this kind of quantity.\n", "\n", "2. The gamma distribution has only one parameter, `alpha`, which is the mean. So it's easy to construct a gamma distribution with the mean we want.\n", "\n", "3. As we'll see, the shape of the gamma distribution is a reasonable choice, given what we know about soccer.\n", "\n", "And there's one more reason, which I will reveal in <<_ConjugatePriors>>.\n", "\n", "SciPy provides `gamma`, which creates an object that represents a gamma distribution.\n", "And the `gamma` object provides provides `pdf`, which evaluates the **probability density function** (PDF) of the gamma distribution.\n", "\n", "Here's how we use it." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.811180Z", "iopub.status.busy": "2021-04-16T19:35:36.810595Z", "iopub.status.idle": "2021-04-16T19:35:36.812206Z", "shell.execute_reply": "2021-04-16T19:35:36.812559Z" } }, "outputs": [], "source": [ "from scipy.stats import gamma\n", "\n", "alpha = 1.4\n", "qs = np.linspace(0, 10, 101)\n", "ps = gamma(alpha).pdf(qs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameter, `alpha`, is the mean of the distribution.\n", "The `qs` are possible values of `lam` between 0 and 10.\n", "The `ps` are **probability densities**, which we can think of as unnormalized probabilities.\n", "\n", "To normalize them, we can put them in a `Pmf` and call `normalize`:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.817622Z", "iopub.status.busy": "2021-04-16T19:35:36.816921Z", "iopub.status.idle": "2021-04-16T19:35:36.820318Z", "shell.execute_reply": "2021-04-16T19:35:36.819842Z" }, "tags": [] }, "outputs": [], "source": [ "from empiricaldist import Pmf\n", "\n", "prior = Pmf(ps, qs)\n", "prior.normalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is a discrete approximation of a gamma distribution.\n", "Here's what it looks like." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.824445Z", "iopub.status.busy": "2021-04-16T19:35:36.823630Z", "iopub.status.idle": "2021-04-16T19:35:36.826395Z", "shell.execute_reply": "2021-04-16T19:35:36.825832Z" }, "tags": [] }, "outputs": [], "source": [ "def decorate_rate(title=''):\n", " decorate(xlabel='Goal scoring rate (lam)',\n", " ylabel='PMF',\n", " title=title)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.861338Z", "iopub.status.busy": "2021-04-16T19:35:36.850188Z", "iopub.status.idle": "2021-04-16T19:35:36.973653Z", "shell.execute_reply": "2021-04-16T19:35:36.974165Z" }, "tags": [] }, "outputs": [], "source": [ "prior.plot(ls='--', label='prior', color='C5')\n", "decorate_rate(r'Prior distribution of $\\lambda$')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This distribution represents our prior knowledge about goal scoring: `lam` is usually less than 2, occasionally as high as 6, and seldom higher than that. " ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "And we can confirm that the mean is about 1.4." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.977679Z", "iopub.status.busy": "2021-04-16T19:35:36.977270Z", "iopub.status.idle": "2021-04-16T19:35:36.981785Z", "shell.execute_reply": "2021-04-16T19:35:36.981415Z" }, "tags": [] }, "outputs": [], "source": [ "prior.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As usual, reasonable people could disagree about the details of the prior, but this is good enough to get started. Let's do an update." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Update\n", "\n", "Suppose you are given the goal-scoring rate, $\\lambda$, and asked to compute the probability of scoring a number of goals, $k$. That is precisely the question we answered by computing the Poisson PMF.\n", "\n", "For example, if $\\lambda$ is 1.4, the probability of scoring 4 goals in a game is:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.986101Z", "iopub.status.busy": "2021-04-16T19:35:36.985682Z", "iopub.status.idle": "2021-04-16T19:35:36.990357Z", "shell.execute_reply": "2021-04-16T19:35:36.989881Z" } }, "outputs": [], "source": [ "lam = 1.4\n", "k = 4\n", "poisson(lam).pmf(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now suppose we are have an array of possible values for $\\lambda$; we can compute the likelihood of the data for each hypothetical value of `lam`, like this:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.994726Z", "iopub.status.busy": "2021-04-16T19:35:36.994093Z", "iopub.status.idle": "2021-04-16T19:35:36.996133Z", "shell.execute_reply": "2021-04-16T19:35:36.995693Z" } }, "outputs": [], "source": [ "lams = prior.qs\n", "k = 4\n", "likelihood = poisson(lams).pmf(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that's all we need to do the update.\n", "To get the posterior distribution, we multiply the prior by the likelihoods we just computed and normalize the result.\n", "\n", "The following function encapsulates these steps." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:36.999424Z", "iopub.status.busy": "2021-04-16T19:35:36.999004Z", "iopub.status.idle": "2021-04-16T19:35:37.000604Z", "shell.execute_reply": "2021-04-16T19:35:37.000963Z" } }, "outputs": [], "source": [ "def update_poisson(pmf, data):\n", " \"\"\"Update Pmf with a Poisson likelihood.\"\"\"\n", " k = data\n", " lams = pmf.qs\n", " likelihood = poisson(lams).pmf(k)\n", " pmf *= likelihood\n", " pmf.normalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first parameter is the prior; the second is the number of goals.\n", "\n", "In the example, France scored 4 goals, so I'll make a copy of the prior and update it with the data." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.006206Z", "iopub.status.busy": "2021-04-16T19:35:37.005520Z", "iopub.status.idle": "2021-04-16T19:35:37.007211Z", "shell.execute_reply": "2021-04-16T19:35:37.007583Z" } }, "outputs": [], "source": [ "france = prior.copy()\n", "update_poisson(france, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the posterior distribution looks like, along with the prior." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.028551Z", "iopub.status.busy": "2021-04-16T19:35:37.028101Z", "iopub.status.idle": "2021-04-16T19:35:37.185307Z", "shell.execute_reply": "2021-04-16T19:35:37.184888Z" }, "tags": [] }, "outputs": [], "source": [ "prior.plot(ls='--', label='prior', color='C5')\n", "france.plot(label='France posterior', color='C3')\n", "\n", "decorate_rate('Posterior distribution for France')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data, `k=4`, makes us think higher values of `lam` are more likely and lower values are less likely. So the posterior distribution is shifted to the right.\n", "\n", "Let's do the same for Croatia:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.190240Z", "iopub.status.busy": "2021-04-16T19:35:37.189712Z", "iopub.status.idle": "2021-04-16T19:35:37.191717Z", "shell.execute_reply": "2021-04-16T19:35:37.192161Z" } }, "outputs": [], "source": [ "croatia = prior.copy()\n", "update_poisson(croatia, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here are the results." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.231183Z", "iopub.status.busy": "2021-04-16T19:35:37.225303Z", "iopub.status.idle": "2021-04-16T19:35:37.342248Z", "shell.execute_reply": "2021-04-16T19:35:37.341893Z" }, "tags": [] }, "outputs": [], "source": [ "prior.plot(ls='--', label='prior', color='C5')\n", "croatia.plot(label='Croatia posterior', color='C0')\n", "\n", "decorate_rate('Posterior distribution for Croatia')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the posterior means for these distributions." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.345387Z", "iopub.status.busy": "2021-04-16T19:35:37.344949Z", "iopub.status.idle": "2021-04-16T19:35:37.348895Z", "shell.execute_reply": "2021-04-16T19:35:37.349408Z" } }, "outputs": [], "source": [ "print(croatia.mean(), france.mean())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The mean of the prior distribution is about 1.4.\n", "After Croatia scores 2 goals, their posterior mean is 1.7, which is near the midpoint of the prior and the data.\n", "Likewise after France scores 4 goals, their posterior mean is 2.7.\n", "\n", "These results are typical of a Bayesian update: the location of the posterior distribution is a compromise between the prior and the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Probability of Superiority\n", "\n", "Now that we have a posterior distribution for each team, we can answer the first question: How confident should we be that France is the better team?\n", "\n", "In the model, \"better\" means having a higher goal-scoring rate against the opponent. We can use the posterior distributions to compute the probability that a random value drawn from France's distribution exceeds a value drawn from Croatia's.\n", "\n", "One way to do that is to enumerate all pairs of values from the two distributions, adding up the total probability that one value exceeds the other." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.352893Z", "iopub.status.busy": "2021-04-16T19:35:37.352483Z", "iopub.status.idle": "2021-04-16T19:35:37.355738Z", "shell.execute_reply": "2021-04-16T19:35:37.355251Z" } }, "outputs": [], "source": [ "def prob_gt(pmf1, pmf2):\n", " \"\"\"Compute the probability of superiority.\"\"\"\n", " total = 0\n", " for q1, p1 in pmf1.items():\n", " for q2, p2 in pmf2.items():\n", " if q1 > q2:\n", " total += p1 * p2\n", " return total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is similar to the method we use in <<_Addends>> to compute the distribution of a sum.\n", "Here's how we use it:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.361765Z", "iopub.status.busy": "2021-04-16T19:35:37.361299Z", "iopub.status.idle": "2021-04-16T19:35:37.363760Z", "shell.execute_reply": "2021-04-16T19:35:37.364124Z" } }, "outputs": [], "source": [ "prob_gt(france, croatia)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Pmf` provides a function that does the same thing." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.367610Z", "iopub.status.busy": "2021-04-16T19:35:37.366920Z", "iopub.status.idle": "2021-04-16T19:35:37.370347Z", "shell.execute_reply": "2021-04-16T19:35:37.370715Z" } }, "outputs": [], "source": [ "Pmf.prob_gt(france, croatia)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The results are slightly different because `Pmf.prob_gt` uses array operators rather than `for` loops.\n", "\n", "Either way, the result is close to 75%. So, on the basis of one game, we have moderate confidence that France is actually the better team.\n", "\n", "Of course, we should remember that this result is based on the assumption that the goal-scoring rate is constant.\n", "In reality, if a team is down by one goal, they might play more aggressively toward the end of the game, making them more likely to score, but also more likely to give up an additional goal.\n", "\n", "As always, the results are only as good as the model." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Predicting the Rematch\n", "\n", "Now we can take on the second question: If the same teams played again, what is the chance Croatia would win?\n", "To answer this question, we'll generate the \"posterior predictive distribution\", which is the number of goals we expect a team to score.\n", "\n", "If we knew the goal scoring rate, `lam`, the distribution of goals would be a Poisson distribution with parameter `lam`.\n", "Since we don't know `lam`, the distribution of goals is a mixture of a Poisson distributions with different values of `lam`.\n", "\n", "First I'll generate a sequence of `Pmf` objects, one for each value of `lam`." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.411054Z", "iopub.status.busy": "2021-04-16T19:35:37.394398Z", "iopub.status.idle": "2021-04-16T19:35:37.494790Z", "shell.execute_reply": "2021-04-16T19:35:37.494245Z" } }, "outputs": [], "source": [ "pmf_seq = [make_poisson_pmf(lam, goals) \n", " for lam in prior.qs]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following figure shows what these distributions look like for a few values of `lam`." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:37.518985Z", "iopub.status.busy": "2021-04-16T19:35:37.518453Z", "iopub.status.idle": "2021-04-16T19:35:38.081641Z", "shell.execute_reply": "2021-04-16T19:35:38.082938Z" }, "tags": [] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "for i, index in enumerate([10, 20, 30, 40]):\n", " plt.subplot(2, 2, i+1)\n", " lam = prior.qs[index]\n", " pmf = pmf_seq[index]\n", " pmf.bar(label=f'$\\lambda$ = {lam}', color='C3')\n", " decorate_goals()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The predictive distribution is a mixture of these `Pmf` objects, weighted with the posterior probabilities.\n", "We can use `make_mixture` from <<_GeneralMixtures>> to compute this mixture." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.175633Z", "iopub.status.busy": "2021-04-16T19:35:38.174868Z", "iopub.status.idle": "2021-04-16T19:35:38.178570Z", "shell.execute_reply": "2021-04-16T19:35:38.179238Z" } }, "outputs": [], "source": [ "from utils import make_mixture\n", "\n", "pred_france = make_mixture(france, pmf_seq)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the predictive distribution for the number of goals France would score in a rematch." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.203125Z", "iopub.status.busy": "2021-04-16T19:35:38.202242Z", "iopub.status.idle": "2021-04-16T19:35:38.375595Z", "shell.execute_reply": "2021-04-16T19:35:38.375124Z" }, "tags": [] }, "outputs": [], "source": [ "pred_france.bar(color='C3', label='France')\n", "decorate_goals('Posterior predictive distribution')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This distribution represents two sources of uncertainty: we don't know the actual value of `lam`, and even if we did, we would not know the number of goals in the next game.\n", "\n", "Here's the predictive distribution for Croatia." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.388532Z", "iopub.status.busy": "2021-04-16T19:35:38.385236Z", "iopub.status.idle": "2021-04-16T19:35:38.392723Z", "shell.execute_reply": "2021-04-16T19:35:38.393065Z" } }, "outputs": [], "source": [ "pred_croatia = make_mixture(croatia, pmf_seq)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.412413Z", "iopub.status.busy": "2021-04-16T19:35:38.411968Z", "iopub.status.idle": "2021-04-16T19:35:38.571854Z", "shell.execute_reply": "2021-04-16T19:35:38.572272Z" }, "scrolled": true, "tags": [] }, "outputs": [], "source": [ "pred_croatia.bar(color='C0', label='Croatia')\n", "decorate_goals('Posterior predictive distribution')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use these distributions to compute the probability that France wins, loses, or ties the rematch." ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.575867Z", "iopub.status.busy": "2021-04-16T19:35:38.575226Z", "iopub.status.idle": "2021-04-16T19:35:38.578736Z", "shell.execute_reply": "2021-04-16T19:35:38.578262Z" } }, "outputs": [], "source": [ "win = Pmf.prob_gt(pred_france, pred_croatia)\n", "win" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.582943Z", "iopub.status.busy": "2021-04-16T19:35:38.582298Z", "iopub.status.idle": "2021-04-16T19:35:38.585664Z", "shell.execute_reply": "2021-04-16T19:35:38.585183Z" } }, "outputs": [], "source": [ "lose = Pmf.prob_lt(pred_france, pred_croatia)\n", "lose" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.590259Z", "iopub.status.busy": "2021-04-16T19:35:38.589444Z", "iopub.status.idle": "2021-04-16T19:35:38.593139Z", "shell.execute_reply": "2021-04-16T19:35:38.592536Z" } }, "outputs": [], "source": [ "tie = Pmf.prob_eq(pred_france, pred_croatia)\n", "tie" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assuming that France wins half of the ties, their chance of winning the rematch is about 65%." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.597954Z", "iopub.status.busy": "2021-04-16T19:35:38.597082Z", "iopub.status.idle": "2021-04-16T19:35:38.600558Z", "shell.execute_reply": "2021-04-16T19:35:38.601362Z" } }, "outputs": [], "source": [ "win + tie/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a bit lower than their probability of superiority, which is 75%. And that makes sense, because we are less certain about the outcome of a single game than we are about the goal-scoring rates.\n", "Even if France is the better team, they might lose the game." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Exponential Distribution\n", "\n", "As an exercise at the end of this notebook, you'll have a chance to work on the following variation on the World Cup Problem:\n", "\n", ">In the 2014 FIFA World Cup, Germany played Brazil in a semifinal match. Germany scored after 11 minutes and again at the 23 minute mark. At that point in the match, how many goals would you expect Germany to score after 90 minutes? What was the probability that they would score 5 more goals (as, in fact, they did)?\n", "\n", "In this version, notice that the data is not the number of goals in a fixed period of time, but the time between goals.\n", "\n", "To compute the likelihood of data like this, we can take advantage of the theory of Poisson processes again. If each team has a constant goal-scoring rate, we expect the time between goals to follow an [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).\n", "\n", "If the goal-scoring rate is $\\lambda$, the probability of seeing an interval between goals of $t$ is proportional to the PDF of the exponential distribution:\n", "\n", "$$\\lambda \\exp(-\\lambda t)$$\n", "\n", "Because $t$ is a continuous quantity, the value of this expression is not a probability; it is a probability density. However, it is proportional to the probability of the data, so we can use it as a likelihood in a Bayesian update.\n", "\n", "SciPy provides `expon`, which creates an object that represents an exponential distribution.\n", "However, it does not take `lam` as a parameter in the way you might expect, which makes it awkward to work with.\n", "Since the PDF of the exponential distribution is so easy to evaluate, I'll use my own function." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.606263Z", "iopub.status.busy": "2021-04-16T19:35:38.605541Z", "iopub.status.idle": "2021-04-16T19:35:38.607919Z", "shell.execute_reply": "2021-04-16T19:35:38.607420Z" } }, "outputs": [], "source": [ "def expo_pdf(t, lam):\n", " \"\"\"Compute the PDF of the exponential distribution.\"\"\"\n", " return lam * np.exp(-lam * t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see what the exponential distribution looks like, let's assume again that `lam` is 1.4; we can compute the distribution of $t$ like this:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.612959Z", "iopub.status.busy": "2021-04-16T19:35:38.612199Z", "iopub.status.idle": "2021-04-16T19:35:38.614877Z", "shell.execute_reply": "2021-04-16T19:35:38.614521Z" } }, "outputs": [], "source": [ "lam = 1.4\n", "qs = np.linspace(0, 4, 101)\n", "ps = expo_pdf(qs, lam)\n", "pmf_time = Pmf(ps, qs)\n", "pmf_time.normalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's what it looks like:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.618454Z", "iopub.status.busy": "2021-04-16T19:35:38.617962Z", "iopub.status.idle": "2021-04-16T19:35:38.619909Z", "shell.execute_reply": "2021-04-16T19:35:38.620390Z" }, "tags": [] }, "outputs": [], "source": [ "def decorate_time(title=''):\n", " decorate(xlabel='Time between goals (games)',\n", " ylabel='PMF',\n", " title=title)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.657510Z", "iopub.status.busy": "2021-04-16T19:35:38.643269Z", "iopub.status.idle": "2021-04-16T19:35:38.810844Z", "shell.execute_reply": "2021-04-16T19:35:38.811207Z" }, "tags": [] }, "outputs": [], "source": [ "pmf_time.plot(label='exponential with $\\lambda$ = 1.4')\n", "\n", "decorate_time('Distribution of time between goals')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is counterintuitive, but true, that the most likely time to score a goal is immediately. After that, the probability of each successive interval is a little lower.\n", "\n", "With a goal-scoring rate of 1.4, it is possible that a team will take more than one game to score a goal, but it is unlikely that they will take more than two games." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "This chapter introduces three new distributions, so it can be hard to keep them straight.\n", "Let's review:\n", "\n", "* If a system satisfies the assumptions of a Poisson model, the number of events in a period of time follows a Poisson distribution, which is a discrete distribution with integer quantities from 0 to infinity. In practice, we can usually ignore low-probability quantities above a finite limit.\n", "\n", "* Also under the Poisson model, the interval between events follows an exponential distribution, which is a continuous distribution with quantities from 0 to infinity. Because it is continuous, it is described by a probability density function (PDF) rather than a probability mass function (PMF). But when we use an exponential distribution to compute the likelihood of the data, we can treat densities as unnormalized probabilities.\n", "\n", "* The Poisson and exponential distributions are parameterized by an event rate, denoted $\\lambda$ or `lam`.\n", "\n", "* For the prior distribution of $\\lambda$, I used a gamma distribution, which is a continuous distribution with quantities from 0 to infinity, but I approximated it with a discrete, bounded PMF. The gamma distribution has one parameter, denoted $\\alpha$ or `alpha`, which is also its mean.\n", "\n", "I chose the gamma distribution because the shape is consistent with our background knowledge about goal-scoring rates.\n", "There are other distributions we could have used; however, we will see in <<_ConjugatePriors>> that the gamma distribution can be a particularly good choice.\n", "\n", "But we have a few things to do before we get there, starting with these exercises." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Let's finish the exercise we started:\n", "\n", ">In the 2014 FIFA World Cup, Germany played Brazil in a semifinal match. Germany scored after 11 minutes and again at the 23 minute mark. At that point in the match, how many goals would you expect Germany to score after 90 minutes? What was the probability that they would score 5 more goals (as, in fact, they did)?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the steps I recommend:\n", "\n", "1. Starting with the same gamma prior we used in the previous problem, compute the likelihood of scoring a goal after 11 minutes for each possible value of `lam`. Don't forget to convert all times into games rather than minutes.\n", "\n", "2. Compute the posterior distribution of `lam` for Germany after the first goal.\n", "\n", "3. Compute the likelihood of scoring another goal after 12 more minutes and do another update. Plot the prior, posterior after one goal, and posterior after two goals.\n", "\n", "4. Compute the posterior predictive distribution of goals Germany might score during the remaining time in the game, `90-23` minutes. Note: You will have to think about how to generate predicted goals for a fraction of a game.\n", "\n", "5. Compute the probability of scoring 5 or more goals during the remaining time." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.815055Z", "iopub.status.busy": "2021-04-16T19:35:38.814508Z", "iopub.status.idle": "2021-04-16T19:35:38.816805Z", "shell.execute_reply": "2021-04-16T19:35:38.816456Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.822023Z", "iopub.status.busy": "2021-04-16T19:35:38.821587Z", "iopub.status.idle": "2021-04-16T19:35:38.823315Z", "shell.execute_reply": "2021-04-16T19:35:38.823760Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.828321Z", "iopub.status.busy": "2021-04-16T19:35:38.827420Z", "iopub.status.idle": "2021-04-16T19:35:38.831037Z", "shell.execute_reply": "2021-04-16T19:35:38.830566Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:38.880115Z", "iopub.status.busy": "2021-04-16T19:35:38.873830Z", "iopub.status.idle": "2021-04-16T19:35:38.999279Z", "shell.execute_reply": "2021-04-16T19:35:38.998901Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.017480Z", "iopub.status.busy": "2021-04-16T19:35:39.012527Z", "iopub.status.idle": "2021-04-16T19:35:39.118263Z", "shell.execute_reply": "2021-04-16T19:35:39.117817Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.132602Z", "iopub.status.busy": "2021-04-16T19:35:39.130808Z", "iopub.status.idle": "2021-04-16T19:35:39.137447Z", "shell.execute_reply": "2021-04-16T19:35:39.137001Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.158798Z", "iopub.status.busy": "2021-04-16T19:35:39.158215Z", "iopub.status.idle": "2021-04-16T19:35:39.291504Z", "shell.execute_reply": "2021-04-16T19:35:39.291118Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.294944Z", "iopub.status.busy": "2021-04-16T19:35:39.294527Z", "iopub.status.idle": "2021-04-16T19:35:39.299061Z", "shell.execute_reply": "2021-04-16T19:35:39.298714Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.302588Z", "iopub.status.busy": "2021-04-16T19:35:39.302155Z", "iopub.status.idle": "2021-04-16T19:35:39.306746Z", "shell.execute_reply": "2021-04-16T19:35:39.306108Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Returning to the first version of the World Cup Problem. Suppose France and Croatia play a rematch. What is the probability that France scores first?" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Hint: Compute the posterior predictive distribution for the time until the first goal by making a mixture of exponential distributions. You can use the following function to make a PMF that approximates an exponential distribution." ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.310510Z", "iopub.status.busy": "2021-04-16T19:35:39.310077Z", "iopub.status.idle": "2021-04-16T19:35:39.311871Z", "shell.execute_reply": "2021-04-16T19:35:39.312246Z" }, "tags": [] }, "outputs": [], "source": [ "def make_expo_pmf(lam, high):\n", " \"\"\"Make a PMF of an exponential distribution.\n", " \n", " lam: event rate\n", " high: upper bound on the interval `t`\n", " \n", " returns: Pmf of the interval between events\n", " \"\"\"\n", " qs = np.linspace(0, high, 101)\n", " ps = expo_pdf(qs, lam)\n", " pmf = Pmf(ps, qs)\n", " pmf.normalize()\n", " return pmf" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.368616Z", "iopub.status.busy": "2021-04-16T19:35:39.368114Z", "iopub.status.idle": "2021-04-16T19:35:39.369779Z", "shell.execute_reply": "2021-04-16T19:35:39.370158Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.379507Z", "iopub.status.busy": "2021-04-16T19:35:39.379076Z", "iopub.status.idle": "2021-04-16T19:35:39.409912Z", "shell.execute_reply": "2021-04-16T19:35:39.409484Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.448811Z", "iopub.status.busy": "2021-04-16T19:35:39.440846Z", "iopub.status.idle": "2021-04-16T19:35:39.601265Z", "shell.execute_reply": "2021-04-16T19:35:39.601621Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.605608Z", "iopub.status.busy": "2021-04-16T19:35:39.604876Z", "iopub.status.idle": "2021-04-16T19:35:39.607907Z", "shell.execute_reply": "2021-04-16T19:35:39.607362Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** In the 2010-11 National Hockey League (NHL) Finals, my beloved Boston\n", "Bruins played a best-of-seven championship series against the despised\n", "Vancouver Canucks. Boston lost the first two games 0-1 and 2-3, then\n", "won the next two games 8-1 and 4-0. At this point in the series, what\n", "is the probability that Boston will win the next game, and what is\n", "their probability of winning the championship?\n", "\n", "To choose a prior distribution, I got some statistics from\n", "http://www.nhl.com, specifically the average goals per game\n", "for each team in the 2010-11 season. The distribution is well modeled by a gamma distribution with mean 2.8.\n", "\n", "In what ways do you think the outcome of these games might violate the assumptions of the Poisson model? How would these violations affect your predictions?" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.611311Z", "iopub.status.busy": "2021-04-16T19:35:39.610761Z", "iopub.status.idle": "2021-04-16T19:35:39.612613Z", "shell.execute_reply": "2021-04-16T19:35:39.613050Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.619564Z", "iopub.status.busy": "2021-04-16T19:35:39.618800Z", "iopub.status.idle": "2021-04-16T19:35:39.622375Z", "shell.execute_reply": "2021-04-16T19:35:39.621760Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.637525Z", "iopub.status.busy": "2021-04-16T19:35:39.634539Z", "iopub.status.idle": "2021-04-16T19:35:39.789592Z", "shell.execute_reply": "2021-04-16T19:35:39.789137Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.800657Z", "iopub.status.busy": "2021-04-16T19:35:39.800187Z", "iopub.status.idle": "2021-04-16T19:35:39.802525Z", "shell.execute_reply": "2021-04-16T19:35:39.802876Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.815048Z", "iopub.status.busy": "2021-04-16T19:35:39.814400Z", "iopub.status.idle": "2021-04-16T19:35:39.817231Z", "shell.execute_reply": "2021-04-16T19:35:39.817589Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:39.864860Z", "iopub.status.busy": "2021-04-16T19:35:39.845438Z", "iopub.status.idle": "2021-04-16T19:35:40.011253Z", "shell.execute_reply": "2021-04-16T19:35:40.010825Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.018735Z", "iopub.status.busy": "2021-04-16T19:35:40.016139Z", "iopub.status.idle": "2021-04-16T19:35:40.134862Z", "shell.execute_reply": "2021-04-16T19:35:40.134445Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.144335Z", "iopub.status.busy": "2021-04-16T19:35:40.143540Z", "iopub.status.idle": "2021-04-16T19:35:40.375993Z", "shell.execute_reply": "2021-04-16T19:35:40.375477Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.389727Z", "iopub.status.busy": "2021-04-16T19:35:40.389285Z", "iopub.status.idle": "2021-04-16T19:35:40.629676Z", "shell.execute_reply": "2021-04-16T19:35:40.629239Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.634426Z", "iopub.status.busy": "2021-04-16T19:35:40.633703Z", "iopub.status.idle": "2021-04-16T19:35:40.636896Z", "shell.execute_reply": "2021-04-16T19:35:40.636374Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.641477Z", "iopub.status.busy": "2021-04-16T19:35:40.640795Z", "iopub.status.idle": "2021-04-16T19:35:40.643879Z", "shell.execute_reply": "2021-04-16T19:35:40.644307Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:40.649670Z", "iopub.status.busy": "2021-04-16T19:35:40.648993Z", "iopub.status.idle": "2021-04-16T19:35:40.652576Z", "shell.execute_reply": "2021-04-16T19:35:40.652015Z" } }, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Tags", "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.8.12" } }, "nbformat": 4, "nbformat_minor": 4 }