diff --git a/examples/goblin.ipynb b/examples/goblin.ipynb new file mode 100644 index 000000000..b1a9bf257 --- /dev/null +++ b/examples/goblin.ipynb @@ -0,0 +1,204 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The goblin problem\n", + "\n", + "Allen Downey\n", + "\n", + "[MIT License](https://en.wikipedia.org/wiki/MIT_License)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "sns.set(style='white')\n", + "\n", + "from thinkstats2 import Pmf, Cdf\n", + "\n", + "import thinkstats2\n", + "import thinkplot\n", + "\n", + "decorate = thinkplot.config" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The goblin problem\n", + "\n", + "In Dungeons and Dragons, the amount of damage a goblin can withstand is the sum of two six-sided dice. The amount of damage you inflict with a short sword is determined by rolling one six-sided die.\n", + "\n", + "Suppose you are fighting a goblin and you have already inflicted 4 points of damage. What is your probability of defeating the goblin with your next successful attack?\n", + "\n", + "(Where \"defeat\" means that the goblin can withstand no more damage.)\n", + "\n", + "Note: You can solve this problem with paper and pencil or computationally; you can solve it using Bayes's Theorem, combinatorics, or using the Pmf class." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 0.16666666666666666\n", + "2 0.16666666666666666\n", + "3 0.16666666666666666\n", + "4 0.16666666666666666\n", + "5 0.16666666666666666\n", + "6 0.16666666666666666\n" + ] + } + ], + "source": [ + "d6 = Pmf(range(1,7))\n", + "d6.Print()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 0.027777777777777776\n", + "3 0.05555555555555555\n", + "4 0.08333333333333333\n", + "5 0.1111111111111111\n", + "6 0.1388888888888889\n", + "7 0.16666666666666669\n", + "8 0.1388888888888889\n", + "9 0.1111111111111111\n", + "10 0.08333333333333333\n", + "11 0.05555555555555555\n", + "12 0.027777777777777776\n" + ] + } + ], + "source": [ + "twice = d6 + d6\n", + "twice.Print()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 0.0\n", + "3 0.0\n", + "4 0.0\n", + "5 0.1333333333333333\n", + "6 0.16666666666666663\n", + "7 0.19999999999999998\n", + "8 0.16666666666666663\n", + "9 0.1333333333333333\n", + "10 0.09999999999999998\n", + "11 0.06666666666666665\n", + "12 0.033333333333333326\n" + ] + } + ], + "source": [ + "twice[2] = 0\n", + "twice[3] = 0\n", + "twice[4] = 0\n", + "twice.Normalize()\n", + "twice.Print()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2 0.0\n", + "-1 0.0\n", + "0 0.0\n", + "1 0.1333333333333333\n", + "2 0.16666666666666663\n", + "3 0.19999999999999998\n", + "4 0.16666666666666663\n", + "5 0.1333333333333333\n", + "6 0.09999999999999998\n", + "7 0.06666666666666665\n", + "8 0.033333333333333326\n" + ] + } + ], + "source": [ + "remaining = twice - 4\n", + "remaining.Print()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5499999999999998" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d6.ProbGreater(remaining) + d6.ProbEqual(remaining)" + ] + } + ], + "metadata": { + "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.6.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}