MediumProbability and Statistics

One-Sample t-Test

Probability and Statistics

Medium

Problem

Compute the one-sample t-statistic for observations x_1,\ldots,x_n and hypothesized mean \mu_0. First compute the sample standard deviation:

s = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}(x_i-\bar{x})^2}

Then compute:

t = \frac{\bar{x}-\mu_0}{s/\sqrt{n}}

Here, \bar{x} is the sample mean. If s=0, return zero when \bar{x}=\mu_0 and signed infinity otherwise. Return the statistic as a Python float.

Theory

The one-sample t-test determines whether the mean of a sample is significantly different from a known or hypothesized population mean.

It answers the question: "Is the observed sample mean different from what we would expect under the null hypothesis?"

Developed by William Sealy Gosset under the pseudonym "Student" in 1908.


When to Use the One-Sample t-Test

Use this test when:

Examples:


The Hypotheses

Null hypothesis (H_0):

The population mean equals the hypothesized value.

H_0: \mu = \mu_0

Alternative hypothesis (H_1):

Two-tailed: H_1: \mu \neq \mu_0 (mean is different)

Left-tailed: H_1: \mu < \mu_0 (mean is less than)

Right-tailed: H_1: \mu > \mu_0 (mean is greater than)


The t-Statistic

The test statistic measures how many standard errors the sample mean is from the hypothesized mean:

t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

where:


Understanding the t-Statistic

Large |t|: Sample mean is far from \mu_0 in terms of standard errors. Evidence against H_0.

Small |t|: Sample mean is close to \mu_0. Consistent with H_0.

Sign of t:


The t-Distribution

Under H_0, the t-statistic follows a t-distribution with n-1 degrees of freedom.

Properties of t-distribution:

Why heavier tails?

We estimate \sigma with s, introducing additional uncertainty. Heavier tails account for this.


Degrees of Freedom

df = n - 1

We lose one degree of freedom because we estimate the mean from the data.

Effect of df:


Step-by-Step Procedure

Step 1: State hypotheses (H_0 and H_1)

Step 2: Choose significance level \alpha (commonly 0.05)

Step 3: Calculate sample mean \bar{x} and standard deviation s

Step 4: Compute the t-statistic

Step 5: Determine p-value or critical value

Step 6: Make decision:


Worked Example

Research question: A company claims batteries last 500 hours on average. A sample of 25 batteries has mean life 490 hours with standard deviation 30 hours. Is the mean different from 500?

Step 1: Hypotheses

H_0: \mu = 500

H_1: \mu \neq 500 (two-tailed)

Step 2: Significance level

\alpha = 0.05


Step 3: Calculate t-statistic

\bar{x} = 490, \mu_0 = 500, s = 30, n = 25

t = \frac{490 - 500}{30 / \sqrt{25}} = \frac{-10}{30/5} = \frac{-10}{6} = -1.67

Step 4: Degrees of freedom

df = 25 - 1 = 24

Step 5: Critical value and p-value

For two-tailed test at \alpha = 0.05 with df = 24:

t_{critical} = \pm 2.064

p-value \approx 0.108 (from t-distribution table or calculator)

Step 6: Decision

|t| = 1.67 < 2.064 and p-value = 0.108 > 0.05

Fail to reject H_0. Insufficient evidence that mean battery life differs from 500 hours.


Critical Values

Common critical values for two-tailed tests at \alpha = 0.05:

For one-tailed tests, use \alpha instead of \alpha/2.


P-Value Interpretation

The p-value is the probability of observing a t-statistic at least as extreme as the calculated value, assuming H_0 is true.

Two-tailed test:

\text{p-value} = 2 \times P(T > |t|)

One-tailed test (right):

\text{p-value} = P(T > t)

One-tailed test (left):

\text{p-value} = P(T < t)


Confidence Interval Approach

A (1-\alpha) \times 100\% confidence interval for \mu:

\bar{x} \pm t_{\alpha/2, n-1} \times \frac{s}{\sqrt{n}}

If \mu_0 is NOT in the interval, reject H_0 at level \alpha.

Example: For the battery data:

CI = 490 \pm 2.064 \times \frac{30}{5} = 490 \pm 12.38 = [477.62, 502.38]

Since 500 is IN the interval, we fail to reject H_0 (consistent with our earlier conclusion).


Assumptions

1. Random sampling

Observations are randomly selected from the population.

2. Independence

Observations are independent of each other.

3. Normality

The population is normally distributed, OR the sample size is large (n \geq 30, by Central Limit Theorem).

4. No extreme outliers

Outliers can distort the mean and inflate standard deviation.


Checking Normality

Visual methods:

Formal tests:

Rule of thumb:


Effect Size: Cohen's d

The t-statistic depends on sample size. For effect size, use Cohen's d:

d = \frac{\bar{x} - \mu_0}{s}

Interpretation:

Example: d = (490 - 500)/30 = -0.33 (small-to-medium effect)


Power of the Test

Power = probability of correctly rejecting H_0 when H_1 is true.

\text{Power} = 1 - \beta

where \beta is the probability of Type II error (false negative).

Factors affecting power:


Sample Size Determination

To achieve desired power for detecting effect size d:

n \approx \frac{2(z_{\alpha/2} + z_{\beta})^2}{d^2}

For 80% power (z_{\beta} = 0.84) at \alpha = 0.05 (z_{\alpha/2} = 1.96):

n \approx \frac{2(1.96 + 0.84)^2}{d^2} = \frac{15.68}{d^2}

For d = 0.5: n \approx 63


One-Sample t-Test vs Z-Test

Z-test:

t-test:

When n is large, both give nearly identical results.


Relationship to Confidence Intervals

There is a direct correspondence:

This provides two equivalent ways to test hypotheses.


Common Mistakes

1. Using z-test when \sigma is unknown

Always use t-test when estimating \sigma from data.

2. Ignoring assumptions

Non-normality with small samples invalidates the test.

3. Confusing statistical and practical significance

A small difference can be statistically significant with large n.

4. Multiple comparisons

Testing many hypotheses inflates Type I error rate.


Alternatives When Assumptions Fail

Non-normal data with small samples:

Outliers present:

Very small samples (n < 10):


Applications in Machine Learning

A/B testing:

Model evaluation:

Feature analysis:

Examples

Example 1

Input
x = [2.1, 2.4, 1.9, 2.6, 2.0], mu0 = 2.0
Output
1.53393
Explanation
The sample mean is above the hypothesized mean by about 1.53 standard errors.

Example 2

Input
x = [3.0, 5.0], mu0 = 4.0
Output
0.0

Example 3

Input
x = [1.0, 1.5, 2.0], mu0 = 3.0
Output
-5.196152

Hints

  1. Compute centered = x - np.mean(x) before the corrected variance.
  2. The standard error is sample_std / np.sqrt(x.size).

Requirements

Constraints

Starter Code

import numpy as np

def t_test_one_sample(x: list, mu0: float) -> float:
    """
    Returns the t-statistic as a float.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basic positive t-statisticpublic
Two samples - zero tpublic
Negative t-statisticpublic