{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problem 1\n",
    "\n",
    "The likelihood for a coin flipping experiment will be defined by the binomial\n",
    "\n",
    "\\begin{equation}\n",
    "\\mathcal{L}(p) = \\binom{n}{Y}p^Y(1-p)^{n-Y}\n",
    "\\end{equation}\n",
    "\n",
    "where\n",
    "  - $n$: total number of flips\n",
    "  - $Y$: Observed number of Heads\n",
    "  - $p$: Probability of each flip being Heads\n",
    "  \n",
    "The test statistic, $\\lambda$, is defined as\n",
    "\n",
    "\\begin{equation}\n",
    "\\lambda=\\frac{\\mathcal{L}(p_0)}{\\mathcal{L}(\\hat{p})}\n",
    "\\end{equation}\n",
    "\n",
    "Where $\\hat{p}$ is the value of $p$ that maximizes $\\mathcal{L}$. It can be simply found by setting $\\frac{d}{dp}\\mathcal{L}(p)|_{p=\\hat{p}}=0$ that $\\hat{p}=\\frac{Y}{n}$. From this it follows that\n",
    "\n",
    "\\begin{equation}\n",
    "\\lambda=\\frac{1}{2^n}\\left(\\frac{n}{Y}\\right)^Y \\left(\\frac{n}{n-Y}\\right)^{n-Y}\n",
    "\\end{equation}\n",
    "\n",
    "The likelihood ratio test then is given (for some $\\alpha$), by\n",
    "\\begin{align}\n",
    "\\lambda &< k_\\alpha \\\\\n",
    "\\frac{1}{2^n}\\left(\\frac{n}{Y}\\right)^Y \\left(\\frac{n}{n-Y}\\right)^{n-Y} &< k_\\alpha\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import binom\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.rcParams['figure.dpi']=150"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3.6/site-packages/ipykernel/__main__.py:3: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  app.launch_new_instance()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'$\\\\lambda$')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAycAAAIlCAYAAADPFocWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAXEQAAFxEByibzPwAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xu0rlddH/rvrwSSJrEGSZQ4AgQ3\nkEIQDAECKRdBvBEYBsUGoQcTqaNWQGoATwoSE0Ih2hqhZGBbNEJPC6fKLUoEoVgOJKKAm5N4gpgQ\njSTclCCQe7j8zh/vs+nKYq293nXba757fz5jvGPmfebzzvVbM3Ovvb/ruVV3BwAAYKf9o50uAAAA\nIBFOAACAQQgnAADAEIQTAABgCMIJAAAwBOEEAAAYgnACAAAMQTgBAACGIJwAAABDEE4AAIAhCCcA\nAMAQhBMAAGAIwgkAADAE4QQAABiCcAIAAAxBOAEAAIZw0E4XcKCoqs8lOTTJdTtdCwAAbKF7Jbml\nu++52YGqu7egHtZSVV85+OCDv23Xrl07XQoAAGyZa665JrfffvuN3f1PNjuWIyf7znW7du160JVX\nXrnTdQAAwJY5/vjj8/GPf3xLzg5yzQkAADAE4QQAABiCcAIAAAxBOAEAAIYgnAAAAEMQTgAAgCEI\nJwAAwBCEEwAAYAjCCQAAMAThBAAAGIJwAgAADEE4AQAAhrCw4aSqTqyqs6rqbVX16arqqrptE+Md\nUVWvrqq/rarbp/Y1VXXEVtYNAACs7KCdLmATXpbkx7ZioKq6R5IPJbl/kr9O8o4kxyf5hSRPrqpH\ndfcNW/G1AACAlS3skZPMwsTLkzw1yT03OdZvZBZM3pbkuO4+rbsfnOS1Se6X5IJNjg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AQAA\nhiCcAAAAQxBOAACAIQgnAADAEIQTAABgCMIJAAAwhP8fnrDiOGRbQZoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f066092a940>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Nflips = 36\n",
    "def lambda_(n, Y):\n",
    "    return 2.0**-n * (n/Y)**Y * (n/(n-Y))**(n-Y)\n",
    "\n",
    "xs = np.array(range(Nflips+1))\n",
    "\n",
    "plt.bar(xs, lambda_(Nflips, xs))\n",
    "plt.xlabel('Y')\n",
    "plt.ylabel(r'$\\lambda$')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**a)** We have as a given that the exclusion region is $|Y-18| \\ge 4$. This corresponds to $k_\\alpha=0.4080...$, so now let's find the $\\alpha$ value by running some MC experiments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha = 0.132401\n"
     ]
    }
   ],
   "source": [
    "k = lambda_(36, 14)\n",
    "Nexp = 1000000\n",
    "\n",
    "Ys = binom.rvs(Nflips, 0.5, size=Nexp)\n",
    "lambdas = lambda_(Nflips, Ys)\n",
    "\n",
    "Npass = np.sum(lambdas < k)\n",
    "print(f'alpha = {Npass/Nexp}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, for the given acceptance criterion, $\\alpha\\approx 0.13$.\n",
    "\n",
    "----\n",
    "**b)** Now, suppose that $p=0.7$. What is the value of $beta$?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "beta = 0.162632\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3.6/site-packages/ipykernel/__main__.py:3: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  app.launch_new_instance()\n"
     ]
    }
   ],
   "source": [
    "Ys = binom.rvs(Nflips, 0.7, size=Nexp)\n",
    "lambdas = lambda_(Nflips, Ys)\n",
    "\n",
    "Npass = np.sum(lambdas >= k)\n",
    "print(f'beta = {Npass/Nexp}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, for the chosen value of $p$, $\\beta\\approx0.16$.\n",
    "\n",
    "---\n",
    "**c)** False, it is the probability of our test rejecting $H_0$ when it is true.\n",
    "\n",
    "**d)** False, it is the probability of our test failing to reject $H_0$ when $H_a(p=0.7)$ is true.\n",
    "\n",
    "**e)** Correct, It was assumed that $H_a(p=0.7)$ was true.\n",
    "\n",
    "**f)** True, since the alternative hypotheses is more similar to the null hypotheses, they are more difficult to distinguish, which will lead to a larger $\\beta$ value.\n",
    "\n",
    "**g)** False, $\\alpha$ is the probably rejecting $H_0$, when it is true.\n",
    "\n",
    "**h)** True, the rejection region growing from $|y-18|\\ge4$ to $|y-18|\\ge2$ would lead to more incorrect rejections.\n",
    "\n",
    "**i)** True.\n",
    "\n",
    "**j)** False, it would be smaller because a less stringent rejection criteria would make it easier to reject $H_0$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
