# Lecture_33
Author: Zheyuan Wu
Date: 2023-04-12

#### Zheyuan Wu

#### 2023-04-12

### Hypothesis test for Variance

Example: A company produces metal pipes of a standard length. Five years ago it tested its production quality and found that the lengths of the pipes produced were normally distributed with a standard deviation of 1.1 cm. They want to test whether they are still meeting this level of quality by testing a random sample of 30 pipes. They found the sample standard deviation for the 30 pipes was 1.2 cm. Use \(\alpha\) = 0.10.

Let \(X_1,...,X_n\) be iid normal with variance \(\sigma^2\). Let \(S^2\) denote the sample variance. (or a random sample of size n from normal.)

1. State the hypotheses

\[ H_0:\sigma^2=\sigma^2_0~~~H_0:\sigma^2=\sigma^2_0~~~H_0:\sigma^2=\sigma^2_0\\ H_a:\sigma^2>\sigma^2_0~~~H_a:\sigma^2<\sigma^2_0~~~H_a:\sigma^2\neq \sigma^2_0\\ \]

Example:

\(\sigma^2=\) variation of pipe length currently.

\[ H_0:\sigma^2=1.1^2\\ H_a:\sigma^2> 1.1^2(quality~gets~worse)~~\sigma^2<1.1^2(quality~gets~better)\\ H_a:\sigma^2\neq 1.1^2 \]

iid = same distribution and independent.

If the \(H_0\) hypothesis is true, we shall expect \(S^2\) should be closer to \(\sigma^2\), and \(S^2\over\sigma^2\), shall be close to 1.

2. Compute the test statistic

\[ \chi^2_{H_0}={(n-1)S^2\over \sigma^2_0} \]

Condition the population is normal

Example:

\[ \chi_{H_0}^2={(30-1)\times (1.2)^2\over (1.1)^2}=34.512 \]

3. Reach a conclusion

- If \(H_0\) is true, \(\chi_{H_0}^2\sim \chi^2_{n-1}\).

- We want to determine if the observed value of \(\chi_{H_0}^2\) is unusual for a \(\chi_{n-1}^2\) random variable.

  1. Rejection rule or region:

  \(H_a:\sigma^2>\sigma_0^2\)

  - Reject \(H_0\) if \(\chi_{X_0}^2\geq \chi_{n-1,\alpha}^2\)

  \(H_a:\sigma^2<\sigma_0^2\)

  - Reject \(H_0\) if \(\chi_{X_0}^2\leq \chi_{n-1,1-\alpha}^2\) Use 1-a because the chi-square distribution is not symmetric

  \(H_a:\sigma^2\neq \sigma^2_0\)

  - Reject \(H_0\) if \(\chi_{X_0}^2\geq \chi_{n-1,\alpha/2}^2\) or \(\chi_{X_0}^2\leq \chi_{n-1,1-\alpha/2}^2\)
  2. p value:

  \(H_a:\sigma^2>\sigma_0^2\)

  - p-value = \(P(\chi_{n-1,\alpha}^2\geq \chi_{H_0}^2)\)

  \(H_a:\sigma^2<\sigma_0^2\)

  - p-value = \(P(\chi_{n-1,1-\alpha}^2\leq \chi_{H_0}^2)\)

  \(H_a:\sigma^2\neq\sigma_0^2\)

  - p-value = \(min(2\times P(\chi_{n-1,\alpha/2}^2\geq \chi_{H_0}^2),2\times P(\chi_{n-1,1-\alpha/2}^2\leq \chi_{H_0}^2))\)

  Reject \(H_0\) if p-value \(\leq \alpha\) (the \(\alpha\) is the significance level of the test that controls the type I error)

Example:

\[ \alpha=0.1,\chi_{H_0}^2=34.512,n=30,df=29\\ H_0:\sigma^2=1.21,vs.H_a\sigma^2\neq 1.21 \]

Method 1: reject \(H_0\) if \(\chi_{H_0}^2>\chi_{n-1,\alpha/2}^2\) or \(\chi_{H_0}^2<\chi_{n-1,1-\alpha/2}^2\)

