cmkl_assignments/fall-2024/math/mat-205/00010/MAT-205_00010.ipynb

240 lines
8.7 KiB
Plaintext
Raw Normal View History

2024-11-27 20:52:31 +07:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import scipy.stats as stats\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"Dat = pd.read_csv('DataLoL.csv')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(hypergeometric) Probability of observing between 21 and 40 cases of breast cancer: 0.664806\n",
"(hypergeometric) Probability of observing more than 60 cases of breast cancer: 0.000352\n",
"(hypergeometric) Probability of observing less than or equal to 30 cases of breast cancer: 0.108530\n",
"(hypergeometric) Probability of observing exactly 35 cases of breast cancer: 0.059625\n"
]
}
],
"source": [
"# Given parameters\n",
"n = 36121175 # the female population of Thailand ()\n",
"r = round((38 / 100000) * n) # the number of women in Thailand with breast cancer\n",
"k = 100000 # sample size\n",
"\n",
"# Create the hypergeometric distribution object\n",
"rv = stats.hypergeom(n, r, k)\n",
"\n",
"# Calculate the probability using hypergeometric distribution\n",
"prob_21_to_40 = rv.cdf(40) - rv.cdf(20) # (a)\n",
"\n",
"prob_more_than_60 = 1 - rv.cdf(60) # (b)\n",
"\n",
"prob_less_than_equal_30 = rv.cdf(30) # (c)\n",
"\n",
"prob_exactly_35 = rv.pmf(35) # (d)\n",
"\n",
"# Print the results\n",
"print(f\"(hypergeometric) Probability of observing between 21 and 40 cases of breast cancer: {prob_21_to_40:.6f}\")\n",
"print(f\"(hypergeometric) Probability of observing more than 60 cases of breast cancer: {prob_more_than_60:.6f}\")\n",
"print(f\"(hypergeometric) Probability of observing less than or equal to 30 cases of breast cancer: {prob_less_than_equal_30:.6f}\")\n",
"print(f\"(hypergeometric) Probability of observing exactly 35 cases of breast cancer: {prob_exactly_35:.6f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 3**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Binomial) Probability of observing between 21 and 40 cases of breast cancer: 0.664631\n",
"(Binomial) Probability of observing more than 60 cases of breast cancer: 0.000359\n",
"(Binomial) Probability of observing less than or equal to 30 cases of breast cancer: 0.108849\n",
"(Binomial) Probability of observing exactly 35 cases of breast cancer: 0.059569\n"
]
}
],
"source": [
"# Given parameters\n",
"n = 36121175 # the female population of Thailand\n",
"r = round((38 / 100000) * n) # the number of women in Thailand with breast cancer\n",
"k = 100000 # sample size\n",
"p = r / n # Probability of success\n",
"\n",
"# calculate the probability using binomial approximation\n",
"prob_21_to_40_binom = stats.binom.cdf(40, k, p) - stats.binom.cdf(20, k, p) # (a)\n",
"\n",
"prob_more_than_60_binom = 1 - stats.binom.cdf(60, k, p) # (b)\n",
"\n",
"prob_less_than_equal_30_binom = stats.binom.cdf(30, k, p) # (c)\n",
"\n",
"prob_exactly_35_binom = stats.binom.pmf(35, k, p) # (d)\n",
"\n",
"# Print the results\n",
"print(f\"(Binomial) Probability of observing between 21 and 40 cases of breast cancer: {prob_21_to_40_binom:.6f}\")\n",
"print(f\"(Binomial) Probability of observing more than 60 cases of breast cancer: {prob_more_than_60_binom:.6f}\")\n",
"print(f\"(Binomial) Probability of observing less than or equal to 30 cases of breast cancer: {prob_less_than_equal_30_binom:.6f}\")\n",
"print(f\"(Binomial) Probability of observing exactly 35 cases of breast cancer: {prob_exactly_35_binom:.6f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 4**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Poisson) Probability of observing between 21 and 40 cases of breast cancer: 0.664607\n",
"(Poisson) Probability of observing more than 60 cases of breast cancer: 0.000360\n",
"(Poisson) Probability of observing less than or equal to 30 cases of breast cancer: 0.108893\n",
"(Poisson) Probability of observing exactly 35 cases of breast cancer: 0.059561\n"
]
}
],
"source": [
"# Given parameters\n",
"k = 100000 # Sample size\n",
"n = 36121175 # Total female population of Thailand\n",
"r = round((38 / 100000) * n) # Estimated number of women with breast cancer\n",
"p = r / n # Probability of success\n",
"\n",
"# Calculate lambda for Poisson approximation\n",
"lambda_poisson = k * p\n",
"\n",
"# calculate the probability using poisson approximation\n",
"prob_21_to_40_poisson = stats.poisson.cdf(40, lambda_poisson) - stats.poisson.cdf(20, lambda_poisson) # (a)\n",
"\n",
"prob_more_than_60_poisson = 1 - stats.poisson.cdf(60, lambda_poisson) # (b)\n",
"\n",
"prob_less_than_equal_30_poisson = stats.poisson.cdf(30, lambda_poisson) # (c)\n",
"\n",
"prob_exactly_35_poisson = stats.poisson.pmf(35, lambda_poisson) # (d)\n",
"\n",
"# Print the results\n",
"print(f\"(Poisson) Probability of observing between 21 and 40 cases of breast cancer: {prob_21_to_40_poisson:.6f}\")\n",
"print(f\"(Poisson) Probability of observing more than 60 cases of breast cancer: {prob_more_than_60_poisson:.6f}\")\n",
"print(f\"(Poisson) Probability of observing less than or equal to 30 cases of breast cancer: {prob_less_than_equal_30_poisson:.6f}\")\n",
"print(f\"(Poisson) Probability of observing exactly 35 cases of breast cancer: {prob_exactly_35_poisson:.6f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 5**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of X happens before the 7th game: 0.794816\n",
"Probability of X happens at the 7th game: 0.047604\n",
"Probability of X happens after the 7th game: 0.157579\n",
"Probability of Y happens befor the 7th game: 0.277992\n",
"Probability of Y happens at the 7th game: 0.038150\n",
"Probability of Y happens after the 7th game: 0.683857\n"
]
}
],
"source": [
"# number of game in Dat\n",
"totalGame = len(Dat)\n",
"\n",
"# calculate probability of team blue wins and kills the dragon\n",
"probBlueWinsAndDragons = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueDragons'] == 1)]) / totalGame\n",
"\n",
"# calculate probability of team blue wins, kills the dragon and kills the heralds\n",
"probBlueWinsAndDragonsAndHeralds = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueDragons'] == 1) & (Dat['blueHeralds'] == 1)]) / totalGame\n",
"\n",
"# calculate probability of the event X\n",
"probXLessthan7 = stats.geom.cdf(6, probBlueWinsAndDragons) # (a)\n",
"\n",
"probXExactly7 = stats.geom.pmf(7, probBlueWinsAndDragons) # (b)\n",
"\n",
"probXMorethan7 = 1 - stats.geom.cdf(7, probBlueWinsAndDragons) # (c)\n",
"\n",
"# calculate probability of the event Y\n",
"probYLessthan7 = stats.geom.cdf(6, probBlueWinsAndDragonsAndHeralds) # (a)\n",
"\n",
"probYxactly7 = stats.geom.pmf(7, probBlueWinsAndDragonsAndHeralds) # (b)\n",
"\n",
"probYMorethan7 = 1 - stats.geom.cdf(7, probBlueWinsAndDragonsAndHeralds) # (c)\n",
"\n",
"# Print the results\n",
"print(f\"Probability of X happens before the 7th game: {probXLessthan7:.6f}\")\n",
"print(f\"Probability of X happens at the 7th game: {probXExactly7:.6f}\")\n",
"print(f\"Probability of X happens after the 7th game: {probXMorethan7:.6f}\")\n",
"print(f\"Probability of Y happens befor the 7th game: {probYLessthan7:.6f}\")\n",
"print(f\"Probability of Y happens at the 7th game: {probYxactly7:.6f}\")\n",
"print(f\"Probability of Y happens after the 7th game: {probYMorethan7:.6f}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}