{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hat problem\n", "Four students put their stats textbooks into a hat. The teacher shuffles the stats textbooks in the hat then hands the textbooks back to the students in a random order. What is the probability that:\n", " - None of the students got the right textbook\n", " - Exactly one of the students got the right textbook\n", " - Exactly two of the students got the right textbook\n", " - Exactly three (and thus four) of the students got the right textbook\n", " \n", "I don't think this counts as monte carlo because rather than generating random samples I generate all arrangements. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n", "init_notebook_mode(connected=False)\n", "\n", "def make_chart(probabilities, title):\n", " \"\"\"\n", " Generate a pie chart showing the probability distributio\n", " \"\"\"\n", " fig = {\n", " 'data': [{\n", " 'labels': list(probabilities.keys()),\n", " 'values': list(probabilities.values()),\n", " 'type': 'pie'\n", " }],\n", " 'layout': {\n", " 'title': title\n", " }\n", " }\n", " iplot(fig)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import itertools\n", "\n", "NUM_STUDENTS = 4\n", "\n", "def score_hand_back(handback):\n", " \"\"\"\n", " Examine the given handback and determine how many of the books\n", " went to the correct student\n", " \"\"\"\n", " return sum(1 for idx, val in enumerate(handback) if idx == val)\n", "\n", "assert(score_hand_back([0, 1, 2, 3]) == 4)\n", "assert(score_hand_back([0, 3, 1, 2]) == 1)\n", "\n", "def normalize(d):\n", " \"\"\"\n", " Normalize the values in the given dictionary such that\n", " the sum of all values is 1\n", " \"\"\"\n", " return {key: val / sum(d.values()) for (key, val) in d.items()}\n", "\n", "assert(normalize({0: 5, \"fish\": 15}) == {0: 1/4, \"fish\": 3/4})\n", "\n", "def simulate_hats():\n", " \"\"\"\n", " Simulate every possible handback. Count how many times each\n", " outcome occurs. Return a dictionary where the key is the number of\n", " students who got the correct book and the value at that key is the\n", " percentage of the time that happens.\n", " \"\"\"\n", " occurances = {0: 0, 1: 0, 2: 0, 4: 0}\n", " possible_handbacks = itertools.permutations(range(NUM_STUDENTS))\n", " for handback in possible_handbacks:\n", " occurances[score_hand_back(handback)] += 1\n", " return normalize(occurances)\n", "\n", "make_chart(simulate_hats(), \"Probability that n students get correct textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Red wine smoothie problem\n", "\n", "Red Robin has a new red wine smoothie. Each smoothie comes with one of four lucky charms (the charm you receive with the smoothie is random). When you have collected all four lucky charms, you get a free smoothie. How many smoothies do you have to order on average to get all four smoothies?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import random\n", "from collections import defaultdict\n", "\n", "NUM_CHARMS = 4\n", "\n", "def run_trial():\n", " \"\"\"\n", " Simulate giving someone smoothies until they have all four charms.\n", " Return the number of smoothies they needed to get all four.\n", " \"\"\"\n", " all_smoothies = set(range(NUM_CHARMS))\n", " num_tries = 0\n", " received_smoothies = set()\n", " while received_smoothies != all_smoothies:\n", " received_smoothies.add(random.choice(range(NUM_CHARMS)))\n", " num_tries += 1\n", " return num_tries\n", "\n", "def avg(lst):\n", " \"\"\"\n", " return the average of all the numbers in a list\n", " \"\"\"\n", " return sum(lst) / len(lst)\n", "\n", "def simulate_smoothies(iterations=10000):\n", " \"\"\"\n", " run |iterations| trials and return the average\n", " \"\"\"\n", " ret = defaultdict(lambda: 0)\n", " for _ in range(iterations):\n", " ret[run_trial()] += 1\n", " return ret\n", "\n", "make_chart(simulate_smoothies(),\n", " \"Probability that customer will have to order n smoothies before collecting all four\")" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }