Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 195 additions & 24 deletions lab-intro-probability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,34 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 1: Ironhack Airlines\n",
"Probability all passengers have seats: 0.8845\n",
"That's about 88.45%\n"
]
}
],
"source": [
"#code here"
"# Challenge ones\n",
"\n",
"from scipy.stats import binom\n",
"import numpy as np\n",
"\n",
"# plane has 450 seats, sells 460 tickets\n",
"# Each passenger has 3% chance of missing (97% show up)\n",
"n = 460 # tickets sold\n",
"p = 0.97 # probability of showing up\n",
"\n",
"# we want probability that 450 or fewer show up\n",
"prob_all_seated = binom.cdf(450, n, p)\n",
"\n",
"print(f\"Challenge 1: Ironhack Airlines\")\n",
"print(f\"Probability all passengers have seats: {prob_all_seated:.4f}\")\n",
"print(f\"That's about {prob_all_seated*100:.2f}%\")"
]
},
{
Expand Down Expand Up @@ -72,11 +97,36 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 2: Call Center\n",
"Probability needs at least 3 attempts: 0.4900\n",
"Or about 49.00%\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import geom\n",
"\n",
"# probability of success on first attempt = 0.3\n",
"p = 0.3\n",
"\n",
"# need at least 3 attempts means first 2 fail\n",
"# P(X >= 3) = 1 - P(X < 3) = 1 - P(X=1) - P(X=2)\n",
"\n",
"prob_1 = geom.pmf(1, p)\n",
"prob_2 = geom.pmf(2, p)\n",
"\n",
"prob_at_least_3 = 1 - prob_1 - prob_2\n",
"\n",
"print(\"Challenge 2: Call Center\")\n",
"print(f\"Probability needs at least 3 attempts: {prob_at_least_3:.4f}\")\n",
"print(f\"Or about {prob_at_least_3*100:.2f}%\")"
]
},
{
Expand Down Expand Up @@ -107,11 +157,30 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 3: Website\n",
"Probability server overwhelmed in 1 hour: 0.0129\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import poisson\n",
"\n",
"# average 500 visits per hour\n",
"# server handles up to 550\n",
"lambda_rate = 500\n",
"\n",
"# probability of MORE than 550 visits\n",
"prob_overwhelmed = 1 - poisson.cdf(550, lambda_rate)\n",
"\n",
"print(\"Challenge 3: Website\")\n",
"print(f\"Probability server overwhelmed in 1 hour: {prob_overwhelmed:.4f}\")#code here"
]
},
{
Expand All @@ -123,11 +192,30 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of being overwhelmed during the 24 hours: 0.2677\n",
"result is 26.77%\n"
]
}
],
"source": [
"#code here"
"# whats probability not overwhelmed in one hour\n",
"prob_not_overwhelmed = 1 - prob_overwhelmed\n",
"\n",
"# probability of being not overwhelmed for all 24 hours\n",
"prob_never_overwhelmed_24h = prob_not_overwhelmed ** 24\n",
"\n",
"# probability overwhelmed at least once\n",
"prob_overwhelmed_24h = 1 - prob_never_overwhelmed_24h\n",
"\n",
"print(f\"Probability of being overwhelmed during the 24 hours: {prob_overwhelmed_24h:.4f}\")\n",
"print(f\"result is {prob_overwhelmed_24h*100:.2f}%\")"
]
},
{
Expand Down Expand Up @@ -159,8 +247,32 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 4: Helpdesk\n",
"Probability customer arrives within 5 min: 0.3935\n",
"roughly 39.3%\n"
]
}
],
"source": [
"from scipy.stats import expon\n",
"\n",
"# average customer every ten mins\n",
"# so lambda = 1/10 = 0.1 per minute\n",
"mean_time = 10\n",
"lambda_rate = 1/mean_time\n",
"\n",
"# probability next customer arrives within 5 minutes\n",
"prob_within_5 = expon.cdf(5, scale=mean_time)\n",
"\n",
"print(\"Challenge 4: Helpdesk\")\n",
"print(f\"Probability customer arrives within 5 min: {prob_within_5:.4f}\")\n",
"print(f\"roughly {prob_within_5*100:.1f}%\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -173,10 +285,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Probability employee gets a break: 0.2231\n",
"Result is 22.31%\n"
]
}
],
"source": [
"# probability no customer for 15 minutes (employee gets break)\n",
"\n",
"# this is P(X > 15)\n",
"prob_no_customer_15 = 1 - expon.cdf(15, scale=mean_time)\n",
"\n",
"print(f\"\\nProbability employee gets a break: {prob_no_customer_15:.4f}\")\n",
"print(f\"Result is {prob_no_customer_15*100:.2f}%\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -196,11 +326,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 5: Bird Weights\n",
"Probability weight is between 140-160g: 0.6827\n",
"Final result is 68.27%\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import norm\n",
"\n",
"# mean is 150g, std dev is 10g\n",
"mean = 150\n",
"std = 10\n",
"\n",
"# probability of between 140 and 160\n",
"prob_between = norm.cdf(160, mean, std) - norm.cdf(140, mean, std)\n",
"\n",
"print(\"Challenge 5: Bird Weights\")\n",
"print(f\"Probability weight is between 140-160g: {prob_between:.4f}\")\n",
"print(f\"Final result is {prob_between*100:.2f}%\")"
]
},
{
Expand All @@ -219,17 +370,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Challenge 6: Electronic Component\n",
"Probability component fails within 30 hours: 0.4512\n",
"About 45.1%\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import expon\n",
"\n",
"# mean lifetime is 50 hours\n",
"mean_lifetime = 50\n",
"\n",
"# probability of it fails within first 30 hours\n",
"prob_fail_30 = expon.cdf(30, scale=mean_lifetime)\n",
"\n",
"print(\"Challenge 6: Electronic Component\")\n",
"print(f\"Probability component fails within 30 hours: {prob_fail_30:.4f}\")\n",
"print(f\"About {prob_fail_30*100:.1f}%\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -243,7 +414,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down