Skip to content

Instantly share code, notes, and snippets.

@Ziul
Created June 29, 2022 16:58
Show Gist options
  • Save Ziul/3d9009cd89ab331472250bc04f04d5e5 to your computer and use it in GitHub Desktop.
Save Ziul/3d9009cd89ab331472250bc04f04d5e5 to your computer and use it in GitHub Desktop.
MotyHall.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "MotyHall.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyP2/8afzWAYzQKv2IBT4mg9",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Ziul/3d9009cd89ab331472250bc04f04d5e5/motyhall.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Honty Hall Example\n",
"\n",
"Let's create a example to check if the Monty Hall Paradox really works"
],
"metadata": {
"id": "ryou3nXAU8rY"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "7nJ_tmPkU4eW"
},
"outputs": [],
"source": [
"from random import choice\n",
"from string import ascii_lowercase as _keys_"
]
},
{
"cell_type": "code",
"source": [
"def monty_hall(doors_left:int = 2, total:int = int(1e5), start_doors:int = len(_keys_)) -> float:\n",
" \"\"\"\n",
" Repeat the Monty Hall example several times to check probabilities\n",
"\n",
" Args:\n",
" total (int): How many times to repeat the experiment. Default: int(1e5)\n",
" doors_left (int): Total of doors left in the end. Default: 2.\n",
" start_doors (int): Initial amount of doors. Max: 26. Default: 26\n",
" Returns:\n",
" rate (float): Success rate\n",
" \"\"\"\n",
" # all available doors\n",
" keys = _keys_[:start_doors]\n",
"\n",
" hits = 0 # how many times the player wons\n",
"\n",
" for _ in range(total):\n",
" # set all ports\n",
" ports = dict.fromkeys(keys, False)\n",
" # set the correct answer\n",
" answer = choice(keys)\n",
" ports[answer] = True\n",
" # player choose the first door\n",
" user_choice = choice(keys)\n",
"\n",
" while len(ports.keys()) > doors_left:\n",
" # remove a port not winner\n",
" del ports[choice(list(ports.keys() - answer))]\n",
" # player choose another random door different from the actual\n",
" user_choice = choice(list(ports.keys() - user_choice))\n",
" if answer == user_choice:\n",
" # player wons\n",
" hits +=1\n",
" # returns success rate\n",
" return hits/total"
],
"metadata": {
"id": "Ynh3vY1uVPrx"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"monty_hall(doors_left=2, start_doors =3)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Wy6o-BAxVXu2",
"outputId": "bcbe09d8-4cbc-4ea3-b226-504e0bfbc595"
},
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.49996"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"monty_hall(doors_left=2, start_doors = 10)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1VuyZ6QEVgfH",
"outputId": "441a10f7-3198-40af-ba76-245247f36dbf"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.50025"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"monty_hall(doors_left=3)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tczXPpyTVm2H",
"outputId": "620f9126-f341-4218-9ff5-9d71913c5ba6"
},
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.3377"
]
},
"metadata": {},
"execution_count": 5
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment