diff --git a/lab_intro_probability.ipynb b/lab_intro_probability.ipynb new file mode 100644 index 0000000..9c0e090 --- /dev/null +++ b/lab_intro_probability.ipynb @@ -0,0 +1,576 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iNEEJPxB-7Dt" + }, + "source": [ + "# Lab | Intro to Probability" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UuO5PJzh-7Dw" + }, + "source": [ + "**Objective**\n", + "\n", + "Welcome to this Intro to Probability lab, where we explore decision-making scenarios through the lens of probability and strategic analysis. In the business world, making informed decisions is crucial, especially when faced with uncertainties. This lab focuses on scenarios where probabilistic outcomes play a significant role in shaping strategies and outcomes. Students will engage in exercises that require assessing and choosing optimal paths based on data-driven insights. The goal is to enhance your skills by applying probability concepts to solve real-world problems." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d3dLY8Xk-7Dx" + }, + "source": [ + "**Challenge 1**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "K7mnzpd3-7Dx" + }, + "source": [ + "#### Ironhack Airlines\n", + "\n", + "Often Airlines sell more tickets than they have seats available, this is called overbooking. Consider the following:\n", + "- A plane has 450 seats.\n", + "- Based on historical data we conclude that each individual passenger has a 3% chance of missing it's flight.\n", + "\n", + "If the Ironhack Airlines routinely sells 460 tickets, what is the chance that they have a seats for all passenger?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "fQbW7MIt-7Dx", + "outputId": "0d661dfb-857e-4440-c4a4-3e4770d0f3da" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability that all passengers get a seat: 0.1155\n", + "That's approximately 11.55%\n" + ] + } + ], + "source": [ + "from scipy.stats import binom\n", + "\n", + "# Parameters\n", + "seats = 450\n", + "tickets = 460\n", + "show_up_prob = 0.97\n", + "\n", + "# Calculate probability of more than 450 passengers showing up\n", + "more_than_seats_probability = binom.cdf(seats, tickets, show_up_prob)\n", + "\n", + "# Subtract from 1 to find the probability for at most 450 passengers showing up\n", + "probability = 1 - more_than_seats_probability\n", + "\n", + "print(f\"Probability that all passengers get a seat: {probability:.4f}\")\n", + "print(f\"That's approximately {probability * 100:.2f}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mEewL_zo-7Dy" + }, + "source": [ + "**Challenge 2**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ER5wV6Hq-7Dy" + }, + "source": [ + "#### Ironhack Call Center" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jLU9V0zH-7Dz" + }, + "source": [ + "Suppose a customer service representative at a call center is handling customer complaints. Consider the following:\n", + "- The probability of successfully resolving a customer complaint on the first attempt is 0.3.\n", + "\n", + "\n", + "What is the probability that the representative needs to make at least three attempts before successfully resolving a customer complaint?" + ] + }, + { + "cell_type": "markdown", + "source": [ + "Attempting 2 different ways. The first is Geometric Distribution PMF - it directly calculates the probability of the first success on a specific attempt - \"waiting for the first success\".\n" + ], + "metadata": { + "id": "77RPNz7XH0ms" + } + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HlzL4j6S-7Dz", + "outputId": "ac208b50-b9a3-4356-8168-d47005fe7a3b" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of needing at least three attempts: 0.4900\n", + "That's approximately 49.00%\n" + ] + } + ], + "source": [ + "from scipy.stats import geom\n", + "\n", + "# Probability of success\n", + "p = 0.3\n", + "\n", + "# At least 3 attempts before success implies failing the first 2.\n", + "# Using complementary probability for P(X < 3)\n", + "p_at_least_3 = 1 - (geom.pmf(1, p) + geom.pmf(2, p))\n", + "\n", + "print(f\"Probability of needing at least three attempts: {p_at_least_3:.4f}\")\n", + "print(f\"That's approximately {p_at_least_3 * 100:.2f}%\")" + ] + }, + { + "cell_type": "markdown", + "source": [ + "This is 2nd Geometric Distribution CDF: Sums probabilities up to a certain attempt, making it useful for calculating the probability of the first success occurring before or at a certain attempt, and it's easily adapted to find the probability of needing more attempts by using its complement." + ], + "metadata": { + "id": "gkMGunoWIohm" + } + }, + { + "cell_type": "code", + "source": [ + "from scipy.stats import geom\n", + "\n", + "# Probability of success on a single attempt\n", + "p = 0.3\n", + "\n", + "# Use CDF to find the probability that the first success occurs on or before the 2nd attempt\n", + "p_less_than_3 = geom.cdf(2, p)\n", + "\n", + "# The complement gives the probability that the first success is on or after the 3rd attempt\n", + "p_at_least_3 = 1 - p_less_than_3\n", + "\n", + "print(f\"Probability of needing at least three attempts: {p_at_least_3:.4f}\")\n", + "print(f\"That's approximately {p_at_least_3 * 100:.2f}%\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-SF2muDkHjhC", + "outputId": "b9f7a9b5-b827-43d3-9f30-bb548241cbe8" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of needing at least three attempts: 0.4900\n", + "That's approximately 49.00%\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xiKZ6fzI-7Dz" + }, + "source": [ + "**Challenge 3**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i6hwGTLC-7D0" + }, + "source": [ + "#### Ironhack Website" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j71LGTFi-7D0" + }, + "source": [ + "Consider a scenario related to Ironhack website traffic. Where:\n", + "- our website takes on average 500 visits per hour.\n", + "- the website's server is designed to handle up to 550 vists per hour.\n", + "\n", + "\n", + "What is the probability of the website server being overwhelmed?" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ou2hREnk-7D0", + "outputId": "d5dfc786-3532-4d02-d078-ebeef3e810da" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of server being overwhelmed: 0.0129\n" + ] + } + ], + "source": [ + "from scipy.stats import poisson\n", + "\n", + "# Average rate of visits per hour\n", + "lambda_visits = 500\n", + "\n", + "# Maximum visits the server can handle\n", + "max_visits = 550\n", + "\n", + "# CDF gives the probability of up to 550 visits\n", + "prob_up_to_550 = poisson.cdf(max_visits, lambda_visits)\n", + "\n", + "# Complement gives the probability of more than 550 visits\n", + "prob_overwhelmed = 1 - prob_up_to_550\n", + "\n", + "print(f\"Probability of server being overwhelmed: {prob_overwhelmed:.4f}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F2K1vrL6-7D0" + }, + "source": [ + "What is the probability of being overwhelmed at some point during a day? (consider 24hours)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3Ac2g_tV-7D0", + "outputId": "fff2d212-ba61-462d-e08b-5615d9a51174" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of being overwhelmed at least once in a day: 0.2677\n" + ] + } + ], + "source": [ + "from scipy.stats import poisson\n", + "\n", + "lambda_visits = 500\n", + "max_visits = 550\n", + "\n", + "# Probability of not being overwhelmed in a single hour\n", + "prob_not_overwhelmed_single_hour = poisson.cdf(max_visits, lambda_visits)\n", + "\n", + "# Probability of not being overwhelmed during any of the 24 hours\n", + "prob_not_overwhelmed_full_day = prob_not_overwhelmed_single_hour ** 24\n", + "\n", + "# Probability of being overwhelmed at least once in a day\n", + "prob_overwhelmed_at_least_once = 1 - prob_not_overwhelmed_full_day\n", + "\n", + "print(f\"Probability of being overwhelmed at least once in a day: {prob_overwhelmed_at_least_once:.4f}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JVlks03V-7D0" + }, + "source": [ + "**Challenge 4**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bDFH6U-f-7D0" + }, + "source": [ + "#### Ironhack Helpdesk" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LT7WN9rh-7D0" + }, + "source": [ + "Consider a scenario related to the time between arrivals of customers at a service desk.\n", + "\n", + "On average, a customers arrives every 10minutes.\n", + "\n", + "What is the probability that the next customer will arrive within the next 5 minutes?" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "gv-D3YKP-7D0", + "outputId": "21abeddd-c4d3-4d48-a7e4-77e8d8c1273d" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability that the next customer will arrive within the next 5 minutes: 0.3935\n" + ] + } + ], + "source": [ + "from scipy.stats import expon\n", + "\n", + "# The average rate of arrivals (customers per minute)\n", + "lambda_rate = 1 / 10 # 0.1 per minute\n", + "\n", + "# Calculate the probability of arrival within the next 5 minutes\n", + "prob_within_5_minutes = expon.cdf(5, scale=1/lambda_rate)\n", + "\n", + "print(f\"Probability that the next customer will arrive within the next 5 minutes: {prob_within_5_minutes:.4f}\")" + ] + }, + { + "cell_type": "markdown", + "source": [ + "my note: Inverse Relationship: Since λ represents how many times an event happens per unit of time, its inverse (scale) gives the average unit of time per event." + ], + "metadata": { + "id": "V_Ec6QmESPlb" + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VPyp_Xqs-7D1" + }, + "source": [ + "If there is no customer for 15minutes, employees can that a 5minutes break.\n", + "\n", + "What is the probability an employee taking a break?" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vQTG94M9-7D1", + "outputId": "c6dc5f9f-ebaa-45ee-de4b-02e46950b7f7" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of an employee being able to take a break: 0.2231\n" + ] + } + ], + "source": [ + "# Average arrival rate per minute\n", + "lambda_rate = 0.1\n", + "\n", + "# Calculate the probability of no customer arriving within 15 minutes\n", + "prob_no_arrival_15_min = 1 - expon.cdf(15, scale=1/lambda_rate)\n", + "\n", + "print(f\"Probability of an employee being able to take a break: {prob_no_arrival_15_min:.4f}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zcnq6xrE-7D1" + }, + "source": [ + "**Challenge 5**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "trgs1sVe-7D1" + }, + "source": [ + "The weights of a certain species of birds follow a normal distribution with a mean weight of 150 grams and a standard deviation of 10 grams.\n", + "\n", + "- If we randomly select a bird, what is the probability that its weight is between 140 and 160 grams?" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "obH5MYcL-7D1", + "outputId": "257be46e-3be1-48e1-82f8-1ecf38762760" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability that a bird weighs between 140 and 160 grams: 0.6827\n" + ] + } + ], + "source": [ + "from scipy.stats import norm\n", + "\n", + "# Define the parameters\n", + "mu = 150 # mean\n", + "sigma = 10 # standard deviation\n", + "\n", + "# Calculate the probability of a weight less than 160 grams\n", + "prob_less_than_160 = norm.cdf(160, mu, sigma)\n", + "\n", + "# Calculate the probability of a weight less than 140 grams\n", + "prob_less_than_140 = norm.cdf(140, mu, sigma)\n", + "\n", + "# The probability of a weight between 140 and 160 grams\n", + "prob_between_140_and_160 = prob_less_than_160 - prob_less_than_140\n", + "\n", + "print(f\"Probability that a bird weighs between 140 and 160 grams: {prob_between_140_and_160:.4f}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hAWNv5CX-7D1" + }, + "source": [ + "**Challenge 6**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nPDef-FD-7D1" + }, + "source": [ + "If the lifetime (in hours) of a certain electronic component follows an exponential distribution with a mean lifetime of 50 hours, what is the probability that the component fails within the first 30 hours?" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GwwXacRm-7D1", + "outputId": "67d8fc29-6212-48a2-e8e7-0ea381d50af8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability that the component fails within 30 hours: 0.4512\n" + ] + } + ], + "source": [ + "from scipy.stats import expon\n", + "\n", + "# Mean lifetime\n", + "mean_lifetime = 50\n", + "\n", + "# Rate λ\n", + "lambda_rate = 1 / mean_lifetime\n", + "\n", + "# Probability of failure within the first 30 hours\n", + "prob_failure_within_30_hours = expon.cdf(30, scale=mean_lifetime)\n", + "\n", + "print(f\"Probability that the component fails within 30 hours: {prob_failure_within_30_hours:.4f}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.10.9" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file