Confidence Intervals for Two Normal PopulationsĀ¶

Here we assume that a random sample \((X_{11},\ldots,X_{1n})\) is drawn from a \(N(\mu_1, \sigma_1^2)\) distribution and a random sample \((X_{21},\ldots,X_{2n})\) is drawn from a \(N(\mu_2, \sigma_2^2)\) distribution.

Confidence Interval for \(\mu_1-\mu_2\) with known \(\sigma_1^2\) and \(\sigma_2^2\)Ā¶

A \(100(1-\alpha)\%\) confidence interval for \(\mu_1-\mu_2\) with known \(\sigma_1^2\) and \(\sigma_2^2\) is: \(\bigg[(\bar{X}_1-\bar{X}_2) - z_{(1-\alpha/2)}*\sqrt{ \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}}, (\bar{X}_1-\bar{X}_2) + z_{(1-\alpha/2)}*\sqrt{ \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}}\bigg]\).

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(X1, X2, n1, n2, sigma21, sigma22, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data:
        X1 = np.asarray(X1)
        X2 = np.asarray(X2)
        n1 = X1.shape[0]
        n2 = X2.shape[0]
        Xbar1 = np.mean(X1)
        Xbar2 = np.mean(X2)
    else:
        Xbar1 = X1
        Xbar2 = X2
    
    sigma1 = np.sqrt(sigma21)
    sigma2 = np.sqrt(sigma22)
    
    quantile = stats.norm.ppf(q = 1 - alpha / 2)
    
    lower = np.round((Xbar1-Xbar2) - quantile * np.sqrt((sigma1**2 / n1) + (sigma2**2/n2)),4)
    upper = np.round((Xbar1-Xbar2) + quantile * np.sqrt((sigma1**2 / n1) + (sigma2**2/n2)),4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03BC_1 -  \u03BC_2" % (confidence*100))

    return confidence_interval
X1 = np.array([32, 37, 35, 28, 41, 44, 35, 31, 34])
X2 = np.array([35, 31, 29, 25, 34, 40, 27, 32, 31])
CI_mean(X1, X2, n1 = 9, n2 = 9, sigma21 = 22, sigma22 = 22, confidence = 0.95, raw_data = True)
A 95 % Confidence Interval for Ī¼_1 -  Ī¼_2
(-0.667, 8.0003)

Confidence Interval for \(\mu_1-\mu_2\) with unknown but equal \(\sigma_1^2\) and \(\sigma_2^2\)Ā¶

A \(100(1-\alpha)\%\) confidence interval for \(\mu_1-\mu_2\) with unknown but equal \(\sigma_1^2\) and \(\sigma_2^2\) is:
\(\bigg[(\bar{X}_1-\bar{X}_2) - t_{(1-\alpha/2)}(n-1)*\sqrt{ \frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}, (\bar{X}_1-\bar{X}_2) + t_{(1-\alpha/2)}(n-1)*\sqrt{ \frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}\bigg]\).

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(X1, X2, n1, n2, s21, s22, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data:
        X1 = np.asarray(X1)
        X2 = np.asarray(X2)
        n1 = X1.shape[0]
        n2 = X2.shape[0]
        Xbar1 = np.mean(X1)
        Xbar2 = np.mean(X2)
        s21 = np.var(X1, ddof=1)
        s22 = np.var(X1, ddof=1)
    else:
        Xbar1 = X1
        Xbar2 = X2
    
    sp = np.sqrt(((n1-1)*s21 + (n2-1)*s22) / (n1+n2-2))
    
    quantile = stats.t.ppf(q = 1 - alpha / 2, df = (n1 + n2-2))
    
    lower = np.round((Xbar1-Xbar2) - quantile * sp*np.sqrt((1/ n1) + (1/n2)),4)
    upper = np.round((Xbar1-Xbar2) + quantile * sp*np.sqrt((1/ n1) + (1/n2)),4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03BC_1 -  \u03BC_2" % (confidence*100))

    return confidence_interval
CI_mean(X1 = 10250, X2 = 9670, n1 = 17, n2 = 15, s21 = 500**2, s22 = 400**2, confidence = 0.90, raw_data = False)
A 90 % Confidence Interval for Ī¼_1 -  Ī¼_2
(305.7888, 854.2112)

Confidence interval for the ratio of variancesĀ¶

A \(100(1-\alpha)\%\) confidence interval for \(\frac{\sigma_1^2}{\sigma_2^2}\) is: \(\bigg[\frac{\sigma_1^2/\sigma_2^2}{F_{(1-\alpha/2)}(n_1-1,n_2-1)}, \frac{\sigma_1^2/\sigma_2^2}{F_{(\alpha/2)}(n_1-1,n_2-1)}\bigg]\).

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(X1, X2, n1, n2, confidence = 0.95, raw_data = True):
   
    alpha = 1 - confidence
    
    if raw_data == True:
        X1 = np.asarray(X1)
        X2 = np.asarray(X2)
        n1 = X1.shape[0]
        n2 = X2.shape[0]
        s21 = np.std(X1, ddof = 1)
        s22 = np.std(X2, ddof = 1)
    else:
        s21 = X1
        s22 = X2
        
    quantile_l = stats.f.ppf(q = alpha / 2, dfn = (n1-1), dfd = (n2-2))
    quantile_u = stats.f.ppf(q = 1 - alpha / 2, dfn = (n1-1), dfd = (n2-2))
    
    lower = np.round((s22/s21)/quantile_u,4)
    upper = np.round((s22/s21)/quantile_l,4)
    
    confidence_interval = (lower, upper)
    
    print("A %.2d %% Confidence Interval for \u03C3_1^2 /\u03C3_2^2  " % (confidence*100))

    return confidence_interval
CI_var(X1 = 71, X2 = 52, n1 = 11, n2 = 14, confidence = 0.95, raw_data = False)
A 95 % Confidence Interval for Ļƒ_1^2 /Ļƒ_2^2  
(0.2171, 2.652)

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