{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
Confidence bounds for the mean of a bounded population: Binomial and Hypergeometric
\n", "\n", "## We will use basic combinatorics to find upper and lower confidence bounds for the mean of a bounded population.\n", "\n", "+ Elementary derivation for {0, 1} populations\n", "+ Obvious extension to 2-valued populations\n", "+ Thresholding for general bounded populations\n", "+ Trinomial and Multinomial bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparison with the normal approximation\n", "Let's start by constructing two-sided confidence intervals for the same {0, 1} cases in which the Normal approximation did so badly.\n", "\n", "To find two-sided intervals, we find lower and upper confidence bounds at half the value of $\\alpha$. This is equivalent to constructing acceptance regions that are \"balanced\" in the sense that, under the null hypothesis, the chance of rejecting the hypothesis because $X$ is too big is equal to the chance of rejecting the hypothesis because $X$ is too small.\n", "\n", "The code tells the story:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# This is the first cell with code: set up the Python environment\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import math\n", "import numpy as np\n", "import scipy as sp\n", "import scipy.stats\n", "from scipy.stats import binom\n", "import pandas as pd\n", "from ipywidgets import interact, interactive, fixed\n", "import ipywidgets as widgets\n", "from IPython.display import clear_output, display, HTML" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def binoLowerCL(n, x, cl = 0.975, inc=0.000001, p = None):\n", " \"Lower confidence level cl confidence interval for Binomial p, for x successes in n trials\"\n", " if p is None:\n", " p = float(x)/float(n)\n", " lo = 0.0\n", " if (x > 0):\n", " f = lambda q: cl - scipy.stats.binom.cdf(x-1, n, q)\n", " lo = sp.optimize.brentq(f, 0.0, p, xtol=inc)\n", " return lo\n", "\n", "def binoUpperCL(n, x, cl = 0.975, inc=0.000001, p = None):\n", " \"Upper confidence level cl confidence interval for Binomial p, for x successes in n trials\"\n", " if p is None:\n", " p = float(x)/float(n)\n", " hi = 1.0\n", " if (x < n):\n", " f = lambda q: scipy.stats.binom.cdf(x, n, q) - (1-cl)\n", " hi = sp.optimize.brentq(f, p, 1.0, xtol=inc) \n", " return hi" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | fraction of 1s | \n", "sample size | \n", "Student-t cov | \n", "Binom cov | \n", "Student-t len | \n", "Binom len | \n", "
---|---|---|---|---|---|---|
0 | \n", "0.001 | \n", "25.0 | \n", "2.8% | \n", "97.2% | \n", "0.0047 | \n", "0.1391 | \n", "
1 | \n", "0.001 | \n", "50.0 | \n", "5.1% | \n", "99.9% | \n", "0.0041 | \n", "0.0729 | \n", "
2 | \n", "0.001 | \n", "100.0 | \n", "9.5% | \n", "99.6% | \n", "0.0038 | \n", "0.038 | \n", "
3 | \n", "0.001 | \n", "400.0 | \n", "34.6% | \n", "99.0% | \n", "0.0037 | \n", "0.011 | \n", "
4 | \n", "0.010 | \n", "25.0 | \n", "20.8% | \n", "99.8% | \n", "0.0357 | \n", "0.1518 | \n", "
5 | \n", "0.010 | \n", "50.0 | \n", "40.7% | \n", "98.6% | \n", "0.0361 | \n", "0.0881 | \n", "
6 | \n", "0.010 | \n", "100.0 | \n", "60.9% | \n", "97.5% | \n", "0.0297 | \n", "0.052 | \n", "
7 | \n", "0.010 | \n", "400.0 | \n", "90.8% | \n", "95.8% | \n", "0.0188 | \n", "0.0221 | \n", "
8 | \n", "0.100 | \n", "25.0 | \n", "93.4% | \n", "99.3% | \n", "0.2377 | \n", "0.2631 | \n", "
9 | \n", "0.100 | \n", "50.0 | \n", "87.9% | \n", "96.3% | \n", "0.1675 | \n", "0.1812 | \n", "
10 | \n", "0.100 | \n", "100.0 | \n", "93.4% | \n", "95.2% | \n", "0.1183 | \n", "0.126 | \n", "
11 | \n", "0.100 | \n", "400.0 | \n", "93.9% | \n", "95.1% | \n", "0.0588 | \n", "0.061 | \n", "
\n", " Consider the second example in the Normal Approximation notebook, a mixture of a uniform and a point mass at zero.\n", " Suppose for the moment that we are interested in an upper confidence bound, e.g., an upper bound on the \n", " overstatement of a collection of accounts, for the purpose of establishing that a company's books are fairly \n", " represented.\n", "
\n", "\n", "\n", " We have an unknown population $\\{ x_j \\}_{j=1}^N$.\n", " We want an upper confidence bound for $\\mu_x \\equiv \\frac{1}{N} \\sum_{j=1}^N x_j$.\n", "
\n", " \n", "\n", " Suppose we have an a priori upper bound $u_j$ on the value $x_j$ of item $j$, $\\forall j$.\n", " For simplicity, let's take $u_j = 1$.\n", " Pick a threshold $t < 1$.\n", " Imagine a new population $\\{ y_j \\}_{j=1}^N$, where \n", "
\n", "\n", " $$y_j \\equiv \\left \\{ \\begin{array}{ll} t, & x_j \\le t \\cr 1, & x_j > t. \\end{array} \\right . $$\n", "\n", "\n", " Clearly $\\mu_y \\equiv \\frac{1}{N} \\sum_{j=1}^N y_j \\ge \\mu_x$, so an upper confidence bound for $\\mu_y$\n", " is also an upper confidence bound for $\\mu_x$.\n", " But we can find an upper confidence bound for $\\mu_y$ using a random sample with replacement and\n", " the general two-value transformation of the Binomial,\n", " as sketched above.\n", "
\n", "\n", "+ draw a random sample with replacement of size $n$ from the population\n", "+ let $X$ denote the number of items in the sample with value greater than $t$. Then $X$ has a Binomial distribution with parameters $n$ and $p$, where $p$ is the population fraction of items with value greater than $t$\n", "+ find an upper $1-\\alpha$ confidence bound $p^+$ for $p$ by inverting Binomial tests\n", "+ an upper $1-\\alpha$ confidence bound for $\\mu$ is $(1-p^+)t + p^+$.\n", "\n", "Let's see how this does compared to a one-sided Student-t interval. We will estimate the coverage for a variety of thresholds $t$. We will also estimate the average length of the intervals, to get an idea of the tradeoff between coverage and precision.\n", "\n", "It is perhaps discomfiting to have a tuning parameter $t$ in the method, but regardless of the choice of $t$, the intervals are guaranteed to be _conservative_ (true converage probability at least $1-\\alpha$.\n", "However, the expected _length_ of the interval depends on $t$ and on the underlying population of values. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | mass at 0 | \n", "sample size | \n", "Student-t cov | \n", "Bin t=0.2 cov | \n", "Bin t=0.1 cov | \n", "Bin t=0.01 cov | \n", "Bin t=0.001 cov | \n", "Student-t len | \n", "Bin t=0.2 len | \n", "Bin t=0.1 len | \n", "Bin t=0.01 len | \n", "Bin t=0.001 len | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0.900 | \n", "25.0 | \n", "89.5% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.316 | \n", "0.383 | \n", "0.318 | \n", "0.262 | \n", "0.257 | \n", "
1 | \n", "0.900 | \n", "50.0 | \n", "98.8% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.332 | \n", "0.337 | \n", "0.266 | \n", "0.204 | \n", "0.198 | \n", "
2 | \n", "0.900 | \n", "100.0 | \n", "99.9% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.334 | \n", "0.311 | \n", "0.236 | \n", "0.17 | \n", "0.163 | \n", "
3 | \n", "0.900 | \n", "400.0 | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.339 | \n", "0.285 | \n", "0.206 | \n", "0.136 | \n", "0.129 | \n", "
4 | \n", "0.990 | \n", "25.0 | \n", "22.9% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.047 | \n", "0.301 | \n", "0.214 | \n", "0.137 | \n", "0.129 | \n", "
5 | \n", "0.990 | \n", "50.0 | \n", "38.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.058 | \n", "0.257 | \n", "0.165 | \n", "0.083 | \n", "0.075 | \n", "
6 | \n", "0.990 | \n", "100.0 | \n", "60.2% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.069 | \n", "0.234 | \n", "0.139 | \n", "0.055 | \n", "0.046 | \n", "
7 | \n", "0.990 | \n", "400.0 | \n", "96.7% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.093 | \n", "0.216 | \n", "0.119 | \n", "0.032 | \n", "0.023 | \n", "
8 | \n", "0.999 | \n", "25.0 | \n", "2.3% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.004 | \n", "0.291 | \n", "0.203 | \n", "0.123 | \n", "0.115 | \n", "
9 | \n", "0.999 | \n", "50.0 | \n", "6.3% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.008 | \n", "0.248 | \n", "0.154 | \n", "0.07 | \n", "0.061 | \n", "
10 | \n", "0.999 | \n", "100.0 | \n", "9.8% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.008 | \n", "0.225 | \n", "0.128 | \n", "0.041 | \n", "0.032 | \n", "
11 | \n", "0.999 | \n", "400.0 | \n", "32.5% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "100.0% | \n", "0.015 | \n", "0.207 | \n", "0.108 | \n", "0.019 | \n", "0.01 | \n", "