{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MAT-204:00020 - Introduction to Probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "# Read the .csv file\n", "Dat = pd.read_csv(\"DataLoL.csv\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# Total amount of game played\n", "total_game = len(Dat['blueWins']) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Questions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 1**" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Probability of blue team destroying more structures than the red team AND if blue team won (via Dat): 0.06795131845841784\n", "Probability of blue team destroying more structures than red team, if blue team won (vat dat_blue_wins): 0.06795131845841786\n" ] } ], "source": [ "# Probability using the Dat dataset\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 blue team destroying more structures than the red team AND if blue team won (via Dat): \", prob_blue_wins_more_structure / prob_blue_wins)\n", "\n", "# Probability using dat_blue_wins\n", "# Filter the dataset to only the records where the blue team won.\n", "dat_blue_wins = Dat[Dat['blueWins'] == 1]\n", "\n", "# Filter the dataset to the records where the blue team won and destroyed more structured. \n", " # Essentially calculating the intersection of A & B or (A ∩ B)\n", "dat_blue_wins_more_structure = dat_blue_wins[dat_blue_wins['blueTowersDestroyed'] > dat_blue_wins['redTowersDestroyed']]\n", "\n", "# Verify the result using the dat_blue_wins set. \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 blue team destroying more structures than red team, if blue team won (vat dat_blue_wins): \", prob_blue_wins_more_structure)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 2**" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Chain Rule] Probability of blue team wins, kills the dragon, kills the heralds and does first kill: 0.037858082801903024\n", "[Dat set] Probability of blue team wins, kills the dragon, kills the heralds and does first kill: 0.037858082801903024\n" ] } ], "source": [ "# Probability that the blue team wins\n", "dat_blue_wins = Dat[Dat['blueWins'] == 1]\n", "prob_blue_win = len(dat_blue_wins) / total_game\n", "\n", "# Probability that blue team wins and also 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", "# Probability that blue team wins, kills the dragon and 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", "# Probability that blue team wins, kills the dragon, kills the heralds and does 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(\"[Chain Rule] Probability of blue team wins, kills the dragon, kills the heralds and does first kill:\", prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood)\n", "print(\"[Dat set] Probability of blue team wins, kills the dragon, kills the heralds and does first kill:\", 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": 30, "metadata": {}, "outputs": [], "source": [ "# Divide the dataset Dat['blueTotalGold'] into four quantiles. \n", "Dat['blueQuantileGold'] = pd.qcut(Dat['blueTotalGold'], 4, labels=False) \n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Law of Total Probability] P(A) calculated: 0.4990383642069036\n", "[Event / Sample Space] P(A): 0.4990383642069035\n" ] } ], "source": [ "# Declare each variable for 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", "# Proportion of the games 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", "# Rate that blue team wins for 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", "# Probability blue team wins via the 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(\"[Law of Total Probability] P(A) calculated:\", prob_a)\n", "\n", "# Verify the result using the probability calculated via event / sample space. \n", "prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game\n", "print(\"[Event / Sample Space] P(A):\", prob_blue_wins)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 4**" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Bayes's Theorem] Probability of blue team wins given that it destroys more structures than the red team: 0.7596371882086167\n", "[P(A|B) using Dat] Probability of blue team winning given that it destroys more structures than the red team: 0.7596371882086167\n" ] } ], "source": [ "# Probability via Bayes's Theorem\n", "\n", "# P(A) or the Probability that blue team wins.\n", "prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game\n", "\n", "# P(B) or the Probability that blue team destroys more structures than the red team. \n", "prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n", "\n", "# P(A ∩ B) or the Probability that blue team wins and also destroys more structures than the red team. \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", "# P(A|B) using Bayes's theorem\n", "print(\"[Bayes's Theorem] Probability of blue team wins given that it destroys more structures than the red team:\", prob_blue_more_structure_if_blue_wins * prob_blue_wins / prob_blue_more_structure)\n", "\n", "# Verify using the Dat dataset\n", "# P(A ∩ B) (same events as above)\n", "prob_blue_wins_more_structure = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed'])]) / total_game \n", "\n", "# P(B) (same events as above)\n", "prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n", "\n", "# calculate P(A|B) using Dat\n", "print(\"[P(A|B) using Dat] Probability of blue team winning given that it destroys more structures than the red team:\", prob_blue_wins_more_structure / prob_blue_more_structure)\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "env", "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }