{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a747c570",
   "metadata": {},
   "source": [
    "# Point estimates\n",
    "\n",
    "We are trying to reveal some parameter of unknown distribution that we sample from. \n",
    "In this problem we are sampling from distribution $U(0,\\theta)$. \n",
    "To imagine a story about this: we play a game where the amount of time we get to play is random, \n",
    "generated by a device set up to generate a uniform random variable from interval $[0,\\theta]$ for some \n",
    "value of $\\theta$. How to decide the value of $\\theta$ from several observations? \n",
    "\n",
    "A discrete version of this is the \"German tank problem\". \n",
    "https://en.wikipedia.org/wiki/German_tank_problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "22947b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import stats\n",
    "from math import sqrt\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "684b3c14",
   "metadata": {},
   "source": [
    "The next function generates num numbers from the distribution $U(0,secretmax)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f884412e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sampleX(num, secretmax):\n",
    "    \"\"\"\n",
    "        Returns a sample of num independent values from U(0,secretmax).\n",
    "    \"\"\"\n",
    "    return stats.uniform.rvs(loc=0,scale=secretmax,size=num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "434f722e",
   "metadata": {},
   "source": [
    "We present few variants of estimate. \n",
    "\n",
    "The first one is obtained by the moment method, the second by the maximum likelihood.\n",
    "\n",
    "(Can you find a better one? The best possible solution is not yet discovered.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "85c3a944",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_estimate(x):\n",
    "    return 2*x.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b67cdc81",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_estimate2(x):\n",
    "    return x.max()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5693cc6f",
   "metadata": {},
   "source": [
    "Now: how to test which estimate is better? \n",
    "\n",
    "We compute the bias and the mean squared error.\n",
    "\n",
    "Observe the results and think about what it means."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "6d4fec26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean squared error: 1133.8687645169293, Bias: 0.0503636089879933\n"
     ]
    }
   ],
   "source": [
    "secret  = 130\n",
    "num = 10**4\n",
    "L = []\n",
    "D = []\n",
    "for _ in range(num):\n",
    "    data = sampleX(5,secret)\n",
    "    D.append(data)\n",
    "    estimate = my_estimate(data)\n",
    "    L.append(estimate)\n",
    "\n",
    "    \n",
    "estimates = np.array(L)\n",
    "MSE = np.mean((estimates-secret)**2)\n",
    "  \n",
    "print(f\"Mean squared error: {MSE}, Bias: {estimates.mean()-secret}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ad5d7bd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean squared error: 805.6292811516064, Bias: -21.592652247581242\n"
     ]
    }
   ],
   "source": [
    "secret  = 130\n",
    "num = 10**4\n",
    "L = []\n",
    "D = []\n",
    "for _ in range(num):\n",
    "    data = sampleX(5,secret)\n",
    "    D.append(data)\n",
    "    estimate = my_estimate2(data)\n",
    "    L.append(estimate)\n",
    "\n",
    "    \n",
    "estimates = np.array(L)\n",
    "MSE = np.mean((estimates-secret)**2)\n",
    "  \n",
    "print(f\"Mean squared error: {MSE}, Bias: {estimates.mean()-secret}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "feee6bbe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
