Asymptotic Confidence IntervalsΒΆ

Asymptotic Confidence Interval for \(\mu\)ΒΆ

A \(100(1-\alpha)\%\) asymptotic confidence interval for \(\mu\) with a unknown \(\sigma^2\) is: \(\big[\bar{X} - z_{1-\alpha/2}*\frac{s}{\sqrt n}, \bar{X} + z_{1-\alpha/2}*\frac{s}{\sqrt n}\big]\).

Here is a simulation study example (code main source).

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm, expon

def CI(X, confidence=0.95):
    alpha = 1 - confidence
    X = np.asarray(X)
    n = X.shape[0]
    Xbar = np.mean(X)
    s = np.std(X) * np.sqrt(n / (n-1)) # when n is large this factor
                                       # have little effect
    quantile = norm.ppf(1 - alpha /  2)
    lower = Xbar - quantile * s / np.sqrt(n)
    upper = Xbar + quantile * s / np.sqrt(n)
    
    return lower, upper

myrv = expon(scale=4)
ninterval = 100
f = plt.figure(figsize=(10, 8))
ax = f.gca()
covered = 0
for i in range(ninterval):
    X = myrv.rvs(size=(100,))
    L, U = CI(X, confidence=0.9)
    cover = (L < myrv.mean()) * (U > myrv.mean())
    ax.plot([L, U], [i, i], color={True:'blue', False:'red'}[cover])
    ax.axvline(myrv.mean(), c='k', linestyle='--')
    covered += cover
ax.set_title('%d of %d cover the true mean' % (covered, ninterval))
Text(0.5, 1.0, '88 of 100 cover the true mean')
../../_images/ci_asym_5_1.png

Asymptotic Confidence Interval for Population Proportion pΒΆ

A \(100(1-\alpha)\%\) asymptotic confidence interval for p is: \(\big[\hat{p} - z_{1-\alpha/2}*\sqrt\frac{\hat{p}(1-\hat{p})}{n}, \bar{X} + z_{1-\alpha/2}*\frac{s}{\sqrt n}\big]\).

import statsmodels as sm
from statsmodels.stats.proportion import proportion_confint   
proportion_confint(count = 33, nobs = 78, alpha=0.05, method='normal')
(0.3134368253908742, 0.5327170207629719)

Session InfoΒΆ

import session_info
session_info.show()
Click to view session information
-----
matplotlib          3.5.2
numpy               1.22.4
scipy               1.8.1
session_info        1.0.0
statsmodels         0.13.2
-----
Click to view modules imported as dependencies
PIL                 9.1.1
asttokens           NA
backcall            0.2.0
beta_ufunc          NA
binom_ufunc         NA
cffi                1.15.0
colorama            0.4.4
cycler              0.10.0
cython_runtime      NA
dateutil            2.8.2
debugpy             1.6.0
decorator           5.1.1
defusedxml          0.7.1
entrypoints         0.4
executing           0.8.3
hypergeom_ufunc     NA
ipykernel           6.13.0
ipython_genutils    0.2.0
jedi                0.18.1
kiwisolver          1.4.2
matplotlib_inline   NA
mpl_toolkits        NA
nbinom_ufunc        NA
packaging           21.3
pandas              1.4.2
parso               0.8.3
pexpect             4.8.0
pickleshare         0.7.5
pkg_resources       NA
prompt_toolkit      3.0.29
psutil              5.9.1
ptyprocess          0.7.0
pure_eval           0.2.2
pydev_ipython       NA
pydevconsole        NA
pydevd              2.8.0
pydevd_file_utils   NA
pydevd_plugins      NA
pydevd_tracing      NA
pygments            2.12.0
pyparsing           3.0.9
pytz                2022.1
six                 1.16.0
sphinxcontrib       NA
stack_data          0.2.0
tornado             6.1
traitlets           5.2.1.post0
wcwidth             0.2.5
zmq                 23.0.0
-----
IPython             8.4.0
jupyter_client      7.3.1
jupyter_core        4.10.0
notebook            6.4.11
-----
Python 3.8.12 (default, May  4 2022, 08:13:04) [GCC 9.4.0]
Linux-5.13.0-1023-azure-x86_64-with-glibc2.2.5
-----
Session information updated at 2022-05-28 16:28