{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "42bbd167",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Probability basics 10/02/26\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    "\n",
    "rng = np.random.default_rng(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37ffa1ee",
   "metadata": {},
   "source": [
    "$$ P(A) = \\frac{\\text{Success}}{\\text{Trial}}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a137a7b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def bernoulli(p, rng=rng):\n",
    "    return rng.random() < p\n",
    "\n",
    "def estimate(trials, fn):\n",
    "    \"\"\"Return running estimate of P(A) over time\"\"\"\n",
    "    x = np.fromiter((fn() for _ in range(trials)), dtype=np.int8, count=trials)\n",
    "    return np.cumsum(x)/np.arange(1,trials+1)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1334e105",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16276 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Example 1: coin flip as Bernoulli trails\n",
    "p = 0.5 # fair coin \n",
    "N = 5000\n",
    "\n",
    "# running estimate of P(A) over time; A=success\n",
    "estimation = estimate(N, lambda: bernoulli(p,rng))\n",
    "plt.plot(estimation,'.k')\n",
    "plt.axhline(p,color='red')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b8d42630",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x113238ec0>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15764 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.fromiter((bernoulli(p,rng) for _ in range(N)), dtype=np.int8, count=N)\n",
    "proba = np.cumsum(x)/np.arange(1, N+1)\n",
    "plt.plot(proba, 'ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d420ae37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(0.351845), 0.35)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example 2: Law of total probability via simulation\n",
    "# Objective: Estimate the overall probability of drawing a red ball, when the ball is \n",
    "# drawn from a randomly selected urn\n",
    "#-=-=-=-=-=-=-=\n",
    "\n",
    "# Parameters:\n",
    "pA = 0.3 # probability that the experiment selects urn A\n",
    "pRedA = 0.7 # probability of red given urn A was selected P(R|A)\n",
    "pRedB = 0.2 # P(R|B)\n",
    "N = 200_000\n",
    "\n",
    "def bernoulli(p, rng=rng):\n",
    "    return rng.random() < p\n",
    "\n",
    "def drawRed():\n",
    "    chooseA = bernoulli(pA)\n",
    "    if chooseA:\n",
    "        return bernoulli(pRedA)\n",
    "    else:\n",
    "        return bernoulli(pRedB)\n",
    "    \n",
    "def estimate(trials, fn):\n",
    "    \"\"\"Return running estimate of P(A) over time\"\"\"\n",
    "    x = np.fromiter((fn() for _ in range(trials)), dtype=np.int8, count=trials)\n",
    "    return np.cumsum(x)/np.arange(1,trials+1)\n",
    "\n",
    "pHat = estimate(N,drawRed)\n",
    "pHat[-1]\n",
    "pExact = pA*pRedA+(1-pA)*pRedB\n",
    "pHat[-1], pExact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "93603e2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13100 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(pHat,'.k')\n",
    "plt.xlim([0,50])\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.14.2)",
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
