diff --git a/.ipynb_checkpoints/lab-intro-probability-checkpoint.ipynb b/.ipynb_checkpoints/lab-intro-probability-checkpoint.ipynb new file mode 100644 index 0000000..6f40565 --- /dev/null +++ b/.ipynb_checkpoints/lab-intro-probability-checkpoint.ipynb @@ -0,0 +1,425 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Intro to Probability" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": {}, + "source": [ + "**Challenge 1**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: scipy in c:\\users\\barba\\anaconda3\\lib\\site-packages (1.13.1)\n", + "Requirement already satisfied: numpy<2.3,>=1.22.4 in c:\\users\\barba\\anaconda3\\lib\\site-packages (from scipy) (1.26.4)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install scipy" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that all passengers have seats is: 0.8845\n" + ] + } + ], + "source": [ + "from scipy.stats import binom\n", + "\n", + "# Number of tickets sold\n", + "n = 460\n", + "# Probability of a passenger showing up\n", + "p = 0.97\n", + "# Seats available\n", + "seats = 450\n", + "\n", + "# Calculate the probability of having 450 or fewer passengers showing up\n", + "probability = binom.cdf(seats, n, p)\n", + "\n", + "print(f'The probability that all passengers have seats is: {probability:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Challenge 2**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Ironhack Call Center " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of needing at least 3 attempts is: 0.1470\n" + ] + } + ], + "source": [ + "# Probability of success on an individual attempt\n", + "p_success = 0.3\n", + "\n", + "# Probability of failure on an individual attempt\n", + "p_failure = 1 - p_success\n", + "\n", + "# Probability of needing at least 3 attempts (first two attempts fail, third is a success)\n", + "probability_at_least_3_attempts = (p_failure ** 2) * p_success\n", + "\n", + "print(f'The probability of needing at least 3 attempts is: {probability_at_least_3_attempts:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Challenge 3**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Ironhack Website" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed is: 0.0129\n" + ] + } + ], + "source": [ + "from scipy.stats import poisson\n", + "\n", + "# Average visit rate\n", + "lambda_rate = 500\n", + "# Server capacity: we want more than 550 visits\n", + "server_capacity = 550\n", + "\n", + "# Calculate the cumulative probability of visits up to 550\n", + "probability_not_overwhelmed = poisson.cdf(server_capacity, lambda_rate)\n", + "\n", + "# Calculate the probability of the server being overwhelmed\n", + "probability_overwhelmed = 1 - probability_not_overwhelmed\n", + "\n", + "print(f'The probability of the website server being overwhelmed is: {probability_overwhelmed:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What is the probability of being overwhelmed at some point during a day? (consider 24hours)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed at some point during the day is: 0.2677\n" + ] + } + ], + "source": [ + "# Probability of being overwhelmed in a single hour\n", + "probability_overwhelmed = 1 - poisson.cdf(server_capacity, lambda_rate)\n", + "\n", + "# Probability of being overwhelmed at least once in 24 hours\n", + "probability_overwhelmed_day = 1 - (1 - probability_overwhelmed) ** 24\n", + "\n", + "print(f'The probability of the website server being overwhelmed at some point during the day is: {probability_overwhelmed_day:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Challenge 4**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Ironhack Helpdesk" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the next customer will arrive within the next 5 minutes is: 0.3935\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "# Mean time between arrivals in minutes\n", + "mean_time = 10\n", + "# Rate parameter for the exponential distribution\n", + "lambda_rate = 1 / mean_time\n", + "\n", + "# Time period we're interested in (within 5 minutes)\n", + "x = 5\n", + "\n", + "# Probability calculation\n", + "probability_within_5_minutes = 1 - math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that the next customer will arrive within the next 5 minutes is: {probability_within_5_minutes:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that an employee can take a 5-minute break is: 0.2231\n" + ] + } + ], + "source": [ + "# Time period for no customers (15 minutes)\n", + "x = 15\n", + "\n", + "# Probability calculation\n", + "probability_no_customers_15_minutes = math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that an employee can take a 5-minute break is: {probability_no_customers_15_minutes:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Challenge 5**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that a bird weighs between 140 and 160 grams is: 0.6827\n" + ] + } + ], + "source": [ + "from scipy.stats import norm\n", + "\n", + "# Mean and standard deviation\n", + "mu = 150\n", + "sigma = 10\n", + "\n", + "# Standardize the values\n", + "z_140 = (140 - mu) / sigma\n", + "z_160 = (160 - mu) / sigma\n", + "\n", + "# Calculate the cumulative probabilities\n", + "probability_140 = norm.cdf(z_140)\n", + "probability_160 = norm.cdf(z_160)\n", + "\n", + "# Difference gives us the probability of the bird's weight being between 140 and 160 grams\n", + "probability_between_140_and_160 = probability_160 - probability_140\n", + "\n", + "print(f'The probability that a bird weighs between 140 and 160 grams is: {probability_between_140_and_160:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Challenge 6**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the component fails within the first 30 hours is: 0.4512\n" + ] + } + ], + "source": [ + "# Mean lifetime in hours\n", + "mean_lifetime = 50\n", + "# Rate parameter for the exponential distribution\n", + "lambda_rate = 1 / mean_lifetime\n", + "\n", + "# Time frame to consider (first 30 hours)\n", + "x = 30\n", + "\n", + "# Probability calculation\n", + "probability_failure_within_30_hours = 1 - math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that the component fails within the first 30 hours is: {probability_failure_within_30_hours:.4f}')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-intro-probability.ipynb b/lab-intro-probability.ipynb index 5893fc1..6f40565 100644 --- a/lab-intro-probability.ipynb +++ b/lab-intro-probability.ipynb @@ -38,11 +38,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: scipy in c:\\users\\barba\\anaconda3\\lib\\site-packages (1.13.1)\n", + "Requirement already satisfied: numpy<2.3,>=1.22.4 in c:\\users\\barba\\anaconda3\\lib\\site-packages (from scipy) (1.26.4)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ - "#code here" + "pip install scipy" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that all passengers have seats is: 0.8845\n" + ] + } + ], + "source": [ + "from scipy.stats import binom\n", + "\n", + "# Number of tickets sold\n", + "n = 460\n", + "# Probability of a passenger showing up\n", + "p = 0.97\n", + "# Seats available\n", + "seats = 450\n", + "\n", + "# Calculate the probability of having 450 or fewer passengers showing up\n", + "probability = binom.cdf(seats, n, p)\n", + "\n", + "print(f'The probability that all passengers have seats is: {probability:.4f}')" ] }, { @@ -72,11 +111,28 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of needing at least 3 attempts is: 0.1470\n" + ] + } + ], "source": [ - "#code here" + "# Probability of success on an individual attempt\n", + "p_success = 0.3\n", + "\n", + "# Probability of failure on an individual attempt\n", + "p_failure = 1 - p_success\n", + "\n", + "# Probability of needing at least 3 attempts (first two attempts fail, third is a success)\n", + "probability_at_least_3_attempts = (p_failure ** 2) * p_success\n", + "\n", + "print(f'The probability of needing at least 3 attempts is: {probability_at_least_3_attempts:.4f}')" ] }, { @@ -107,11 +163,32 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed is: 0.0129\n" + ] + } + ], "source": [ - "#code here" + "from scipy.stats import poisson\n", + "\n", + "# Average visit rate\n", + "lambda_rate = 500\n", + "# Server capacity: we want more than 550 visits\n", + "server_capacity = 550\n", + "\n", + "# Calculate the cumulative probability of visits up to 550\n", + "probability_not_overwhelmed = poisson.cdf(server_capacity, lambda_rate)\n", + "\n", + "# Calculate the probability of the server being overwhelmed\n", + "probability_overwhelmed = 1 - probability_not_overwhelmed\n", + "\n", + "print(f'The probability of the website server being overwhelmed is: {probability_overwhelmed:.4f}')" ] }, { @@ -123,11 +200,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed at some point during the day is: 0.2677\n" + ] + } + ], "source": [ - "#code here" + "# Probability of being overwhelmed in a single hour\n", + "probability_overwhelmed = 1 - poisson.cdf(server_capacity, lambda_rate)\n", + "\n", + "# Probability of being overwhelmed at least once in 24 hours\n", + "probability_overwhelmed_day = 1 - (1 - probability_overwhelmed) ** 24\n", + "\n", + "print(f'The probability of the website server being overwhelmed at some point during the day is: {probability_overwhelmed_day:.4f}')" ] }, { @@ -157,10 +248,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the next customer will arrive within the next 5 minutes is: 0.3935\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "# Mean time between arrivals in minutes\n", + "mean_time = 10\n", + "# Rate parameter for the exponential distribution\n", + "lambda_rate = 1 / mean_time\n", + "\n", + "# Time period we're interested in (within 5 minutes)\n", + "x = 5\n", + "\n", + "# Probability calculation\n", + "probability_within_5_minutes = 1 - math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that the next customer will arrive within the next 5 minutes is: {probability_within_5_minutes:.4f}')" + ] }, { "cell_type": "markdown", @@ -173,10 +287,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that an employee can take a 5-minute break is: 0.2231\n" + ] + } + ], + "source": [ + "# Time period for no customers (15 minutes)\n", + "x = 15\n", + "\n", + "# Probability calculation\n", + "probability_no_customers_15_minutes = math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that an employee can take a 5-minute break is: {probability_no_customers_15_minutes:.4f}')" + ] }, { "cell_type": "markdown", @@ -196,11 +326,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that a bird weighs between 140 and 160 grams is: 0.6827\n" + ] + } + ], "source": [ - "#code here" + "from scipy.stats import norm\n", + "\n", + "# Mean and standard deviation\n", + "mu = 150\n", + "sigma = 10\n", + "\n", + "# Standardize the values\n", + "z_140 = (140 - mu) / sigma\n", + "z_160 = (160 - mu) / sigma\n", + "\n", + "# Calculate the cumulative probabilities\n", + "probability_140 = norm.cdf(z_140)\n", + "probability_160 = norm.cdf(z_160)\n", + "\n", + "# Difference gives us the probability of the bird's weight being between 140 and 160 grams\n", + "probability_between_140_and_160 = probability_160 - probability_140\n", + "\n", + "print(f'The probability that a bird weighs between 140 and 160 grams is: {probability_between_140_and_160:.4f}')" ] }, { @@ -219,19 +374,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the component fails within the first 30 hours is: 0.4512\n" + ] + } + ], "source": [ - "#code here" + "# Mean lifetime in hours\n", + "mean_lifetime = 50\n", + "# Rate parameter for the exponential distribution\n", + "lambda_rate = 1 / mean_lifetime\n", + "\n", + "# Time frame to consider (first 30 hours)\n", + "x = 30\n", + "\n", + "# Probability calculation\n", + "probability_failure_within_30_hours = 1 - math.exp(-lambda_rate * x)\n", + "\n", + "print(f'The probability that the component fails within the first 30 hours is: {probability_failure_within_30_hours:.4f}')" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -243,9 +417,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.12.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }