Confidence Intervals for Single Normal PopulationĀ¶

Here we assume that a random sample \((X_1,\ldots,X_n)\) is drawn from a \(N(\mu, \sigma^2)\) distribution.

Confidence Interval for \(\mu\) with known \(\sigma^2\)Ā¶

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

Letā€™s solve the question we covered in the class (visit lecture notes for the problem definition).

import numpy as np
import scipy.stats as stats

def CI_mean(X, n, sigma, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data == True:
        X = np.asarray(X)
        n = X.shape[0]
        Xbar = np.mean(X)
    else:
        Xbar = X
        
    quantile = stats.norm.ppf(q = 1 - alpha / 2)
    
    lower = np.round(Xbar - quantile * sigma / np.sqrt(n),4)
    upper = np.round(Xbar + quantile * sigma / np.sqrt(n),4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03BC" % (confidence*100))

    return confidence_interval
CI_mean(X = 19.93, n = 9, sigma = 5, confidence = 0.95, raw_data = False)
A 95 % Confidence Interval for Ī¼
(16.6634, 23.1966)

Confidence Interval for \(\sigma^2\) with known \(\mu\)Ā¶

A \(100(1-\alpha)\%\) confidence interval for \(\sigma^2\) with a known \(\mu\) is: \(\big[\frac{\sum_{i=1}^{n}(X_i-\mu)^2}{\chi^2_{1-\alpha/2}(n)}, \frac{\sum_{i=1}^{n}(X_i-\mu)^2}{\chi^2_{\alpha/2}(n)}\big]\).

Letā€™s solve the question we covered in the class (visit lecture notes for the problem definition).

import numpy as np
import scipy.stats as stats

def CI_var(X, n, mu, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data == True:
        X = np.asarray(X)
        n = X.shape[0]
        SS = np.sum((X-mu)**2)
    else:
        SS = X
    
    quantile_l = stats.chi2.ppf(q = alpha / 2, df = n)
    quantile_u = stats.chi2.ppf(q = 1 - alpha / 2, df = n)
    
    lower = np.round(SS / quantile_u,4)
    upper = np.round(SS / quantile_l,4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03C3^2" % (confidence*100))

    return confidence_interval
CI_var(X = 88, n = 9, mu = 5, confidence = 0.95, raw_data = False)
A 95 % Confidence Interval for Ļƒ^2
(4.626, 32.5879)

Confidence Interval for \(\mu\) with unknown \(\sigma^2\)Ā¶

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

Letā€™s solve the question we covered in the class (visit lecture notes for the problem definition).

import numpy as np
import scipy.stats as stats

def CI_mean(X, n, sd, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data == True:
        X = np.asarray(X)
        n = X.shape[0]
        Xbar = np.mean(X)
        sd = np.std(X, ddof = 1)
    else:
        Xbar = X
        sd = sd
        
    quantile = stats.t.ppf(q = 1 - alpha / 2, df = (n-1))
    
    lower = np.round(Xbar - quantile * sd / np.sqrt(n),4)
    upper = np.round(Xbar + quantile * sd / np.sqrt(n),4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03BC" % (confidence*100))

    return confidence_interval
CI_mean(X = 583.64, n = 11, sd = 22.15, confidence = 0.95, raw_data = False)
A 95 % Confidence Interval for Ī¼
(568.7594, 598.5206)

Confidence Interval for \(\sigma^2\) with unknown \(\mu\)Ā¶

A \(100(1-\alpha)\%\) confidence interval for \(\sigma^2\) with a unknown \(\mu\) is: \(\big[\frac{\sum_{i=1}^{n}(X_i-\bar X)^2}{\chi^2_{1-\alpha/2}(n-1)}, \frac{\sum_{i=1}^{n}(X_i-\bar X)^2}{\chi^2_{\alpha/2}(n-1)}\big]\).

Letā€™s solve the question we covered in the class (visit lecture notes for the problem definition).

import numpy as np
import scipy.stats as stats

def CI_var(X, n, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data == True:
        X = np.asarray(X)
        n = X.shape[0]
        Xbar = np.mean(X)
        s  = np.std(X, ddof = 1)
        SS = s * (n-1)
    else:
        SS = X
    
    quantile_l = stats.chi2.ppf(q = alpha / 2, df = (n-1))
    quantile_u = stats.chi2.ppf(q = 1 - alpha / 2, df = (n-1))
    
    lower = np.round(SS / quantile_u,4)
    upper = np.round(SS / quantile_l,4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03C3^2" % (confidence*100))

    return confidence_interval
CI_var(X = 128.41, n = 13, confidence = 0.90, raw_data = False)
A 90 % Confidence Interval for Ļƒ^2
(6.1072, 24.5712)

Session InfoĀ¶

import session_info
session_info.show()
Click to view session information
-----
numpy               1.22.4
scipy               1.8.1
session_info        1.0.0
-----
Click to view modules imported as dependencies
asttokens           NA
backcall            0.2.0
beta_ufunc          NA
binom_ufunc         NA
colorama            0.4.4
cython_runtime      NA
dateutil            2.8.2
debugpy             1.6.0
decorator           5.1.1
entrypoints         0.4
executing           0.8.3
hypergeom_ufunc     NA
ipykernel           6.13.0
ipython_genutils    0.2.0
jedi                0.18.1
mpl_toolkits        NA
nbinom_ufunc        NA
packaging           21.3
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
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