From 35bda9d3c9c350c57d8b07b72002e49a34e2526d Mon Sep 17 00:00:00 2001 From: bfernandezrivas Date: Sat, 21 Mar 2026 13:08:28 +0100 Subject: [PATCH] Update lab-intro-probability.ipynb --- lab-intro-probability.ipynb | 220 ++++++++++++++++++++++++++++++++---- 1 file changed, 195 insertions(+), 25 deletions(-) diff --git a/lab-intro-probability.ipynb b/lab-intro-probability.ipynb index 5893fc1..f7ca885 100644 --- a/lab-intro-probability.ipynb +++ b/lab-intro-probability.ipynb @@ -38,11 +38,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Estimated probability: 0.8835\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "import numpy as np\n", + "\n", + "# Parameters\n", + "n_simulations = 100000\n", + "tickets_sold = 460\n", + "show_prob = 0.97\n", + "seats = 450\n", + "\n", + "# Simulate number of passengers who show up\n", + "# Binomial: number of successes (people who show up)\n", + "simulated_showups = np.random.binomial(tickets_sold, show_prob, n_simulations)\n", + "\n", + "# Count how many simulations have <= 450 passengers\n", + "successful_cases = np.sum(simulated_showups <= seats)\n", + "\n", + "# Estimate probability\n", + "probability = successful_cases / n_simulations\n", + "\n", + "print(f\"Estimated probability: {probability:.4f}\")" ] }, { @@ -72,11 +100,36 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Estimated probability: 0.4904\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "import numpy as np\n", + "\n", + "# Parameters\n", + "p = 0.3\n", + "n_simulations = 100000\n", + "\n", + "# Simulate number of attempts until first success\n", + "attempts = np.random.geometric(p, n_simulations)\n", + "\n", + "# Count cases where attempts >= 3\n", + "cases = np.sum(attempts >= 3)\n", + "\n", + "# Estimate probability\n", + "probability = cases / n_simulations\n", + "\n", + "print(f\"Estimated probability: {probability:.4f}\")" ] }, { @@ -107,11 +160,29 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability of being overwhelmed in one hour: 0.012898\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "from scipy.stats import poisson\n", + "\n", + "# Average visits per hour\n", + "lam = 500\n", + "\n", + "# Server overwhelmed if visits > 550\n", + "prob_overwhelmed_hour = 1 - poisson.cdf(550, lam)\n", + "\n", + "print(f\"Probability of being overwhelmed in one hour: {prob_overwhelmed_hour:.6f}\")" ] }, { @@ -123,11 +194,34 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability of being overwhelmed in one hour: 0.012898\n", + "Probability of being overwhelmed at least once in 24 hours: 0.267704\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "from scipy.stats import poisson\n", + "\n", + "# Average visits per hour\n", + "lam = 500\n", + "\n", + "# Probability of overload in one hour\n", + "p_hour = 1 - poisson.cdf(550, lam)\n", + "\n", + "# Probability of overload at least once in 24 hours\n", + "p_day = 1 - (1 - p_hour) ** 24\n", + "\n", + "print(f\"Probability of being overwhelmed in one hour: {p_hour:.6f}\")\n", + "print(f\"Probability of being overwhelmed at least once in 24 hours: {p_day:.6f}\")" ] }, { @@ -157,10 +251,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability customer arrives within 5 minutes: 0.3935\n" + ] + } + ], + "source": [ + "#code here \n", + "\n", + "import numpy as np\n", + "\n", + "lam = 0.1 # rate per minute\n", + "t = 5\n", + "\n", + "prob = 1 - np.exp(-lam * t)\n", + "\n", + "print(f\"Probability customer arrives within 5 minutes: {prob:.4f}\")" + ] }, { "cell_type": "markdown", @@ -173,10 +286,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability of no customers for 15 minutes: 0.2231\n" + ] + } + ], + "source": [ + "#code here \n", + "\n", + "import numpy as np\n", + "\n", + "lam = 0.1 # rate per minute\n", + "t = 15\n", + "\n", + "prob = np.exp(-lam * t)\n", + "\n", + "print(f\"Probability of no customers for 15 minutes: {prob:.4f}\")" + ] }, { "cell_type": "markdown", @@ -196,11 +328,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability: 0.6827\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "from scipy.stats import norm\n", + "\n", + "mu = 150\n", + "sigma = 10\n", + "\n", + "prob = norm.cdf(160, mu, sigma) - norm.cdf(140, mu, sigma)\n", + "\n", + "print(f\"Probability: {prob:.4f}\")" ] }, { @@ -219,11 +368,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probability of failure within 30 hours: 0.4512\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "\n", + "import numpy as np\n", + "\n", + "# rate\n", + "lam = 1 / 50\n", + "\n", + "# time\n", + "t = 30\n", + "\n", + "# probability\n", + "prob = 1 - np.exp(-lam * t)\n", + "\n", + "print(f\"Probability of failure within 30 hours: {prob:.4f}\")" ] } ], @@ -243,7 +413,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.9.6" } }, "nbformat": 4,