{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "0ab0bb60-e647-48a5-9cb8-645d706dae28"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import scipy as sp"
      ],
      "id": "3649f334-3453-4afe-acb9-655f8d04cd4a"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "X = sp.stats.expon(scale = 1_000)"
      ],
      "id": "4a00be28-7974-4746-bb90-80e00c1f9980"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "array([ 611.94994999,   32.73060543,  260.90819498,  973.65795288,\n",
              "       1632.10218268, 4381.7841653 ,  835.46812813,  528.23626679,\n",
              "        254.15655304,  441.6069858 , 2919.16986341, 2251.14528326,\n",
              "        235.38532241,  162.70802574,  444.60070433,  557.57739079,\n",
              "         76.06937215,  154.1181732 ,   17.14743599,  876.32466646])"
            ]
          }
        }
      ],
      "source": [
        "N = 20\n",
        "data = X.rvs(size = N)\n",
        "data"
      ],
      "id": "3cd8b701-4239-45f8-ba99-5ba1ae42071c"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
        "mhat = np.mean(data)"
      ],
      "id": "fecfe202-64af-48c8-a378-cd6558d61073"
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(882.3423611380082)"
            ]
          }
        }
      ],
      "source": [
        "mhat"
      ],
      "id": "ce6b1479-ef6a-42ad-8a6b-d0094649b4be"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [],
      "source": [
        "T = sp.stats.t(df = N - 1) # df = degrees of freedom"
      ],
      "id": "75679e47-990f-41a2-903e-450a39c85d5b"
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [],
      "source": [
        "t = T.ppf(0.975)"
      ],
      "id": "ffca6b4b-bdac-4a2e-8863-aba5c4dd6163"
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = np.std(data)"
      ],
      "id": "5756f7bc-0b71-4719-8615-8583549035b2"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(1393.9459800414802)"
            ]
          }
        }
      ],
      "source": [
        "mhat + t * s / np.sqrt(N)"
      ],
      "id": "947c8c5d-b599-4cb9-90b5-1b0719a4695a"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(370.73874223453623)"
            ]
          }
        }
      ],
      "source": [
        "mhat - t * s / np.sqrt(N)"
      ],
      "id": "8c4493d1-9ffc-49cc-af6c-f9588feada47"
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "453.9537556994002 850.4374755559503\n",
            "472.78925179802263 969.2202032215766\n",
            "401.3036839198024 922.9629909214336\n",
            "497.56431925865024 968.0345750398307\n",
            "345.2210743336254 795.058582518935\n",
            "524.2389366909345 927.9887092515028\n",
            "419.0184087085173 946.1539418458832\n",
            "358.94499283345345 939.7740992441109\n",
            "439.35808400766615 939.9633577953923\n",
            "436.5919885338835 988.4433657061343\n",
            "425.4287665070773 979.3029945668578\n",
            "416.87828016667794 999.6568330626903\n",
            "1008.0904235724335 1617.9305335847307\n",
            "390.55419146578095 932.9463894164827\n",
            "312.918882779649 971.5254379461342\n",
            "354.1858152185465 710.0067431749425\n",
            "283.3343873631666 811.1640287979503\n",
            "257.1346110475754 795.2412205752319\n",
            "395.5975749528006 913.6656017393395\n",
            "447.5933094835603 859.0398166196821\n",
            "342.4168714980951 810.1722189496563\n",
            "381.0829847389331 958.4878554860232\n",
            "430.53656333175303 953.1713122605302\n",
            "431.3415387888875 992.1820341479252\n",
            "1022.2790818797441 1684.2586352978108\n",
            "301.1602963763309 988.2192654477269\n",
            "1084.9189160480055 2325.7594694117774\n",
            "390.74720430112006 935.6306460156641\n",
            "437.11023062346624 919.504289684404"
          ]
        }
      ],
      "source": [
        "R = 300\n",
        "b = np.zeros(R)\n",
        "for r in range(R):\n",
        "    data = X.rvs(size = N)\n",
        "    mhat = np.mean(data)\n",
        "    s = np.std(data)\n",
        "    ub = mhat + t * s / np.sqrt(N)\n",
        "    lb = mhat - t * s / np.sqrt(N)\n",
        "    if lb <= X.mean() <= ub:\n",
        "        b[r] = 1\n",
        "    else:\n",
        "        print(lb, ub)"
      ],
      "id": "e21b8267-1916-4b91-82af-6641b28fabe6"
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(0.9129333333333334)"
            ]
          }
        }
      ],
      "source": [
        "np.mean(b)"
      ],
      "id": "fa98a080-b1a9-419b-acdf-91cd222d6189"
    }
  ],
  "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"
    }
  }
}