From bb8feeb99df30457e9e03d8df8a6638190b40f78 Mon Sep 17 00:00:00 2001
From: Ulrich <ulrich.kerzel@rwth-aachen.de>
Date: Tue, 18 Mar 2025 12:15:01 +0100
Subject: [PATCH] add notebook for Monty Hall problem

---
 datascienceintro/MontyHall.ipynb              | 116 +++++++++++++++++
 .../solutions/Solution_MontyHall.ipynb        | 122 ++++++++++++++++++
 2 files changed, 238 insertions(+)
 create mode 100644 datascienceintro/MontyHall.ipynb
 create mode 100644 datascienceintro/solutions/Solution_MontyHall.ipynb

diff --git a/datascienceintro/MontyHall.ipynb b/datascienceintro/MontyHall.ipynb
new file mode 100644
index 0000000..b8d1d6a
--- /dev/null
+++ b/datascienceintro/MontyHall.ipynb
@@ -0,0 +1,116 @@
+{
+  "cells": [
+    {
+      "cell_type": "markdown",
+      "metadata": {
+        "id": "aQVYzt-A7so-"
+      },
+      "source": [
+        "# Monty Hall\n",
+        "\n",
+        "In this puzzle, a contestant in a game-show gets to choose between 3 doors.\n",
+        "Behind one of the doors, a prize (the \"car\") is hidden, the other two doors do not win (the \"goat\").\n",
+        "\n",
+        "The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors.\n",
+        "\n",
+        "Is it beneficial to switch the doors?"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": 1,
+      "metadata": {
+        "id": "KuoDvxxe8F3M"
+      },
+      "outputs": [],
+      "source": [
+        "import random"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "yH85YcAI-0eY"
+      },
+      "outputs": [],
+      "source": [
+        "\n",
+        "def monty_hall_simulation(num_trials=5000):\n",
+        "    stay_wins = 0\n",
+        "    switch_wins = 0\n",
+        "\n",
+        "    for _ in range(num_trials):\n",
+        "        # Randomly place the car behind one of the three doors\n",
+        "        car_door = # YOUR CODE HERE\n",
+        "        # Contestant makes an initial choice randomly\n",
+        "        contestant_choice = # YOUR CODE HERE\n",
+        "\n",
+        "        # Host opens a door\n",
+        "        possible_doors = [1, 2, 3]\n",
+        "        # Host cannot open the door with the car or the contestant's choice\n",
+        "        doors_host_can_open = [# YOUR CODE HERE]\n",
+        "        host_opened_door = # YOUR CODE HERE\n",
+        "\n",
+        "        # Determine the other door that the contestant can switch to\n",
+        "        remaining_door = [# YOUR CODE HERE][0]\n",
+        "\n",
+        "        # If the contestant stays with their initial choice\n",
+        "        if contestant_choice == car_door:\n",
+        "            stay_wins += 1\n",
+        "        # If the contestant switches to the remaining door\n",
+        "        elif remaining_door == car_door:\n",
+        "            switch_wins += 1\n",
+        "\n",
+        "    stay_win_rate = stay_wins / num_trials\n",
+        "    switch_win_rate = switch_wins / num_trials\n",
+        "\n",
+        "    return stay_win_rate, switch_win_rate"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": 12,
+      "metadata": {
+        "colab": {
+          "base_uri": "https://localhost:8080/"
+        },
+        "id": "YOPqK0rd8Q5r",
+        "outputId": "cb585501-1540-46d3-a6e1-e504021f2015"
+      },
+      "outputs": [
+        {
+          "name": "stdout",
+          "output_type": "stream",
+          "text": [
+            "After 5000 trials:\n",
+            "Winning by staying with the initial choice: \t 32.60%\n",
+            "Winning by switching to the other door: \t 67.40%\n"
+          ]
+        }
+      ],
+      "source": [
+        "num_trials = 5000\n",
+        "stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials)\n",
+        "\n",
+        "print(f\"After {num_trials} trials:\")\n",
+        "print(f\"Winning by staying with the initial choice: \\t {stay_win_rate*100:.2f}%\")\n",
+        "print(f\"Winning by switching to the other door: \\t {switch_win_rate*100:.2f}%\")"
+      ]
+    }
+  ],
+  "metadata": {
+    "colab": {
+      "provenance": []
+    },
+    "kernelspec": {
+      "display_name": "Python 3",
+      "name": "python3"
+    },
+    "language_info": {
+      "name": "python"
+    }
+  },
+  "nbformat": 4,
+  "nbformat_minor": 0
+}
diff --git a/datascienceintro/solutions/Solution_MontyHall.ipynb b/datascienceintro/solutions/Solution_MontyHall.ipynb
new file mode 100644
index 0000000..17ff77f
--- /dev/null
+++ b/datascienceintro/solutions/Solution_MontyHall.ipynb
@@ -0,0 +1,122 @@
+{
+  "nbformat": 4,
+  "nbformat_minor": 0,
+  "metadata": {
+    "colab": {
+      "provenance": []
+    },
+    "kernelspec": {
+      "name": "python3",
+      "display_name": "Python 3"
+    },
+    "language_info": {
+      "name": "python"
+    }
+  },
+  "cells": [
+    {
+      "cell_type": "markdown",
+      "source": [
+        "# Monty Hall\n",
+        "\n",
+        "In this puzzle, a contestant in a game-show gets to choose between 3 doors.\n",
+        "Behind one of the doors, a prize (the \"car\") is hidden, the other two doors do not win (the \"goat\").\n",
+        "\n",
+        "The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors.\n",
+        "\n",
+        "Is it beneficial to switch the doors?"
+      ],
+      "metadata": {
+        "id": "aQVYzt-A7so-"
+      }
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "import random"
+      ],
+      "metadata": {
+        "id": "KuoDvxxe8F3M"
+      },
+      "execution_count": 1,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "execution_count": 11,
+      "metadata": {
+        "id": "yH85YcAI-0eY"
+      },
+      "outputs": [],
+      "source": [
+        "\n",
+        "def monty_hall_simulation(num_trials=5000):\n",
+        "    stay_wins = 0\n",
+        "    switch_wins = 0\n",
+        "\n",
+        "    for _ in range(num_trials):\n",
+        "        # Randomly place the car behind one of the three doors\n",
+        "        car_door = random.randint(1, 3)\n",
+        "        # Contestant makes an initial choice randomly\n",
+        "        contestant_choice = random.randint(1, 3)\n",
+        "\n",
+        "        # Host opens a door\n",
+        "        possible_doors = [1, 2, 3]\n",
+        "        # Host cannot open the door with the car or the contestant's choice\n",
+        "        doors_host_can_open = [door for door in possible_doors\n",
+        "                               if\n",
+        "                               door != car_door and\n",
+        "                               door != contestant_choice]\n",
+        "        host_opened_door = random.choice(doors_host_can_open)\n",
+        "\n",
+        "        # Determine the other door that the contestant can switch to\n",
+        "        remaining_door = [door for door in possible_doors\n",
+        "                          if\n",
+        "                          door != contestant_choice and\n",
+        "                          door != host_opened_door][0]\n",
+        "\n",
+        "        # If the contestant stays with their initial choice\n",
+        "        if contestant_choice == car_door:\n",
+        "            stay_wins += 1\n",
+        "        # If the contestant switches to the remaining door\n",
+        "        elif remaining_door == car_door:\n",
+        "            switch_wins += 1\n",
+        "\n",
+        "    stay_win_rate = stay_wins / num_trials\n",
+        "    switch_win_rate = switch_wins / num_trials\n",
+        "\n",
+        "    return stay_win_rate, switch_win_rate"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "num_trials = 5000\n",
+        "stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials)\n",
+        "\n",
+        "print(f\"After {num_trials} trials:\")\n",
+        "print(f\"Winning by staying with the initial choice: \\t {stay_win_rate*100:.2f}%\")\n",
+        "print(f\"Winning by switching to the other door: \\t {switch_win_rate*100:.2f}%\")"
+      ],
+      "metadata": {
+        "colab": {
+          "base_uri": "https://localhost:8080/"
+        },
+        "id": "YOPqK0rd8Q5r",
+        "outputId": "cb585501-1540-46d3-a6e1-e504021f2015"
+      },
+      "execution_count": 12,
+      "outputs": [
+        {
+          "output_type": "stream",
+          "name": "stdout",
+          "text": [
+            "After 5000 trials:\n",
+            "Winning by staying with the initial choice: \t 32.60%\n",
+            "Winning by switching to the other door: \t 67.40%\n"
+          ]
+        }
+      ]
+    }
+  ]
+}
\ No newline at end of file
-- 
GitLab