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
231 changes: 211 additions & 20 deletions lab-intro-probability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,74 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#code here"
"from scipy.stats import binom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.8844772466215431\n"
]
}
],
"source": [
"p=0.97 # probability of passengers showing up for the flight\n",
"n=460 # number of tickets (trials)\n",
"distribution = binom(n,p)\n",
"print(distribution.cdf(450)) # the probabillity of hthe chance that there will be enough seats for all 450 passengers"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.11552275337845685\n"
]
}
],
"source": [
"p=0.97 # probability of passengers showing up for the flight\n",
"n=460 # number of tickets (trials)\n",
"distribution = binom(n,p)\n",
"print(1-distribution.cdf(450)) #this is a probability that more than 450 passengers will show up"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability that there are enough seats for all passengers is: 0.8845\n"
]
}
],
"source": [
"p=0.97 # probability of passengers showing up for the flight\n",
"n=460 # number of tickets (trials)\n",
"seats=450\n",
"\n",
"probability = binom.cdf(seats, n, p)\n",
"\n",
"print(f\"The probability that there are enough seats for all passengers is: {probability:.4f}\")\n"
]
},
{
Expand Down Expand Up @@ -70,13 +133,48 @@
"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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4409999999999998\n"
]
}
],
"source": [
"p=0.3\n",
"n=3\n",
"distribution = binom(n,p)\n",
"print(distribution.pmf(1)) # the probability that at least one of the three attempts will be succsessful is 44%\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.49\n"
]
}
],
"source": [
"#code here"
"import scipy.stats as stats\n",
"p = 0.3 # probability of success on a single attempt\n",
"\n",
"# Calculate the probability of needing at least 3 attempts\n",
"prob_at_least_three_attempts = 1 - stats.geom.cdf(2, p)\n",
"print(prob_at_least_three_attempts)\n",
"\n"
]
},
{
Expand Down Expand Up @@ -111,7 +209,26 @@
"metadata": {},
"outputs": [],
"source": [
"#code here"
"from scipy.stats import poisson"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0129\n"
]
}
],
"source": [
"n=500 # average number of visits per hour\n",
"probability_website_overwelmed = poisson(n).sf(550) # distribution of 550 visits and more - survival function (1 - cdf)\n",
"print(probability_website_overwelmed.round(4)) # probability is 1%"
]
},
{
Expand All @@ -123,11 +240,26 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.2677\n"
]
}
],
"source": [
"#code here"
"n = 500 # average number of visits per hour\n",
"prob_not_overwhelmed_hour = poisson(n).cdf(550) # probability of not being overwhelmed in one hour - at most 550 visits\n",
"\n",
"prob_not_overwhelmed_day = prob_not_overwhelmed_hour ** 24 # probability of not being overwhelmed in all 24 hours\n",
"\n",
"prob_overwhelmed_at_least_once = 1 - prob_not_overwhelmed_day # probability of being overwhelmed at least once during the day\n",
"\n",
"print(prob_overwhelmed_at_least_once.round(4)) # probability is 26%\n"
]
},
{
Expand Down Expand Up @@ -159,8 +291,25 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.9925534169290756)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rate_customer_arrives = 1/10\n",
"\n",
"from scipy.stats import expon\n",
"\n",
"expon(rate_customer_arrives).cdf(5) # the probability that customer arrives within next 5 minutes is 99,3%\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -173,10 +322,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"np.float64(3.3807434840493755e-07)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 - expon(rate_customer_arrives).cdf(15)\n",
"\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -196,11 +359,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The probability of the bird's weight is between 140 gr and 160 gr is 0.6826894921370859\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import norm\n",
"\n",
"mean = 150\n",
"std = 10\n",
"\n",
"norm_dist = norm(loc = mean, scale = std)\n",
"print(f\"The probability of the bird's weight is between 140 gr and 160 gr is {norm_dist.cdf(160) - norm_dist.cdf(140)}\")"
]
},
{
Expand All @@ -221,15 +398,29 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4511883639059735\n"
]
}
],
"source": [
"#code here"
"mean_lifetime = 50\n",
"\n",
"time_for_failure = 30\n",
"\n",
"prob_failure_within_30 = expon.cdf(time_for_failure, scale=mean_lifetime) # probability that the component fails within 30 hours\n",
"\n",
"print(prob_failure_within_30) # 45%\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -243,7 +434,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down