cmkl_assignments/fall-2024/math/mat-204/Game.ipynb

293 lines
14 KiB
Plaintext
Raw Normal View History

{
"cells": [
2024-11-23 20:14:21 +07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MAT-204:00010 - Probability\n",
"Author: Thanawin Pattanaphol - Date: 23th December 2024 - Description: Basic probability calculations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Importing Libraries"
]
},
{
"cell_type": "code",
2024-11-23 22:45:10 +07:00
"execution_count": 3,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'PIL'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[3], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpd\u001b[39;00m \n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib_venn\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m venn3\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pyplot \u001b[38;5;28;01mas\u001b[39;00m plt \n\u001b[1;32m 5\u001b[0m Dat \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mread_csv(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDataLoL.csv\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"File \u001b[0;32m~/.local/lib/python3.12/site-packages/matplotlib_venn/__init__.py:56\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;124;03mVenn diagram plotting routines.\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[38;5;124;03m arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))\u001b[39;00m\n\u001b[1;32m 54\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m---> 56\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib_venn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_venn2\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m venn2, venn2_circles\n\u001b[1;32m 57\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib_venn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_venn3\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m venn3, venn3_circles\n\u001b[1;32m 58\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib_venn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_util\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m venn2_unweighted, venn3_unweighted\n",
"File \u001b[0;32m~/.local/lib/python3.12/site-packages/matplotlib_venn/_venn2.py:24\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mwarnings\u001b[39;00m\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcollections\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Counter\n\u001b[0;32m---> 24\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01maxes\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Axes\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpatches\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Circle\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcolors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ColorConverter\n",
"File \u001b[0;32m~/.local/lib/python3.12/site-packages/matplotlib/__init__.py:159\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpackaging\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mversion\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m parse \u001b[38;5;28;01mas\u001b[39;00m parse_version\n\u001b[1;32m 157\u001b[0m \u001b[38;5;66;03m# cbook must import matplotlib only within function\u001b[39;00m\n\u001b[1;32m 158\u001b[0m \u001b[38;5;66;03m# definitions, so it is safe to import from it here.\u001b[39;00m\n\u001b[0;32m--> 159\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m _api, _version, cbook, _docstring, rcsetup\n\u001b[1;32m 160\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcbook\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m sanitize_sequence\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_api\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m MatplotlibDeprecationWarning\n",
"File \u001b[0;32m~/.local/lib/python3.12/site-packages/matplotlib/rcsetup.py:28\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbackends\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BackendFilter, backend_registry\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcbook\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ls_mapper\n\u001b[0;32m---> 28\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcolors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Colormap, is_color_like\n\u001b[1;32m 29\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_fontconfig_pattern\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m parse_fontconfig_pattern\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_enums\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m JoinStyle, CapStyle\n",
"File \u001b[0;32m~/.local/lib/python3.12/site-packages/matplotlib/colors.py:52\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnumbers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Real\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mre\u001b[39;00m\n\u001b[0;32m---> 52\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mPIL\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Image\n\u001b[1;32m 53\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mPIL\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mPngImagePlugin\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PngInfo\n\u001b[1;32m 55\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mmpl\u001b[39;00m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'PIL'"
]
}
],
"source": [
"import pandas as pd \n",
"from matplotlib_venn import venn3\n",
"from matplotlib import pyplot as plt \n",
"\n",
"Dat = pd.read_csv('DataLoL.csv')\n",
"\n",
2024-11-23 20:14:21 +07:00
"num_games = len(Dat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Questions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team wins"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Calculating the probability by diving the amount of games that blue won\n",
"# dividing it by the amount of total games\n",
"# Thus: p = Number of time an event occurs / Total nmumber of possible events\n",
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / num_games\n",
"print(\"Probability that Blue Team wins:\", prob_blue_wins)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team wins and kills the dragon"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Doing a similar calculation with the difference being\n",
"# the number of time an event occurs now only counts\n",
"# the number of times blue team wins and kills the dragon\n",
"\n",
"prob_blue_wins_dragons = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueDragons'] == 1)]) / num_games\n",
"print(\"Probability that Blue Team wins and kills the dragon:\", prob_blue_wins_dragons)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team wins and kills the herald"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Similar calculation but with the number of events that\n",
"# team blue wins and kills the herald\n",
"\n",
"prob_blue_wins_heralds = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueHeralds'] == 1)]) / num_games\n",
"print(\"Probability that Blue Team wins and kills the herald:\", prob_blue_wins_heralds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Count: All possible cases (Venn Diagram)"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Declaring variable for each set\n",
"# Loop through the whole games dataset and if the item-\n",
"# -matches the condition, add it to the set for that\n",
"# condition list\n",
"\n",
"game_blue_wins, game_blue_dragon, game_blue_herald = set()\n",
"\n",
2024-11-23 20:14:21 +07:00
"for game in range(num_games):\n",
" if(Dat['blueWins'][game] == 1):\n",
" game_blue_wins.add(game)\n",
2024-11-23 22:45:10 +07:00
" if(Dat['blueDragons'][game] == 1):\n",
" game_blue_dragon.add(game)\n",
2024-11-23 22:45:10 +07:00
" if(Dat['blueHeralds'][game] == 1):\n",
" game_blue_herald.add(game)\n",
"\n",
"venn3([game_blue_wins, game_blue_dragon, game_blue_herald], ('A', 'B', 'C'))\n",
"\n",
"plt.show()"
]
},
2024-11-23 20:14:21 +07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Each possible events (Venn Diagram)"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Setting the variable for each section of the venn diagram\n",
"# Use these values to calculate its probability by\n",
"# diving with the total amount of games\n",
"# Produce venn diagram of the values\n",
"\n",
"count_a = 2055\n",
"count_b = 1096\n",
"count_c = 564\n",
"\n",
"count_ab = 1770\n",
"count_ac = 583\n",
"count_bc = 188\n",
"\n",
"count_abc = 522\n",
"\n",
2024-11-23 20:14:21 +07:00
"p_a = round(2055 / num_games, 3)\n",
"p_b = round(1096 / num_games, 3)\n",
"p_c = round(564 / num_games, 3)\n",
"\n",
2024-11-23 20:14:21 +07:00
"p_ab = round(1770 / num_games, 3)\n",
"p_ac = round(583 / num_games, 3)\n",
"p_bc = round(188 / num_games, 3)\n",
"\n",
2024-11-23 20:14:21 +07:00
"p_abc = round(522 / num_games, 3)\n",
"\n",
"venn = venn3(subsets=(p_a, p_b, p_ab, p_c, p_ac, p_bc, p_abc), set_labels=('A', 'B', 'C'))"
]
},
2024-11-23 20:14:21 +07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team does not manage to do any of the events (Outer White Section)"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Calculate the total amount of the games in the venn diagram\n",
"# Find the complement of (A B C)\n",
"\n",
"count_all = count_a + count_b + count_c + count_ab + count_abc + count_ac + count_bc\n",
"did_not_win_all = num_games - p_all_wins\n",
"\n",
"print(\"Probabilty that Blue Team loses, doesn't kill dragons and heralds:\", did_not_win_all / num_games)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team wins and kills dragon and herald"
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# The probability of this event is essentially the intersection of\n",
"# A (Blue Team Wins), B (Kills Dragon) and C (Kills Herald)\n",
"\n",
"print(\"Probability that blue team wins, kills dragon and herald:\", p_abc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team does not win but kills dragon and the herald "
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Same with above but only with the intersection between\n",
"# B (Kills Dragon) and C (Kills Herald)\n",
"\n",
"print(\"Probability that blue team does not win but kills dragon and the herald :\", p_bc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability: Blue Team wins but does not kill dragon and herald. "
]
},
{
"cell_type": "code",
2024-11-23 20:14:21 +07:00
"execution_count": null,
"metadata": {},
2024-11-23 22:45:10 +07:00
"outputs": [],
"source": [
2024-11-23 20:14:21 +07:00
"# Same with above but only with the A section (Blue Team wins)\n",
"\n",
"print(\"Probability that blue team wins without killing the dragon and the herald:\", p_a)"
]
}
],
"metadata": {
"kernelspec": {
2024-11-23 22:45:10 +07:00
"display_name": "Python 3 (ipykernel)",
"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",
2024-11-23 22:45:10 +07:00
"version": "3.12.7"
}
},
"nbformat": 4,
2024-11-23 22:45:10 +07:00
"nbformat_minor": 4
}