{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "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 Notes"
   ]
  },
  {
   "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": 32,
   "id": "e589ecc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 70728 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 438,
       "width": 696
      }
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.25091334378745\n"
     ]
    }
   ],
   "source": [
    "xmin = -4\n",
    "xmax = 20\n",
    "\n",
    "x = np.linspace(xmin, xmax, 200)\n",
    "y = 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, 2)\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*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4609aba5-dfdc-4500-bec2-cfa2e544cca7",
   "metadata": {},
   "source": [
    "### 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": "code",
   "execution_count": null,
   "id": "11163495-a6cc-43b9-bf41-02748b13d210",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "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
}