\(\chi_{n-1,\alpha/2}^2\) = 17.70837

    # yes, you are right that R would calculate the left tail, but chi square test would do the right tail. lol. 0.05 is 1-0.95
    qchisq(0.05,df=29)

    ## [1] 17.70837

\(\chi_{n-1,1-\alpha/2}^2\) = 42.55697

    qchisq(0.95,df=29)

    ## [1] 42.55697

Failed to reject \(H_0\) since \(17.708<\chi_{H_0}^2=34.512<42.557\)

Method 2: p-value=P(observing more extreme | \(H_0\))=\(2P(\chi^2\geq \chi_{H_0}^2)~or~2P(\chi^2 \leq \chi_{H_0}^2)\)

\[ P(\chi^2\geq34.512)=1-pchisq(34.512,df=29)=0.221\\ P(\chi^2\leq34.512)=1-pchisq(34.512,df=29)=0.779 \]

p-value \(=min\{2\times 0.221,2\times 0.779\}\) which is greater than \(\alpha\)

4. State the conclusion in the context of the problem

Example:

We don’t have sufficient evidence to conclude that the variance in pipe length is different from 1.21 \(cm^2\)

Notes:

1. To perform a hypothesis test, the investigator selects a value of $$.

\(\alpha\) is the probability of Type I error

2. To control $= $ the probability of a Type II error, the sample size can be increased.

power of test = \(1-\beta\) = P(reject \(H_0\) | \(H_0\) is false)

higher power is better. (increasing \(\alpha\) would reduce \(\beta\), thus increase the power), increase n increase the power.

### Confidence Interval and Hypothesis Tests

Confidence intervals can be used to perform two-sides hypothesis tests.

Suppose we want to test the following hypotheses using significance level \(\alpha=0.05\).

    $H_0:\mu=\mu_0$
    $H_a:\mu\neq \mu_0$

- A 95% confidence interval contains the plausible values of \(\mu\).

- If \(\mu_0\) is in the confidence interval, do not reject \(H_0\).

- If \(\mu_0\) is not the confidence interval, reject \(H_0\).

Example: Use a 90% confidence interval to test whether the variance in pipe length differs from \(1.1^2\) with \(\alpha =0.10\).

### Comparing Two Means

In this section, we will discuss comparing means for two populations.

- Let \(X_1,...,X_{n_1}\) be a simple random sample from a population with mean \(\mu_1\) and variance \(\sigma_1^2\).

- Let \(X_1,..,X_{n_2}\) be a simple random sample from a population with mean \(\mu_2\) and variance \(\sigma_2^2\).

- We will assume the two sided samples are independent.

- Our inference will be focused on \(\mu_1-\mu_2\).

#### Case 1: Equal Variances

- Assume \(\sigma_1^2=\sigma_2^2=\sigma^2\).

- A pooled estimator of the common variance \(\sigma^2\) is

\[ S^2_p={(n_1-1)S^2_1+(n_2-1)S_2^2\over n_1+n_2-2} \]

- If both population are normal, then

\[ {(\bar X_1-\bar X_2)-(\mu_1-\mu_2)\over \sqrt{S_p^2({1\over n_1}+{1\over n_2})}}\sim T_{n_1+n_2-2} \]

- In practice, our inference will hold if

1. the populations are not normal but \(n_1\geq 30\) and \(n_2\geq 30\)

2. \(S_1^2\) and \(s_2^2\) are “close enough”:

\[ {max\{S_1^2,S_2^2\}\over min\{S_1^2,S_2^2\}}<\begin{cases}5~if~n_1,n_2\approx 7\\3~if~n_1,n_2\approx15\\2~if~n_1,n_2\approx 30\\\end{cases} \]

Example: During the 2003 season, Major League Baseball took steps to speed up the play of baseball games in order to maintain fan interest. For a sample of 32 games played during the summer of 2002 and a sample of 35 games played during the summer of 2003, the sample mean duration of the games was computed. For games in 2002, the sample mean was 172 minutes with a standard deviation of 10.1 minutes. For games in 2003, the sample mean was 166 minutes with a standard deviation of 12.2 minutes.

Population 1:

Population 2:

Check assumptions: