{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "7f067232-21bd-4089-b389-357ec49bc3dd"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np"
      ],
      "id": "b881b00c-d2dd-403a-81fb-116f37617404"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Our goals are to\n",
        "\n",
        "-   practice Python\n",
        "-   practice general expectations, namely\n",
        "\n",
        "$$ \\mathbb{E}[g(X)] = \\sum_{x \\in S} g(x) \\cdot f(x) $$\n",
        "\n",
        "All of this will only work for a specific class of distributions, namely\n",
        "ones which have a finite sample space. This won’t work for more general\n",
        "distributions, which we will get to in class soon."
      ],
      "id": "c8a06213-3da9-41b6-a14a-3304a0edd620"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
        "def general_expectation(s, f, g, **kwargs):\n",
        "    return np.sum(g(s, **kwargs) * f(s, **kwargs))"
      ],
      "id": "38eac9ac-5cf0-4b11-ac8b-f0c3b8b8bebf"
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {},
      "outputs": [],
      "source": [
        "def uniform_density(x, a = 1, b = 6, **kwargs):\n",
        "    xa = x < a\n",
        "    xb = x > b\n",
        "    idx = xa | xb\n",
        "    d = np.zeros_like(x)\n",
        "    d[~idx] = 1 / (b - a + 1)\n",
        "    return d"
      ],
      "id": "0af1cea7-efeb-4d11-8cd5-e1ce741c980f"
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {},
      "outputs": [],
      "source": [
        "def EX(x, f, **kwargs):\n",
        "    def m(x, **kwargs):\n",
        "        return x\n",
        "    return general_expectation(x, f, m, **kwargs)"
      ],
      "id": "3e27cfd8-aafd-462b-a030-928f31f8a8f2"
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "3430.0"
            ]
          }
        }
      ],
      "source": [
        "a = -13\n",
        "b = 6873\n",
        "SU = np.arange(a, b + 1, dtype = np.float64)\n",
        "EX(SU, uniform_density, a = a, b = b)"
      ],
      "id": "4020a44f-f14f-4cce-9cfd-c775f567049f"
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {},
      "outputs": [],
      "source": [
        "def VX(x, f, **kwargs):\n",
        "    def v(x, **kwargs):\n",
        "        return (x - EX(x, f, **kwargs)) ** 2\n",
        "    return general_expectation(x, f, v, **kwargs)"
      ],
      "id": "c7327c5c-9f1e-4d08-96a8-44ba6de74473"
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "3952564.0"
            ]
          }
        }
      ],
      "source": [
        "VX(SU, uniform_density, a = a, b = b)"
      ],
      "id": "97656d75-6757-4c07-a714-1c99b6a1a378"
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "3952564.0"
            ]
          }
        }
      ],
      "source": [
        "((b - a + 1) ** 2 - 1) / 12"
      ],
      "id": "262d64b5-b9c3-4c23-a516-96c5d8267603"
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "metadata": {},
      "outputs": [],
      "source": [
        "def PX(x, f, **kwargs):\n",
        "    def px(x, **kwargs):\n",
        "        return np.isin(x, kwargs[\"A\"]) # x == kwargs[\"A\"]\n",
        "    return general_expectation(x, f, px, **kwargs)"
      ],
      "id": "1152bbd9-56fd-42a5-8bb9-b657ef605105"
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "0.00043560331058516046"
            ]
          }
        }
      ],
      "source": [
        "PX(SU, uniform_density, a = a, b = b, A = np.array([1., 2, 3]))"
      ],
      "id": "deb487de-e152-4dcc-b3fd-13c1d6e04992"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3 (ipykernel)",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "name": "ipython",
        "version": "3"
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  }
}