{
 "cells": [
  {
   "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",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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",
    "num_games = len(Dat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Questions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Probability: Blue Team wins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that Blue Team wins is:  0.4990383642069035\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that Blue Team wins and kills the dragon is:  0.23200728818706348\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that Blue Team wins and kills the herald is:  0.11185342646016803\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26172 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 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",
    "for game in range(num_games):\n",
    "    if(Dat['blueWins'][game] == 1):\n",
    "        game_blue_wins.add(game)\n",
    "    else if(Dat['blueDragons'][game] == 1):\n",
    "        game_blue_dragon.add(game)\n",
    "    else 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()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Probability: Each possible events (Venn Diagram)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27112 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 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",
    "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",
    "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",
    "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'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Probability: Blue Team does not manage to do any of the events (Outer White Section)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probabilty that Blue Team loses, doesn't kill dragons and heralds:  0.3138981678307521\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that blue team wins, kills dragon and herald:  0.053\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that blue team kills dragon and the herald but does not win:  0.019\n"
     ]
    }
   ],
   "source": [
    "# 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",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability that blue team wins without killing the graon and the herald:  0.208\n"
     ]
    }
   ],
   "source": [
    "# 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": {
   "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.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
