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
197 changes: 171 additions & 26 deletions lab-intro-probability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability that there are seats for all passengers: 0.1155\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import binom\n",
"\n",
"# Define parameters\n",
"n = 460 # Total tickets sold\n",
"p = 0.03 # Probability of not showing up\n",
"\n",
"# We want to find the probability that 10 or more passengers do not show up\n",
"# This means we want P(X >= 10)\n",
"prob_seats_for_all = binom.cdf(9, n, p) # P(X <= 9), complement of P(X >= 10)\n",
"\n",
"print(f\"Probability that there are seats for all passengers: {prob_seats_for_all:.4f}\")"
]
},
{
Expand Down Expand Up @@ -72,11 +90,27 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability that at least three attempts are needed: 0.4900\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import geom\n",
"\n",
"# Given probability of success\n",
"p = 0.3\n",
"\n",
"# Calculate P(X >= 3) using the complement of P(X <= 2)\n",
"prob_at_least_three_attempts = 1 - geom.cdf(2, p)\n",
"\n",
"print(f\"Probability that at least three attempts are needed: {prob_at_least_three_attempts:.4f}\")"
]
},
{
Expand Down Expand Up @@ -107,11 +141,27 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of the server being overwhelmed: 0.0129\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import poisson\n",
"\n",
"# Average rate of visits per hour\n",
"lambda_ = 500\n",
"\n",
"# To calculate the probability of more than 550 visits\n",
"prob_over_550 = 1 - poisson.cdf(550, lambda_)\n",
"\n",
"print(f\"Probability of the server being overwhelmed: {prob_over_550:.4f}\")"
]
},
{
Expand All @@ -123,11 +173,34 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of being overwhelmed at some point during the day: 0.2677\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import poisson\n",
"\n",
"# Parameters\n",
"lambda_ = 500 # average visits per hour\n",
"max_capacity = 550\n",
"\n",
"# Probability of NOT being overwhelmed in one hour\n",
"prob_not_overwhelmed_one_hour = poisson.cdf(max_capacity, lambda_)\n",
"\n",
"# Probability of NOT being overwhelmed in 24 hours\n",
"prob_not_overwhelmed_all_day = prob_not_overwhelmed_one_hour ** 24\n",
"\n",
"# Probability of being overwhelmed at least once in 24 hours\n",
"prob_overwhelmed_some_time = 1 - prob_not_overwhelmed_all_day\n",
"\n",
"print(f\"Probability of being overwhelmed at some point during the day: {prob_overwhelmed_some_time:.4f}\")"
]
},
{
Expand Down Expand Up @@ -157,10 +230,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability that the next customer arrives within 5 minutes: 0.3935\n"
]
}
],
"source": [
"from scipy.stats import expon\n",
"\n",
"# Rate parameter\n",
"lambda_ = 1 / 10 # customers per minute\n",
"\n",
"# Probability the next customer arrives within 5 minutes\n",
"prob_within_5_minutes = expon.cdf(5, scale=1/lambda_)\n",
"\n",
"print(f\"Probability that the next customer arrives within 5 minutes: {prob_within_5_minutes:.4f}\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -173,10 +264,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of taking a break (no arrivals in 15 minutes): 0.2231\n"
]
}
],
"source": [
"import math\n",
"\n",
"# Rate parameter\n",
"lambda_ = 1 / 10 # customers per minute\n",
"\n",
"# Probability of no arrivals in 15 minutes\n",
"prob_no_arrival_15_minutes = math.exp(-lambda_ * 15)\n",
"\n",
"print(f\"Probability of taking a break (no arrivals in 15 minutes): {prob_no_arrival_15_minutes:.4f}\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -196,11 +305,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability that a bird's weight is between 140 and 160 grams: 0.6827\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import norm\n",
"\n",
"# Given parameters\n",
"mean = 150\n",
"std_dev = 10\n",
"\n",
"# Calculate the probability for the range 140 to 160 grams\n",
"probability_between_140_and_160 = norm.cdf(160, loc=mean, scale=std_dev) - norm.cdf(140, loc=mean, scale=std_dev)\n",
"\n",
"print(f\"Probability that a bird's weight is between 140 and 160 grams: {probability_between_140_and_160:.4f}\")"
]
},
{
Expand All @@ -219,17 +345,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability that the component fails within the first 30 hours: 0.4512\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import expon\n",
"\n",
"# Mean lifetime\n",
"mean_lifetime = 50\n",
"\n",
"# Rate parameter\n",
"lambda_ = 1 / mean_lifetime\n",
"\n",
"# Probability of failure within the first 30 hours\n",
"prob_failure_30_hours = expon.cdf(30, scale=mean_lifetime)\n",
"\n",
"print(f\"Probability that the component fails within the first 30 hours: {prob_failure_30_hours:.4f}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -243,7 +388,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down