{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c58309b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "sns.set_theme(style=\"whitegrid\", context=\"notebook\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6732353-51d5-4478-9cf8-5834e57e5a4e",
   "metadata": {},
   "source": [
    "# Chapter 1 Example Problems"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48d9ec9e-83da-40ca-ae79-3c45f8af137c",
   "metadata": {},
   "source": [
    "## Main Concepts\n",
    "\n",
    "Outcome: A result of a random experiment.\n",
    "\n",
    "Sample Space: The set of all possible outcomes.\n",
    "\n",
    "Event: A subset of the sample space.\n",
    "\n",
    "Inclusion-exclusion principle holds for probability\n",
    "\n",
    "Consider a sample space S. If S is a countable set, this refers to a discrete probability\n",
    "mode\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ac122be-50b2-423c-b88f-e4b3327b21bd",
   "metadata": {},
   "source": [
    "## Example Problems"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb87a35-a470-4d98-935f-80c814e3f95d",
   "metadata": {},
   "source": [
    "Example 1.5 - soln\n",
    "\n",
    "- there are 10 people with white shirts and 8 people with red shirts;\n",
    "- 4 people have black shoes and white shirts\n",
    "- 3 people have black shoes and red shirts\n",
    "- the total number of people with white or red shirts or black shoes is 21\n",
    "\n",
    "Let A be the set of people with white shirts, B be the set of people with red shirts and let C be the set of people with black shoes.\n",
    "\n",
    "\\begin{align*}\n",
    "|A|=10 \\\\\n",
    "|B|=8 \\\\\n",
    "|A \\cap C| = 4 \\\\\n",
    "|B \\cap C| = 3 \\\\\n",
    "|A \\cup B \\cup C| = 21\n",
    "\\end{align*}\n",
    "\n",
    "Now we solve for $|C|$:\n",
    "\n",
    "\\begin{align*}\n",
    "|A| + |B| + |C| - |A \\cap B| - |A \\cap C| - |B \\cap C| + |A \\cap B \\cap C| = 21 \\\\\n",
    "10 + 8 + |C| - 0 - 4 - 3 - 0 = 21 \\\\\n",
    "18 + |C| - 7 = 21 \\\\\n",
    "|C| + 11 = 21 \\\\\n",
    "|C| = 10\n",
    "\\end{align*}\n",
    "\n",
    "$\\therefore$ number of people with black shoes is 10\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72b40733-531a-48f3-9879-75601684afc2",
   "metadata": {},
   "source": [
    "Example 1.11 - soln\n",
    "\n",
    "Suppose we have the following information:\n",
    "1. There is a 60 percent chance that it will rain today.\n",
    "2. There is a 50 percent chance that it will rain tomorrow.\n",
    "3. There is a 30 percent chance that it does not rain either day.\n",
    "\n",
    "T = rains\n",
    "F = no rain\n",
    "\n",
    "$S = \\{(F, F), (F, T), (T, F), (T, T)\\}$\n",
    "\n",
    "$P((T, F) \\cup (T, T)) = 0.6$\n",
    "\n",
    "$P((F, T) \\cup (T, T)) = 0.5$\n",
    "\n",
    "$P((F, F)) = 0.3$\n",
    "\n",
    "\\begin{align*}\n",
    "P(S) = 1 \\\\\n",
    "P(\\{(F, F)\\} \\cup \\{(F, T)\\} \\cup \\{(T, F)\\} \\cup \\{(T, T)\\}) = 1 \\\\\n",
    "P((F,F)) + P(\\{(F, T)\\} \\cup \\{(T, F)\\} \\cup \\{(T, T)\\}) = 1 \\\\\n",
    "0.3 + P(\\{(F, T)\\} \\cup \\{(T, F)\\} \\cup \\{(T, T)\\}) = 1 \\\\\n",
    "P(\\{(F, T)\\} \\cup \\{(T, F)\\} \\cup \\{(T, T)\\}) = 0.7 \\\\\n",
    "P(\\{(F, T)\\} \\cup \\{(T, T)\\}) + P((T, F)) = 0.7 \\\\\n",
    "0.5 + P((T, F)) = 0.7 \\\\\n",
    "P((T, F)) = 0.2 \\\\\n",
    "P(\\{(T, F)\\} \\cup \\{(T, T)\\}) + P((F, T)) = 0.7 \\\\\n",
    "P((F, T)) = 0.1\n",
    "\\end{align*}\n",
    "\n",
    "Find the following probabilities:\n",
    "\n",
    "a. The probability that it will rain today or tomorrow.\n",
    "\n",
    "\\begin{align*}\n",
    "P((T, F) \\cup (F, T) \\cup (T, T)) = 0.7\n",
    "\\end{align*}\n",
    "\n",
    "b. The probability that it will rain today and tomorrow.\n",
    "\n",
    "\\begin{align*}\n",
    "P((T, T)) = 1 - 0.3 - 0.2 - 0.1 = 0.4\n",
    "\\end{align*}\n",
    "\n",
    "c. The probability that it will rain today but not tomorrow.\n",
    "\n",
    "\\begin{align*}\n",
    "P((T, F)) = 0.2\n",
    "\\end{align*}\n",
    "\n",
    "d. The probability that it either will rain today or tomorrow, but not both.\n",
    "\n",
    "\\begin{align*} \n",
    "P(\\{(T, F)\\} \\cup \\{(F, T)\\}) = P((T, F)) + P((F, T)) = 0.2 + 0.1 = 0.3\n",
    "\\end{align*}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b5131dd-5ebd-4156-b808-f8df273317fb",
   "metadata": {},
   "source": [
    "Example 1.12 - soln\n",
    "\n",
    "$S = \\{ -1, 0, 1, 2, 3, ... \\}$\n",
    "\n",
    "$\\forall x \\in S, P(x) = \\frac{1}{2^{x + 2}}$\n",
    "\n",
    "What is the probability that I win more than or equal to 1 dollar and less than 4 dollars?\n",
    "\n",
    "\\begin{align*} \n",
    "P({1, 2, 3}) = P(1) + P(2) + P(3) \\\\\n",
    "= 1/8 + 1/16 + 1/32\n",
    "\\end{align*}\n",
    "\n",
    "What is the probability that I win more than 2 dollars?\n",
    "\n",
    "\\begin{align*} \n",
    "\\sum_{i=3}^{\\infty} P(i) = P(3) + P(4) + P(5) + P(6) + ... \\\\\n",
    "= 1/32 + 1/64 + 1/128 + 1/256 + ... \\\\\n",
    "=\\frac{\\frac{1}{32}}{1 - \\frac{1}{2}}\n",
    "=\\frac{1}{16}\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bc9a64b",
   "metadata": {},
   "source": [
    "# 1.3.6 Problems. \n",
    "\n",
    "## Problem 1 - skipping\n",
    "\n",
    "## Problem 2 \n",
    "\n",
    "a. \n",
    "\n",
    "\\begin{align*}\n",
    "S = \\{ x \\mid x >= 2, x \\in \\mathbb{N} \\}\n",
    "\\end{align*}\n",
    "\n",
    "b.\n",
    "1 red, 1 blue, 1 white and 1 green\n",
    "\n",
    "\\begin{align*}\n",
    "S = \\{ (R, B), (B, R), (R, W), (W, R), (R, G), (G, R), (B, W), (W, B), (B, G), (G, B), (W, G), (G, W) \\}\n",
    "\\end{align*}\n",
    "\n",
    "c.\n",
    "\n",
    "$S = [0, \\frac{1}{3})$\n",
    "\n",
    "## Problem 3\n",
    "\n",
    "- $A \\cup B \\cup C = S$\n",
    "- $P(A) = \\frac{3}{6}$\n",
    "- $P(B) = \\frac{4}{6}$\n",
    "- $P(A \\cup B) = \\frac{5}{6}$\n",
    "\n",
    "### a. Find $P(A \\cap B)$\n",
    "\n",
    "$P(A \\cap B) = P(A) + P(B) - P(A \\cup B) = \\frac{3}{6} + \\frac{4}{6} - \\frac{5}{6} = \\frac{2}{6}$\n",
    "\n",
    "### b. Do $A$, $B$ and $C$ form a partition of $S$?\n",
    "\n",
    "No. Proof by contridiction:\n",
    "\n",
    "Assume $A$, $B$ and $C$ form a partition of $S$. That would imply $P(S) = P(A) + P(B) + P(C) = 1$.\n",
    "\n",
    "$P(A) = \\frac{3}{6}$ and $P(B) = \\frac{4}{6}$, so $P(A) + P(B) = \\frac{7}{6}$\n",
    "\n",
    "$P(A) + P(B) > P(S)$ therefore contirdiction.\n",
    "\n",
    "$A$, $B$ and $C$ do not form a partition of $S$. $\\blacksquare$\n",
    "\n",
    "### c. Find $P(C \\setminus (A \\cup B))$\n",
    "\n",
    "\\begin{align*}\n",
    "A \\cup B \\cup C = S \\\\\n",
    "A \\cup B \\cup C \\setminus (A \\cup B) = S \\setminus (A \\cup B) \\\\\n",
    "C \\setminus (A \\cup B) = S \\setminus (A \\cup B)\n",
    "\\end{align*}\n",
    "\n",
    "Therefore $P(C \\setminus (A \\cup B)) = P(S \\setminus (A \\cup B))$.\n",
    "\n",
    "Notice $P(S \\setminus (A \\cup B)) = P((A \\cup B)^c)$, so $P(C \\setminus (A \\cup B)) = P((A \\cup B)^c)$\n",
    "\n",
    "So\n",
    "\n",
    "\\begin{align*}\n",
    "P(C - (A \\cup B)) &= P((A \\cup B)^c) \\\\\n",
    "&= P(S) - P(A \\cup B) \\\\\n",
    "&= 1 - \\frac{5}{6} \\\\\n",
    "&= \\frac{1}{6}\n",
    "\\end{align*}\n",
    "\n",
    "### d. If $P(C \\cap (A \\cup B)) = \\frac{5}{12}, P(C) =$ ?\n",
    "\n",
    "$C = (C \\cap (A \\cup B)) \\cup C \\setminus (A \\cup B)$\n",
    "\n",
    "Therefore\n",
    "\n",
    "\\begin{align*}\n",
    "P(C) &= P((C \\cap (A \\cup B)) \\cup C \\setminus (A \\cup B)) \\\\\n",
    "&= P(C \\cap (A \\cup B)) + P(C \\setminus (A \\cup B)) \\\\\n",
    "&= \\frac{5}{12} + \\frac{1}{6} \\\\\n",
    "&= \\frac{7}{12}\n",
    "\\end{align*}\n",
    "\n",
    "## Problem 4.\n",
    "\n",
    "### a. \n",
    "Let $X$ be our sample space for the first row, and $Y$ the second roll. ($X \\cup Y = S$)\n",
    "\n",
    "$X = \\{ 1, 2, 3, 4, 5, 6 \\}$\n",
    "\n",
    "Let $x$ be the first dice row and $y$ the second dice roll. ($x \\in X$ and $y \\in Y$)\n",
    "\n",
    "The probability $x < y$ is $|X| - x$ or $6 - x$.\n",
    "\n",
    "So \n",
    "\n",
    "\\begin{align*}\n",
    "\\sum_{x \\in X} (6 - x) &= \\sum_{x \\in X} 6 - \\sum_{x \\in X} x \\\\\n",
    "&= 36 - (1 + 2 + 3 + 4 + 5 + 6) \\\\\n",
    "&= 15\n",
    "\\end{align*}\n",
    "\n",
    "Therefore $P(A) = \\frac{15}{36}$\n",
    "\n",
    "### b.\n",
    "\n",
    "Number of outcomes where 6 is only first: 1 * 5\n",
    "Number of outcomes where 6 is only second: 1 * 5 \n",
    "Number of outcomes where 6 is both: 1\n",
    "\n",
    "Therefore $P(B) = \\frac{11}{36}$\n",
    "\n",
    "## Problem 5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e589ecc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29256 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.25091334378745\n"
     ]
    }
   ],
   "source": [
    "xmin = 0\n",
    "xmax = 20\n",
    "\n",
    "x = np.linspace(xmin, xmax, 200)\n",
    "y = 1 - np.exp((-1 * (x / 5)))\n",
    "\n",
    "# ----------------------------------------------------\n",
    "\n",
    "plt.figure(figsize=(8, 5))\n",
    "\n",
    "# 1. Updated the LaTeX label to match your new math\n",
    "plt.plot(x, y, color=\"crimson\", linewidth=2.5, label=r\"$y = e^{-x/5}$\")\n",
    "\n",
    "plt.xlim(xmin, xmax)\n",
    "\n",
    "# 2. Tightened the Y-limits so you can actually see the decay curve\n",
    "plt.ylim(0, 1)\n",
    "\n",
    "plt.legend(fontsize=12, loc=\"upper right\")  # Moved to upper right since curve decays downward\n",
    "plt.show()\n",
    "\n",
    "# print((1 - 2.71 ** (-3/5)) * 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00afcbd1",
   "metadata": {},
   "source": [
    "### a.\n",
    "\n",
    "Our sample space $S$ is the time at which the product breaks. The product could break immediatly ($T = 0$) or never break ($T \\to \\infty$)\n",
    "\n",
    "Therefore $S = [0, \\infty)$\n",
    "\n",
    "### b.\n",
    "\n",
    "$P(T \\geq 0) = e^{-\\frac{t}{5}} = 1$\n",
    "\n",
    "This makes sense, as this states that the probability the product doesnt break immediatly is 1. This would have to be the case, otherwise the product was broken to begin with, which is an impossible senario.\n",
    "\n",
    "$\\lim_{t \\to \\infty} P(T \\geq t) = \\lim_{t \\to \\infty} e^{-\\frac{t}{5}} = 0$ (skiping proof, can be easily seen in graph above)\n",
    "\n",
    "This means the as time goes on, the probability the product doesnt break approachs 0. This would imply the product will eventually break given infinite time.\n",
    "\n",
    "### c.\n",
    "\n",
    "$t_1 \\lt t_2 \\implies P(T \\geq t_1) \\geq P(T \\geq t_2)$.\n",
    "\n",
    "Proof\n",
    "\n",
    "\\begin{align*}\n",
    "t_1 \\leq t_2 \\\\\n",
    "-\\frac{t_1}{5} \\cdot -5 \\geq -\\frac{t_2}{5} \\cdot -5 \\\\\n",
    "-\\frac{t_1}{5} \\geq -\\frac{t_2}{5} \\\\\n",
    "e^{-\\frac{t_1}{5}} \\geq e^{-\\frac{t_2}{5}} \\\\\n",
    "P(T \\geq t_1) \\geq P(T \\geq t_2) \n",
    "\\end{align*}\n",
    "\n",
    "$\\blacksquare$\n",
    "\n",
    "This implies a more time means more probability the product may breakdown which makes intuitive sense.\n",
    "\n",
    "### d. Find the probability that the product breaks down within three years of the purchase time.\n",
    "\n",
    "<!-- \\begin{align*}\n",
    "\\int_{0}^{3} e^{-\\frac{t}{5}} dt\\\\\n",
    "[-5e^{-\\frac{t}{5}} + C]_0^3 \\\\\n",
    "(-5e^{-\\frac{3}{5}} + C) - (-5e^{-\\frac{0}{5}} + C) \\\\\n",
    "-5e^{-\\frac{3}{5}} + C + 5e^{-\\frac{0}{5}} - C \\\\\n",
    "-5e^{-\\frac{3}{5}} + 5 \\\\\n",
    "\\approx 2.251\n",
    "\\end{align*} -->\n",
    "\n",
    "### e. Find the probability that the product breaks down in the second year\n",
    "\n",
    "$P(1 \\leq T \\lt 2)=$?\n",
    "\n",
    "\\begin{align*}\n",
    "P(1 \\leq T \\lt 2)=P(T \\geq 1) + P(T \\lt 2)\n",
    "\\end{align*}\n",
    "\n",
    "## Problem 6\n",
    "\n",
    "Triangle inequaility:\n",
    "\n",
    "$x + y \\gt z$\n",
    "\n",
    "$y + z \\gt x$\n",
    "\n",
    "$x + z \\gt y$\n",
    "\n",
    "Since $x + y + z = 1$ then:\n",
    "\n",
    "$x \\lt \\frac{1}{2}$\n",
    "\n",
    "$y \\lt \\frac{1}{2}$\n",
    "\n",
    "$z \\lt \\frac{1}{2}$\n",
    "\n",
    "The area of our sample space is the plane where $x + y + z = 1$ in $\\mathbb{R}^3$, which is the triangle with points $(1, 0, 0)$, $(0, 1, 0)$, and $(0, 0, 1)$.\n",
    "\n",
    "Area of $S$ is $\\frac{\\sqrt{3}}{2}$ (skipping proof).\n",
    "\n",
    "The triangle $A$ is $\\frac{\\sqrt{3}}{8}$\n",
    "\n",
    "$P(A) = \\frac{|A|}{|S|} = \\frac{\\frac{\\sqrt{3}}{8}}{\\frac{\\sqrt{3}}{2}} = \\frac{\\sqrt{3}}{8} \\cdot \\frac{2}{\\sqrt{3}} = \\frac{1}{4}$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4609aba5-dfdc-4500-bec2-cfa2e544cca7",
   "metadata": {},
   "source": [
    "\n",
    "### Example 1.17 - soln\n",
    "\n",
    "I roll a fair die twice and obtain two numbers X1 = result of the first roll and X2 = result\n",
    "of the second roll. Given that I know X1 + X2 = 7, what is the probability that X1 = 4 or\n",
    "X2 = 4?\n",
    "\n",
    "What is our sample space?\n",
    "\n",
    "\\begin{align*} \n",
    "D = \\{1,2,3,4,5,6\\} \\\\\n",
    "S = D \\times D\n",
    "\\end{align*}\n",
    "\n",
    "\\begin{align*} \n",
    "P(A|B) = \\frac{P(A \\cap B)}{P(B)}\n",
    "\\end{align*}\n",
    "\n",
    "\\begin{align*} \n",
    "A = \\{(x,y) \\mid x=4 \\lor y = 4 \\} \\\\\n",
    "|A| = 12 \\\\\n",
    "P(A) = \\frac{|A|}{|S|} \\\\\n",
    "= \\frac{12}{36} = \\frac{1}{3}\n",
    "\\end{align*}\n",
    "\n",
    "> NOTE my event set A may be overcounting \n",
    "\n",
    "\\begin{align*} \n",
    "B = \\{(x,y) \\mid x+y = 7 , x,y \\in \\mathbb{N}\\} \\\\\n",
    "|B| = 6 \\\\\n",
    "P(B) = \\frac{1}{6} \\\\\n",
    "\\end{align*}\n",
    "\n",
    "\\begin{align*} \n",
    "P(A \\cap B) = \\frac{1}{18}\n",
    "\\end{align*}\n",
    "\n",
    "\n",
    "\\begin{align*} \n",
    "B = \\{(x,y) \\mid x+y = 7 , x,y \\in \\mathbb{N}\\} \\\\\n",
    "A = \\{(x,y) \\mid x=4 \\lor y = 4 \\} \\\\\n",
    "P(A|B) = \\frac{P(A \\cap B)}{P(B)} \\\\\n",
    "= \\frac{\\frac{1}{18} }{ \\frac{1}{6} } \\\\\n",
    "= \\frac{6}{18} = \\frac{1}{3}\n",
    "\\end{align*}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12c8a0d4",
   "metadata": {},
   "source": [
    "## Example 1.18\n",
    "\n",
    "### a.\n",
    "\n",
    "$S_\\text{conditional} = \\{\\{G, G\\}, \\{G, B\\}\\}$, therefore $\\frac{1}{2}$\n",
    "\n",
    "### b.\n",
    "\n",
    "$S_\\text{conditional} = \\{\\{G, G\\}, \\{G, B\\}, \\{B, G\\}\\}$, therefore $\\frac{1}{3}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1305f9d6",
   "metadata": {},
   "source": [
    "$$P(B,C \\mid A) = P(B \\mid A) \\cdot P(C \\mid A,B)$$\n",
    "\n",
    "$$P(A_1 \\cap A_2 \\cap \\dots \\cap A_n \\mid E) = P(A_1 \\mid E) \\cdot P(A_2 \\mid A_1, E) \\cdot P(A_3 \\mid A_1, A_2, E) \\cdots P(A_n \\mid A_1, \\dots, A_{n-1}, E)$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca15c14a",
   "metadata": {},
   "source": [
    "### Example 1.19\n",
    "\n",
    "In a factory there are 100 units of a certain product, 5 of which are defective. We pick\n",
    "three units from the 100 units at random. What is the probability that none of them are\n",
    "defective?\n",
    "\n",
    "Let $A_1$ be the event where we pick a non defective unit first, $A_2$ on our second, and $A_3$ on our third.\n",
    "\n",
    "We want to find $P(A_1 \\cap A_2 \\cap A_3)$.\n",
    "\n",
    "Note that $P(A_1 \\cap A_2 \\cap A_3) = P(A_1)P(A_2|A_1)P(A_3|A_1,A_2)$\n",
    "\n",
    "$$P(A_1) = \\frac{95}{100}$$\n",
    "\n",
    "$$P(A_2|A_1) = \\frac{94}{99}$$\n",
    "\n",
    "$$P(A_3|A_1,A_2) = \\frac{93}{98}$$\n",
    "\n",
    "\\begin{align*} \n",
    "P(A)P(B|A)P(C|A,B) &= \\frac{95}{100} \\cdot \\frac{94}{99} \\cdot \\frac{93}{98} \\\\\n",
    "\\approx 0.856\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4790a1dc",
   "metadata": {},
   "source": [
    "### Example 1.20\n",
    "\n",
    "$S = [1:11]$\n",
    "\n",
    "Let $A$ be the event $N \\lt 7$, $N \\in S$\n",
    "\n",
    "Let $B$ be the event $N$ is even. Are $A$ and $B$ independent?\n",
    "\n",
    "$$P(A) = \\frac{|A|}{|S|} = \\frac{6}{10} = \\frac{3}{5}$$\n",
    "\n",
    "Note that $|B| = 5$.\n",
    "\n",
    "$$P(A|B) = \\frac{|A \\cap B|}{|B|} = \\frac{3}{5}$$\n",
    "\n",
    "$P(A|B) = P(A) \\therefore A$ and $B$ are independent. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b16a508d",
   "metadata": {},
   "source": [
    "### Example 1.21\n",
    "\n",
    "$P(HHHHT) = P(H)P(H)P(H)P(H)P(T) = \\frac{1}{2^5} = \\frac{1}{32}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08fe32c4",
   "metadata": {},
   "source": [
    "### Example 1.22\n",
    "\n",
    "Number of plane rides over 20 years $= 20 \\cdot 20 = 400$\n",
    "\n",
    "Let $A_i$ be the flight businessman dies where $i \\in [1:401]$\n",
    "\n",
    "We want to find $P(A_1 \\cup A_2 \\cup A_3 \\cup \\dots \\cup A_{400})$\n",
    "\n",
    "\\begin{align*}\n",
    "P(A_1 \\cup A_2 \\cup A_3 \\cup \\dots \\cup A_n) &= 1 - (1 - P(A_1))(1 - P(A_2))\\cdots(1 - P(A_{400})) \\\\\n",
    "&= 1 - (1 - \\frac{1}{4\\cdot 10^6})^{400} \\\\\n",
    "\\approx 10^{-4}\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42589434",
   "metadata": {},
   "source": [
    "### Example 1.23\n",
    "\n",
    "a.\n",
    "\n",
    "What is the probability someone makes a shot in a round?\n",
    "\n",
    "Let $A$ be the event player 1 makes the shot, and $B$ the event player 2 makes the shot. \n",
    "\n",
    "\\begin{align*}\n",
    "P(A \\cup B) &= 1 - (1 - P(A))(1 - P(B)) \\\\\n",
    "&= 1 - (1 - p_1)(1 - p_2) \\\\\n",
    "&= p_1 + p_2 - p_1p_2\n",
    "\\end{align*}\n",
    "\n",
    "What is the probability nobody makes a shot in a round?\n",
    "\n",
    "$$P((A \\cup B)^c) = 1 - p_1 - p_2 + p_1p_2 = 1 - (1 - p_1)(1 - p_2)$$\n",
    "\n",
    "What is the probability player one wins in the first round?\n",
    "\n",
    "$$P(W_1(1)) = p_1$$\n",
    "\n",
    "What is the probability player one wins in the second round?\n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1(2)) = P(R_1(1)) + P(R_1(2)) \\\\\n",
    "= p_1 + P((A \\cup B)^c)p_1 \\\\\n",
    "= p_1 + (1 - (1 - p_1)(1 - p_2))p_1\n",
    "\\end{align*}\n",
    "\n",
    "What is the probability player one wins in the third round?\n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1(3)) = P(R_1(1)) + P(R_1(2)) + P(R_1(3)) \\\\\n",
    "= p_1 + P((A \\cup B)^c)p_1 + P((A \\cup B)^c)P((A \\cup B)^c)p_1 \\\\\n",
    "\\end{align*}\n",
    "\n",
    "What is the probability player one wins on round $n$?\n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1(n)) = P(R_1(1)) + P(R_1(2)) + P(R_1(3)) + \\dots + P(R_1(n)) \\\\\n",
    "= p_1 + P((A \\cup B)^c)p_1 + P((A \\cup B)^c)P((A \\cup B)^c)p_1 + \\dots + P((A \\cup B)^c)^np_1 \\\\\n",
    "= p_1 + (1 - p_1 - p_2 + p_1p_2)p_1 + (1 - p_1 - p_2 + p_1p_2)^2p_1 + \\dots + (1 - p_1 - p_2 + p_1p_2)^np_1 \\\\\n",
    "= p_1 (1 + (1 - p_1 - p_2 + p_1p_2) + (1 - p_1 - p_2 + p_1p_2)^2 + \\dots + (1 - p_1 - p_2 + p_1p_2)^n) \n",
    "\\end{align*}\n",
    "\n",
    "Let $x = 1 - p_1 - p_2 + p_1p_2$, substitute for \n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1(n)) = p_1 (1 + x + x^2 + \\dots + x^n) \n",
    "\\end{align*}\n",
    "\n",
    "Notice that $|x| \\lt 1$. Proof\n",
    "\n",
    "$$x = 1 - p_1 - p_2 + p_1p_2 = (1 - p_1)(1 - p_2)$$\n",
    "\n",
    "Since $0<p_1,p_2<1$, we have\n",
    "\n",
    "$0 < (1 - p_1) < 1$ and $0 < (1 - p_2) < 1$ which implies $0 < (1 - p_1)(1 - p_2) < 1$ so $0 < x < 1$ which implies $|x| < 1$\n",
    "\n",
    "Now we consider \n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1) &= p_1 \\cdot \\lim_{n \\to \\infty} (1 + x + x^2 + \\dots + x^n) \\\\\n",
    "&= p_1 \\cdot (\\frac{1}{1 - x}) \\\\\n",
    "&= p_1 \\cdot (\\frac{1}{1 - (1 - p_1 - p_2 + p_1p_2)}) \\\\\n",
    "&= \\frac{p_1}{p_1 + p_2 - p_1p_2} \\\\\n",
    "\\end{align*}\n",
    "\n",
    "b.\n",
    "\n",
    "If $P(W_1) = \\frac{1}{2}$, find the values of $p_1$ and $p_2$.\n",
    "\n",
    "\\begin{align*}\n",
    "P(W_1) &= \\frac{1}{2} \\\\\n",
    "\\frac{p_1}{p_1 + p_2 - p_1p_2} &= \\frac{1}{2} \\\\\n",
    "2p_1 &= p_1 + p_2 - p_1p_2 \\\\\n",
    "0 &= p_1 + p_2 - p_1p_2 - 2p_1 \\\\\n",
    "0 &= p_2 - p_1p_2 - p_1 \\\\\n",
    "\\end{align*}\n",
    "\n",
    "In terms of $p_1$\n",
    "\n",
    "$$p_2 = \\frac{p_1}{1 - p_1}$$\n",
    "\n",
    "In terms of $p_2$\n",
    "\n",
    "$$p_1 = \\frac{p_2}{1 + p_2}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7be1e873",
   "metadata": {},
   "source": [
    "### Example 1.24\n",
    "\n",
    "Let $A$ be the event where my chosen marble is red.\n",
    "\n",
    "Let $B_1$ be the set of the first bag, $B_2$ the second bag, $B_3$ the third bag.\n",
    "\n",
    "Notice that $B_1, B_2,$ and $B_3$ are disjoint and therefore form a partition of our sample space.\n",
    "\n",
    "\n",
    "\\begin{align*}\n",
    "P(A) &= P(A|B_1)P(B_1) + P(A|B_2)P(B_2) + P(A|B_3)P(B_3) \\\\\n",
    "&= \\frac{75}{100} \\cdot \\frac{1}{3} + \\frac{60}{100} \\cdot \\frac{1}{3} + \\frac{45}{100} \\cdot \\frac{1}{3} \\\\\n",
    "&= \\frac{180}{300} = \\frac{6}{10} = 0.6\n",
    "\\end{align*}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3160c4c",
   "metadata": {},
   "source": [
    "### Example 1.25\n",
    "\n",
    "Let $A$ be the event we chose a red marble and let $B$ be the event we chose from the first bag.\n",
    "\n",
    "\\begin{align*}\n",
    "P(B|A) &= \\frac{P(A|B)P(B)}{P(A)} \\\\\n",
    "&= \\frac{\\frac{75}{100} \\cdot \\frac{1}{3}}{\\frac{6}{10}} \\\\\n",
    "&= \\frac{75}{300} \\cdot \\frac{10}{6} \\\\\n",
    "&= \\frac{750}{1800} = \\frac{75}{180} = \\frac{15}{36} = \\frac{5}{12} \\\\\n",
    "\\end{align*}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34782cd8",
   "metadata": {},
   "source": [
    "### Example 1.26\n",
    "\n",
    "Let $A$ be the event we chose a red marble and let $B$ be the event we chose from the first bag.\n",
    "\n",
    "\\begin{align*}\n",
    "P(B|A) &= \\frac{P(A|B)P(B)}{P(A)} \\\\\n",
    "\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "086ffc85",
   "metadata": {},
   "source": [
    "### TODO Example 1.27\n",
    "\n",
    "\\begin{align*}\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4ef0787",
   "metadata": {},
   "source": [
    "# TODO 1.4.5 Problems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d283ef80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "roadmap (3.14.5)",
   "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.14.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
