Tags: #◇CSMODEL #◇Foundations for Inference #◇Computer Science #◇Data Science #◇Statistical Modeling #◇Python #◇Machine Learning
Module 07: Foundations for Inference
◇Key Concepts
1. Point Estimate vs. Interval Estimate
In statistical inference, our goal is to draw conclusions about unknown population parameters (such as the population mean or population proportion ) using data collected from a sample.
- Point Estimate: A single numerical value calculated from sample data used as the best single guess for an unknown population parameter.
- Examples: Sample mean estimates ; sample proportion estimates ; sample standard deviation estimates .
- Limitation: A point estimate almost never exactly equals the true population parameter due to sampling variation. It gives no information about how uncertain or precise the estimate is.
- Interval Estimate (Confidence Interval): A range of plausible values for a population parameter, computed with a specified level of confidence (e.g., 90%, 95%, 99%).
- General Form:
- Benefit: Accounts for sampling variability and quantifies uncertainty.
Sources of Error in Estimation
- Bias (Systematic Error): Introduced by flawed data collection or sampling design (e.g., non-response bias, convenience sampling). Bias shifts estimates away from the true value in one direction. Increasing sample size does NOT fix bias.
- Sampling Error (Random Variability): Natural variation that occurs because a sample contains only a subset of the population. Different random samples yield different point estimates. Sampling error decreases as sample size increases.
2. Sampling Distribution of the Sample Mean & Central Limit Theorem (CLT)
- Sampling Distribution: The probability distribution of a point estimate (e.g., sample mean ) obtained by drawing all possible random samples of size from a population.
- Central Limit Theorem (CLT):
- For independent random observations, as the sample size increases, the sampling distribution of the sample mean (and sample proportion ) approaches a Normal Distribution, regardless of the shape of the underlying population distribution.
- Rule of Thumb for Sample Size:
- If the population is already normally distributed, the sampling distribution of is normal for any sample size .
- If the population is non-normal (skewed, uniform, bimodal, etc.), the sampling distribution of is approximately normal provided .
- Conditions for CLT:
- Independence: Observations must be independent (e.g., random sampling / assignment). If sampling without replacement, sample size must be of the population.
- Sample Size / Normality: for non-normal populations, or population itself must be nearly normal.
3. Standard Error (SE)
The Standard Error measures the standard deviation (spread) of a statistic's sampling distribution. It quantifies how much a point estimate fluctuates from sample to sample.
- Standard Error of the Mean:
If the population standard deviation is unknown, we substitute the sample standard deviation :
- Standard Error of a Proportion:
- Key Takeaway: Standard error is inversely proportional to . To cut the standard error in half, you must quadruple the sample size ().
4. Confidence Intervals (CI) & Confidence Levels
A confidence interval provides a range within which we are confident the true population parameter lies.
- General Formula:
Critical Values () for Common Confidence Levels:
The critical value corresponds to the upper tail cutoff for a two-tailed confidence level of :
| Confidence Level () | Significance Level () | Tail Probability () | Critical Value () |
|---|---|---|---|
| 90% | 0.10 | 0.050 | 1.645 |
| 95% | 0.05 | 0.025 | 1.960 |
| 99% | 0.01 | 0.005 | 2.576 (or 2.58) |
Correct Interpretation of a Confidence Interval:
- Correct: "We are 95% confident that the true population mean falls within the interval ." (This means that if we repeat the sampling process 100 times and construct 100 CIs, approximately 95 of them will contain the true fixed parameter ).
- Incorrect: "There is a 95% probability that the true mean is inside ." (The true mean is a fixed constant, not a random variable. The specific interval either contains or it doesn't; the probability is 0 or 1).
5. Hypothesis Testing Framework
Hypothesis testing is a formal statistical procedure for making decisions using sample data.
Step-by-Step Procedure:
- State the Hypotheses:
- Null Hypothesis (): Skeptical claim, status quo, or statement of no effect/no difference. Assumed true until proven otherwise (e.g., or ).
- Alternative Hypothesis ( or ): Claim to be supported; represents a change, difference, or effect (e.g., two-sided , one-sided or ).
- Choose Significance Level ():
- Common thresholds: (default), , or . Represents maximum tolerable risk of a Type I error.
- Calculate Test Statistic & -value:
- Test Statistic (Z-score for mean when known or ):
- Test Statistic (Z-score for proportion):
- -value: The probability of obtaining a test statistic at least as extreme as the observed sample value, assuming is true.
- Test Statistic (Z-score for mean when known or ):
- Make Decision & Draw Conclusion:
- Rule:
- Never say "Accept ". We only fail to reject .
- Rule:
6. Type I and Type II Errors
Because hypothesis testing relies on sample data, errors can occur:
| Truth \ Decision | Do Not Reject (Fail to Reject) | Reject (In favor of ) |
|---|---|---|
| is True | Correct Decision () | Type I Error () — False Positive |
| is True ( False) | Type II Error () — False Negative | Correct Decision () — Power |
- Type I Error (): Rejecting when is actually true (False Positive).
- Example: Convicting an innocent person; declaring a drug effective when it is not.
- Probability (significance level).
- Type II Error (): Failing to reject when is actually false (False Negative).
- Example: Acquitting a guilty person; failing to detect an effective drug.
- Probability .
- Power (): The probability of correctly rejecting a false . Power increases with larger sample size , larger effect size, or higher significance level .
◇Python Snippets
1. Importing Libraries & Overview of scipy.stats
import numpy as np
import pandas as pd
from scipy import stats
# scipy.stats functions used for inference:
# stats.norm.ppf() -> Percent point function (Quantile / inverse CDF) for Z
# stats.norm.cdf() -> Cumulative distribution function for Z
# stats.t.ppf() -> Inverse CDF for Student's t-distribution
# stats.t.cdf() -> Cumulative distribution function for t
# stats.sem() -> Standard error of the mean
2. Computing Critical Values ( and )
# Finding z* for a given confidence level (Two-Tailed)
confidence_level = 0.95
alpha = 1 - confidence_level
# z* for 95% CI (alpha = 0.05) -> norm.ppf(1 - alpha/2) = norm.ppf(0.975)
z_star_95 = stats.norm.ppf(1 - alpha / 2)
print(f"95% z*: {z_star_95:.4f}") # Output: 1.95996
# z* for 90% and 99% CIs
z_star_90 = stats.norm.ppf(1 - (1 - 0.90) / 2)
z_star_99 = stats.norm.ppf(1 - (1 - 0.99) / 2)
print(f"90% z*: {z_star_90:.4f}, 99% z*: {z_star_99:.4f}")
# Output: 90% z*: 1.6449, 99% z*: 2.5758
# Finding t* for small sample sizes (e.g., n = 15, df = n - 1 = 14)
n = 15
df = n - 1
t_star_95 = stats.t.ppf(1 - alpha / 2, df=df)
print(f"95% t* (df=14): {t_star_95:.4f}") # Output: 2.1448
3. Computing Standard Error
data = np.array([102.3, 98.6, 104.1, 99.8, 101.5, 103.0, 97.9, 100.4])
# Method A: Using scipy.stats.sem (ddof=1 by default)
se_scipy = stats.sem(data)
# Method B: Manual calculation
sample_std = np.std(data, ddof=1)
n = len(data)
se_manual = sample_std / np.sqrt(n)
print(f"SE (scipy): {se_scipy:.4f}, SE (manual): {se_manual:.4f}")
4. Computing Confidence Intervals Manually in Python
# Sample data summary statistics
sample_mean = 94.48
sample_std = 15.20
n = 60
confidence_level = 0.95
# Step 1: Calculate alpha and critical value z*
alpha = 1 - confidence_level
z_star = stats.norm.ppf(1 - alpha / 2)
# Step 2: Compute Standard Error and Margin of Error
se = sample_std / np.sqrt(n)
margin_of_error = z_star * se
# Step 3: Compute CI bounds
ci_lower = sample_mean - margin_of_error
ci_upper = sample_mean + margin_of_error
print(f"Sample Mean: {sample_mean:.2f}")
print(f"Margin of Error: {margin_of_error:.2f}")
print(f"95% Confidence Interval: ({ci_lower:.2f}, {ci_upper:.2f})")
5. Calculating p-values for Z-tests
# Given sample statistics and null hypothesis value mu_0
sample_mean = 105.2
mu_0 = 100.0
sample_std = 12.5
n = 50
alpha = 0.05
# Compute Z test statistic
se = sample_std / np.sqrt(n)
z_stat = (sample_mean - mu_0) / se
# Compute p-value
# Two-tailed test: p-value = 2 * (1 - CDF(|Z|))
p_val_two_tailed = 2 * (1 - stats.norm.cdf(np.abs(z_stat)))
# Upper-tailed test (H_a: mu > mu_0): p-value = 1 - CDF(Z)
p_val_upper = 1 - stats.norm.cdf(z_stat)
print(f"Z-statistic: {z_stat:.4f}")
print(f"Two-tailed p-value: {p_val_two_tailed:.4f}")
# Decision rule
if p_val_two_tailed < alpha:
print("Decision: Reject H0 (Statistically Significant)")
else:
print("Decision: Fail to Reject H0")
◇Formulas & Key Equations
| Concept | Formula | Notes / Variable Definitions |
|---|---|---|
| Sample Mean | Point estimate for | |
| Sample Variance | Sample variance () | |
| SE (Sample Mean) | Standard error of the mean | |
| SE (Sample Proportion) | Standard error of proportion | |
| Margin of Error (ME) | comes from confidence level | |
| Confidence Interval | ||
| Critical Value | Two-tailed quantile function | |
| Z-statistic (Mean) | One-sample Z-test | |
| Z-statistic (Proportion) | Uses null proportion in SE | |
| Two-Tailed -value |
◇Common Exam Traps
[!CAUTION]
Trap 1: Misinterpreting Confidence Interval Probability
- Wrong: "There is a 95% probability that the true mean is between 45.2 and 52.8."
- Right: "We are 95% confident that the true population mean is between 45.2 and 52.8." (Once computed, the specific interval either contains or it doesn't).
[!WARNING]
Trap 2: Confusing Standard Deviation () with Standard Error ()
- Standard deviation measures the dispersion of individual data points around the mean.
- Standard error measures the dispersion of sample means around the true population mean. As sample size increases, remains steady, but shrinks!
[!IMPORTANT]
Trap 3: Believing CLT Requires a Normal Population
- CLT applies specifically when the population is non-normal, assuring us that the sampling distribution of becomes approximately normal if . If the population is already normal, the sample mean is normal for any .
[!NOTE]
Trap 4: One-Tailed vs. Two-Tailed Cutoffs
- For a 95% Confidence Interval (always two-sided), .
- Do not use
norm.ppf(0.95) = 1.645for a 95% two-sided confidence interval!norm.ppf(0.95)is used for a 90% CI or a 95% one-tailed test.
[!WARNING]
Trap 5: Using instead of in Hypothesis Test SE
- When constructing a confidence interval for a proportion, use .
- When performing a hypothesis test for a proportion , use because we assume is true when deriving the sampling distribution.
[!CAUTION]
Trap 6: Saying "Accept " or Confusing -value with
- Never conclude "Accept ". The formal outcome is "Fail to reject ".
- The -value is . It is NOT the probability that is true.
◇Practice Questions
Question 1 (Calculation: 95% Confidence Interval for Mean)
A random sample of battery lifetimes yields a sample mean of hours with a sample standard deviation of hours. Assuming the sample observations are independent, construct a 95% confidence interval for the population mean battery lifetime .
A) B) C) D)
Question 2 (Python Snippet: Finding and CI Output)
Consider the following Python code snippet:
import numpy as np
from scipy import stats
scores = np.array([78, 85, 92, 88, 74, 80, 95, 90, 82, 86])
n = len(scores)
sample_mean = np.mean(scores)
se = stats.sem(scores)
alpha = 0.10 # 90% confidence level
z_star = stats.norm.ppf(1 - alpha / 2)
me = z_star * se
ci = (sample_mean - me, sample_mean + me)
print(f"z_star: {z_star:.3f}, CI: ({ci[0]:.2f}, {ci[1]:.2f})")
What will be the printed output of this code? (Note: sample_mean = 84.90, sample_std = 6.6408, se = 2.1001).
A) z_star: 1.645, CI: (81.45, 88.35) B) z_star: 1.960, CI: (80.78, 89.02) C) z_star: 1.282, CI: (82.21, 87.59) D) z_star: 2.576, CI: (79.49, 90.31)
Question 3 (Conceptual: Central Limit Theorem)
A population of customer wait times at a bank has a heavily right-skewed distribution with population mean minutes and population standard deviation minutes. A data analyst draws a random sample of bank customers. Which of the following statements regarding the sampling distribution of the sample mean wait time is TRUE?
A) The sampling distribution of will be heavily right-skewed with a mean of 12.0 minutes and standard error of 8.0 minutes. B) The Central Limit Theorem does not apply because the underlying population is not normally distributed. C) The sampling distribution of will be approximately normal with a mean of 12.0 minutes and standard error of 1.0 minute. D) The sampling distribution of will be approximately normal with a mean of 12.0 minutes and standard error of 8.0 minutes.
Question 4 (Calculation & Hypothesis Test: One-Sample Z-Test for Proportion)
A tech survey of randomly selected computer science students reveals that students prefer working in Linux over Windows (). Test the null hypothesis (no preference majority) against at significance level .
Compute the test statistic and the -value, and state the conclusion.
A) , Reject ; there is statistically significant evidence of a preference. B) , Fail to reject ; there is insufficient evidence of a preference. C) , Reject ; there is statistically significant evidence of a preference. D) , Fail to reject ; there is insufficient evidence of a preference.
Question 5 (Python Snippet & Decision Rule / Error Types)
Consider the Python script below evaluating a hypothesis test:
from scipy import stats
# Observed Z statistic from a sample
z_stat = 2.15
alpha = 0.05
# Two-tailed p-value computation
p_val = 2 * (1 - stats.norm.cdf(abs(z_stat)))
print(f"p-value: {p_val:.4f}")
if p_val < alpha:
print("Result: Reject H0")
else:
print("Result: Fail to Reject H0")
If is actually true in reality for this population, what is the printed output and what type of decision error occurred?
A) p-value: 0.0316, Result: Reject H0; Type I Error (False Positive). B) p-value: 0.0158, Result: Reject H0; Type II Error (False Negative). C) p-value: 0.0316, Result: Fail to Reject H0; Type I Error (False Positive). D) p-value: 0.9842, Result: Fail to Reject H0; Type II Error (False Negative).
Answer Key
-
Q1: B — Step-by-step solution:
- Given: , , , Confidence Level .
- Standard Error: .
- Critical value for confidence level: .
- Margin of Error: .
- Confidence Interval bounds:
Hence, the CI is .
-
Q2: A — Step-by-step solution:
alpha = 0.10corresponds to a confidence level.z_star = stats.norm.ppf(1 - 0.10/2) = stats.norm.ppf(0.95) = 1.64485 \approx 1.645.sample_mean = 84.90,se = 2.1001.me = 1.64485 * 2.1001 = 3.4544.ci_lower = 84.90 - 3.45 = 81.45,ci_upper = 84.90 + 3.45 = 88.35.- Printed string format gives
z_star: 1.645, CI: (81.45, 88.35).
-
Q3: C — Step-by-step solution:
- According to the Central Limit Theorem, since , the sampling distribution of is approximately normal regardless of the population skewness.
- The mean of the sampling distribution equals the population mean: minutes.
- The standard error is minute.
-
Q4: A — Step-by-step solution:
- Hypotheses: vs .
- Sample proportion .
- Under , .
- Test statistic: .
- Two-tailed -value for :
- Decision: Since , we reject .
-
Q5: A — Step-by-step solution:
z_stat = 2.15.stats.norm.cdf(2.15) \approx 0.98422.1 - 0.98422 = 0.01578.p_val = 2 * 0.01578 = 0.03156 \approx 0.0316.- Since
0.0316 < 0.05, the conditionp_val < alphais True, so it printsResult: Reject H0. - Rejecting a true null hypothesis is by definition a Type I Error (False Positive).
◇References & Navigation
- Previous Topic: ◇EDA
- Next Topic: ◇Inference for Means
- Related Notes: ◇Inference for Means, ◇Inference for Categorical Data
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
What does the Central Limit Theorem (CLT) state?
A Type I error occurs when:
What is a p-value?
The standard error of the mean is calculated as:
A point estimate is: