Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 21 Ben #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
208 changes: 182 additions & 26 deletions lab-intro-probability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that there are enough seats for all passengers is approximately 0.8505 or 85.05%.\n"
]
}
],
"source": [
"#code here"
"import scipy.stats as stats\n",
"\n",
"seats = 450\n",
"tickets_sold = 460\n",
"p_missing = 0.03\n",
"p_showing = 1 - p_missing\n",
"\n",
"# Calculate mean and standard deviation for the normal approximation\n",
"mean = tickets_sold * p_showing\n",
"variance = tickets_sold * p_showing * p_missing\n",
"std_dev = variance ** 0.5\n",
"\n",
"# Calculate the Z-score for 450 passengers showing up\n",
"z = (seats - mean) / std_dev\n",
"\n",
"# Calculate the cumulative probability\n",
"probability = stats.norm.cdf(z)\n",
"\n",
"# Output the result\n",
"print(f\"The probability that there are enough seats for all passengers is approximately {probability:.4f} or {probability * 100:.2f}%.\")\n"
]
},
{
Expand Down Expand Up @@ -72,11 +99,26 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that the representative needs to make at least three attempts is approximately 0.4900 or 49.00%.\n"
]
}
],
"source": [
"#code here"
"p_success = 0.3\n",
"p_failure = 1 - p_success\n",
"\n",
"# Probability of needing at least three attempts\n",
"probability_at_least_three_attempts = p_failure ** 2\n",
"\n",
"# Output the result\n",
"print(f\"The probability that the representative needs to make at least three attempts is approximately {probability_at_least_three_attempts:.4f} or {probability_at_least_three_attempts * 100:.2f}%.\")\n"
]
},
{
Expand Down Expand Up @@ -107,11 +149,26 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability of the website server being overwhelmed is approximately 0.0129 or 1.29%.\n"
]
}
],
"source": [
"#code here"
"lambda_visits = 500\n",
"server_capacity = 550\n",
"\n",
"# Calculate the probability of the server being overwhelmed\n",
"probability_overwhelmed = 1 - stats.poisson.cdf(server_capacity, lambda_visits)\n",
"\n",
"# Output the result\n",
"print(f\"The probability of the website server being overwhelmed is approximately {probability_overwhelmed:.4f} or {probability_overwhelmed * 100:.2f}%.\")\n"
]
},
{
Expand All @@ -125,9 +182,26 @@
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability of the website server being overwhelmed at some point during a day is approximately 0.0000 or 0.00%.\n"
]
}
],
"source": [
"#code here"
"lambda_visits_per_hour = 500\n",
"hours_in_a_day = 24\n",
"total_visits = lambda_visits_per_hour * hours_in_a_day # 12000 visits in a day\n",
"server_capacity = 550 * hours_in_a_day # 13200 visits\n",
"\n",
"# Calculate the probability of being overwhelmed at least once during the day\n",
"probability_overwhelmed_daily = 1 - stats.poisson.cdf(server_capacity, total_visits)\n",
"\n",
"# Output the result\n",
"print(f\"The probability of the website server being overwhelmed at some point during a day is approximately {probability_overwhelmed_daily:.4f} or {probability_overwhelmed_daily * 100:.2f}%.\")"
]
},
{
Expand Down Expand Up @@ -157,10 +231,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that the next customer will arrive within the next 5 minutes is approximately 0.3935 or 39.35%.\n"
]
}
],
"source": [
"import math\n",
"\n",
"# Given data\n",
"lambda_time = 10 # Average time between arrivals in minutes\n",
"mu = 1 / lambda_time # Rate of arrivals in customers per minute\n",
"t = 5 # Time period of interest in minutes\n",
"\n",
"# Calculate the probability that the next customer arrives within the next 5 minutes\n",
"probability_next_customer = 1 - math.exp(-mu * t)\n",
"\n",
"# Output the result\n",
"print(f\"The probability that the next customer will arrive within the next 5 minutes is approximately {probability_next_customer:.4f} or {probability_next_customer * 100:.2f}%.\")\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -173,10 +268,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that an employee can take a break is approximately 0.6065 or 60.65%.\n"
]
}
],
"source": [
"lambda_time = 10 # Average time between arrivals in minutes\n",
"mu = 1 / lambda_time # Rate of arrivals in customers per minute\n",
"t_break = 5 # Time for a break in minutes\n",
"t_no_customer = 15 # Time without a customer in minutes\n",
"\n",
"# Calculate the probability of no customer arriving in the next 5 minutes\n",
"probability_no_customer_next_5 = math.exp(-mu * t_break)\n",
"\n",
"# Output the result\n",
"print(f\"The probability that an employee can take a break is approximately {probability_no_customer_next_5:.4f} or {probability_no_customer_next_5 * 100:.2f}%.\")\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -196,11 +310,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that a randomly selected bird weighs between 140 and 160 grams is approximately 0.6827 or 68.27%.\n"
]
}
],
"source": [
"#code here"
"mean_weight = 150 # Mean in grams\n",
"std_dev_weight = 10 # Standard deviation in grams\n",
"\n",
"# Calculate probabilities\n",
"probability_140 = stats.norm.cdf(140, loc=mean_weight, scale=std_dev_weight)\n",
"probability_160 = stats.norm.cdf(160, loc=mean_weight, scale=std_dev_weight)\n",
"\n",
"# Probability that weight is between 140 and 160 grams\n",
"probability_between = probability_160 - probability_140\n",
"\n",
"# Output the result\n",
"print(f\"The probability that a randomly selected bird weighs between 140 and 160 grams is approximately {probability_between:.4f} or {probability_between * 100:.2f}%.\")"
]
},
{
Expand All @@ -219,17 +352,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that the component fails within the first 30 hours is approximately 0.4512 or 45.12%.\n"
]
}
],
"source": [
"#code here"
"mean_lifetime = 50 # Mean lifetime in hours\n",
"lambda_rate = 1 / mean_lifetime # Rate parameter\n",
"time_of_interest = 30 # Time in hours\n",
"\n",
"# Calculate the probability that the component fails within the first 30 hours\n",
"probability_failure_within_30 = 1 - math.exp(-lambda_rate * time_of_interest)\n",
"\n",
"# Output the result\n",
"print(f\"The probability that the component fails within the first 30 hours is approximately {probability_failure_within_30:.4f} or {probability_failure_within_30 * 100:.2f}%.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -243,9 +399,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}