cmkl_assignments/fall-2024/math/mat-204/00020/MAT-204_00020.ipynb

245 lines
9.1 KiB
Plaintext
Raw Normal View History

2024-11-27 20:52:31 +07:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"# read file\n",
"Dat = pd.read_csv(\"DataLoL.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Total Game played\n",
"total_game = len(Dat['blueWins']) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 1**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of team blue destroyed more structure than team red, if team blue won (using Dat) is 0.06795131845841784\n",
"Probability of team blue destroyed more structure than team red, if team blue won (using dat_blue_wins) is 0.06795131845841786\n"
]
}
],
"source": [
"# calculate probability using Dat\n",
"prob_blue_wins_more_structure = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed'])]) / len(Dat) # P(A ∩ B)\n",
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game #P(A)\n",
"\n",
"print(\"Probability of team blue destroyed more structure than team red, if team blue won (using Dat) is \", prob_blue_wins_more_structure / prob_blue_wins)\n",
"\n",
"# calculate probability using dat_blue_wins\n",
"# filter game team blue wins\n",
"dat_blue_wins = Dat[Dat['blueWins'] == 1]\n",
"\n",
"# filter game team blue win and destroyed more strcuture\n",
"dat_blue_wins_more_structure = dat_blue_wins[dat_blue_wins['blueTowersDestroyed'] > dat_blue_wins['redTowersDestroyed']] # (A ∩ B)\n",
"\n",
"#Verify using dat_blue_wins\n",
"prob_blue_wins_more_structure = len(dat_blue_wins_more_structure) / len(dat_blue_wins) # P(B|A) = P(A ∩ B) / P(A)\n",
"print(\"Probability of team blue destroyed more structure than team red, if team blue won (using dat_blue_wins) is\", prob_blue_wins_more_structure)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2**"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using chain rule) 0.037858082801903024\n",
"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using new dat set) 0.037858082801903024\n"
]
}
],
"source": [
"# calcucate probability team blue wins\n",
"dat_blue_wins = Dat[Dat['blueWins'] == 1]\n",
"prob_blue_win = len(dat_blue_wins) / total_game\n",
"\n",
"# calculate probability team blue wins, kills the dragon\n",
"dat_blue_wins_and_kills_dragons = dat_blue_wins[dat_blue_wins['blueDragons'] == 1]\n",
"prob_blue_wins_and_kills_dragons = len(dat_blue_wins_and_kills_dragons) / len(dat_blue_wins) * prob_blue_win\n",
"\n",
"# calculate probability team blue wins, kills the dragon, kills the heralds\n",
"dat_blue_wins_and_kills_dragons_and_heralds = dat_blue_wins_and_kills_dragons[dat_blue_wins_and_kills_dragons['blueHeralds'] == 1]\n",
"prob_blue_wins_and_kills_dragons_and_heralds = len(dat_blue_wins_and_kills_dragons_and_heralds) / len(dat_blue_wins_and_kills_dragons) * prob_blue_wins_and_kills_dragons\n",
"\n",
"# calculate probability team blue wins, kills the dragon, kills the heralds and does the first kill\n",
"dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood = dat_blue_wins_and_kills_dragons_and_heralds[dat_blue_wins_and_kills_dragons_and_heralds['blueFirstBlood'] == 1]\n",
"prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood = len(dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood) / len(dat_blue_wins_and_kills_dragons_and_heralds) * prob_blue_wins_and_kills_dragons_and_heralds\n",
"\n",
"print(\"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using chain rule)\", prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood)\n",
"print(\"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using new dat set)\", len(dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood) / total_game)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 3**"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"Dat['blueQuantileGold'] = pd.qcut(Dat['blueTotalGold'], 4, labels=False)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P(A) calculated using Law of Total Probability = 0.4990383642069036\n",
"P(A) is 0.4990383642069035\n"
]
}
],
"source": [
"# filter data to each quantiles\n",
"dat_quantile_1 = Dat[Dat['blueQuantileGold'] == 0]\n",
"dat_quantile_2 = Dat[Dat['blueQuantileGold'] == 1]\n",
"dat_quantile_3 = Dat[Dat['blueQuantileGold'] == 2]\n",
"dat_quantile_4 = Dat[Dat['blueQuantileGold'] == 3]\n",
"\n",
"# calculate a proportion of game in each quantiles\n",
"quantile_1_prob = len(dat_quantile_1) / total_game\n",
"quantile_2_prob = len(dat_quantile_2) / total_game\n",
"quantile_3_prob = len(dat_quantile_3) / total_game\n",
"quantile_4_prob = len(dat_quantile_4) / total_game\n",
"\n",
"# calculate team blue win rate in each quantiles\n",
"quantile_1_win_rate = len(dat_quantile_1[dat_quantile_1['blueWins'] == 1]) / len(dat_quantile_1)\n",
"quantile_2_win_rate = len(dat_quantile_2[dat_quantile_2['blueWins'] == 1]) / len(dat_quantile_2)\n",
"quantile_3_win_rate = len(dat_quantile_3[dat_quantile_3['blueWins'] == 1]) / len(dat_quantile_3)\n",
"quantile_4_win_rate = len(dat_quantile_4[dat_quantile_4['blueWins'] == 1]) / len(dat_quantile_4)\n",
"\n",
"#calculate probability team blue wins using law of total probability\n",
"prob_a = (quantile_1_prob * quantile_1_win_rate) + (quantile_2_prob * quantile_2_win_rate) + (quantile_3_prob * quantile_3_win_rate) + (quantile_4_prob * quantile_4_win_rate)\n",
"print(\"P(A) calculated using Law of Total Probability = \", prob_a)\n",
"\n",
"#Verify\n",
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game # calculate probability (event/sample space)\n",
"print(\"P(A) is\", prob_blue_wins)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 4**"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of team blue wins given that it destroyed more structure than team red (using Bayes's theorem) is 0.7596371882086167\n",
"Probability of team blue wins given that it destroyed more structure than team red (using Dat) is 0.7596371882086167\n"
]
}
],
"source": [
"# calculate probability used in Bayes's theorem\n",
"\n",
"# P(A)\n",
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game\n",
"\n",
"# P(B)\n",
"prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n",
"\n",
"# P(A ∩ B)\n",
"dat_blue_more_structure = Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]\n",
"prob_blue_wins_more_structure = len(dat_blue_more_structure[dat_blue_more_structure['blueWins'] == 1]) / total_game\n",
"\n",
"# P(B|A)\n",
"prob_blue_more_structure_if_blue_wins = prob_blue_wins_more_structure / prob_blue_wins\n",
"\n",
"# calculate P(A|B) using Bayes's theorem\n",
"print(\"Probability of team blue wins given that it destroyed more structure than team red (using Bayes's theorem) is \", prob_blue_more_structure_if_blue_wins * prob_blue_wins / prob_blue_more_structure)\n",
"\n",
"\n",
"#Verify using Dat\n",
"\n",
"# P(A ∩ B)\n",
"prob_blue_wins_more_structure = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed'])]) / total_game \n",
"\n",
"# P(B)\n",
"prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n",
"\n",
"# calculate P(A|B) using Dat\n",
"print(\"Probability of team blue wins given that it destroyed more structure than team red (using Dat) is \", prob_blue_wins_more_structure / prob_blue_more_structure)\n",
"\n",
"\n"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}