{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dice & German Tank Problem" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy as sp\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import empiricaldist\n", "from empiricaldist import Pmf, Distribution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manual Way" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>probs</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>4</th>\n", " <td>0.25</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>0.25</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.25</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.25</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ "4 0.25\n", "6 0.25\n", "8 0.25\n", "12 0.25\n", "dtype: float64" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dice = Pmf.from_seq([4,6,8,12])\n", "dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.09374999999999999" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dice[4] *= 0\n", "dice[6] *= 1/6\n", "dice[8] *= 1/8 \n", "dice[12] *= 1/12\n", "\n", "dice.normalize()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>probs</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>4</th>\n", " <td>0.000000</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>0.444444</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.333333</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.222222</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ "4 0.000000\n", "6 0.444444\n", "8 0.333333\n", "12 0.222222\n", "dtype: float64" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def likelihood_dice(data, hypo):\n", " if data > hypo:\n", " return 0\n", " else:\n", " return 1 /hypo\n", " \n", " # Solution\n", " \n", "# def likelihood_dice(data, hypo):\n", "# \"\"\"Likelihood function for the dice problem.\n", " \n", "# data: outcome of the die roll\n", "# hypo: number of sides\n", " \n", "# returns: float probability\n", "# \"\"\"\n", "# if data > hypo:\n", "# return 0\n", "# else:\n", "# return 1 / hypo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>probs</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>4</th>\n", " <td>0.000000</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>0.444444</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.333333</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.222222</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ "4 0.000000\n", "6 0.444444\n", "8 0.333333\n", "12 0.222222\n", "dtype: float64" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dice = Pmf.from_seq([4,6,8,12])\n", "dice.update(likelihood_dice, 6)\n", "dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in [8,7,7,5, 4]:\n", " dice.update(likelihood_dice, roll)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>probs</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>4</th>\n", " <td>0.000000</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>0.000000</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.919294</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.080706</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ "4 0.000000\n", "6 0.000000\n", "8 0.919294\n", "12 0.080706\n", "dtype: float64" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dice.update??" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }