Fixed Top Ad (Local Preview)800x50 • Slot 6608427872
Hypothesis Testing
1. Introduction to Hypothesis Testing
- Hypothesis testing is a statistical method used to evaluate a claim about a population using sample data. It helps us: - Test assumptions - Compare groups - Evaluate Relationships - Quantify Uncertainty - Make Evidence Based Decisions. 📌 Hypothesis testing asks whether the observed sample result provides enough evidence against a default population assumption.
- Suppose a company claims:
The average monthly store revenue is $5,000.We collect revenue data from a sample of stores and ask:Does the sample provide sufficient evidence that average revenue is different from $5,000?Hypothesis testing gives us a structured way to answer this question. - A hypothesis test generally follows these steps: ➜ Define the population question ➜ State the null and alternative hypotheses ➜ Choose a significance level ➜ Select an appropriate statistical test ➜ Check the test assumptions ➜ Calculate the test statistic and p-value ➜ Make a statistical decision ➜ Interpret the result in context. 📌 The significance level and hypotheses should be chosen before examining the test result.
1.1 🧪 What is Hypothesis Testing?
1.2 Example
1.3 Main Steps
2. Null and Alternative Hypotheses
- The null hypothesis, written as H₀, represents the default claim being tested.
It commonly states:
- No Difference
- No Association
- No Effect
- A population parameter equals a specified value.
Example:
H₀: Average monthly revenue = $5,000 - The alternative hypothesis, written as H₁ or Hₐ, represents the competing claim.
It may state:
- A difference exists
- An association exists
- An effect exists
- A parameter is greater than or less than a specified value.
Example:
H₁: Average monthly revenue ≠ $5,000 - Question:
Is the average revenue different from $5,000?Hypotheses:H₀: μ = 5000H₁: μ ≠ 5000Here:μ = Population mean revenue
2.1 🎯 Null Hypothesis
2.2 Alternative Hypothesis
2.3 Example Hypothesis Pair
3. One-Tailed and Two-Tailed Tests
- A two-tailed test checks whether a parameter is different in either direction.
H₀: μ = 5000H₁: μ ≠ 5000Example:Is average monthly revenue different from $5,000? - A right-tailed test checks whether a parameter is greater than a specified value.
H₀: μ ≤ 5000H₁: μ > 5000Example:Is average monthly revenue greater than $5,000? - A left-tailed test checks whether a parameter is less than a specified value.
H₀: μ ≥ 5000H₁: μ < 5000Example:Is average cholesterol below 150 mg/dL?📌 The direction of the alternative hypothesis determines whether the test is left-tailed, right-tailed, or two-tailed.
3.1 ↔️ Two-Tailed Test
3.2 ➡️ Right-Tailed Test
3.3 ⬅️ Left-Tailed Test
4. Significance Level and p-Value
- The significance level is written as:
α. It is the threshold used to determine whether the sample evidence is statistically significant. Common values include:α = 0.05α = 0.01For example,α = 0.05means we are using a 5% significance level. The significance level represents the chosen maximum probability of making a Type I error. - The p-value is the probability of obtaining a result at least as extreme as the observed result, assuming the null hypothesis is true. 📌 A smaller p-value means the observed result is less consistent with the null hypothesis.
- Compare the p-value with the significance level.
If p-value < α:
Reject H₀, The result is statistically significant. If p-value ≥ α:Fail to reject H₀, There is not enough evidence to reject the null hypothesis. - Suppose:
α = 0.05p-value = 0.02Since:0.02 < 0.05Decision:Reject H₀Suppose instead:p-value = 0.18Since:0.18 ≥ 0.05Decision:Fail to reject H₀ - A p-value does not tell us - The probability that H₀ is true - The probability that H₀ is false - The size of an effect - Whether a result is practically important - Whether the study was designed correctly. A high p-value does not prove that H₀ is true. It only means the sample does not provide sufficient evidence to reject it. Fail to reject should not be replaced with accept the null hypothesis.
4.1 ⚖️ What is the Significance Level?
4.2 What is a p-Value?
4.3 Decision Rule
4.4 Example
4.5 Important p-Value Interpretations
5. Test Statistic
- A test statistic measures how far the sample result is from what the null hypothesis predicts. It usually compares:
Observed differencewithExpected sampling variationExamples - Z-statistic - t-statistic - Chi-square statistic - F-statistic. 📌 A larger absolute test statistic generally indicates stronger disagreement with the null hypothesis.
5.1 📏 What is a Test Statistic?
6. Z-Test
- A Z-test can be used to test a claim about a population mean when the population standard deviation is known.
A one-sample Z-statistic is:
Z = (X̄ − μ₀) / (σ / √n)where:X̄ = Sample meanμ₀ = Mean stated by the null hypothesisσ = Population standard deviationn = Sample sizeThe p-value is calculated using the standard Normal distribution. - A one-sample mean Z-test is commonly used when ✅ The outcome is numeric ✅ The observations are independent ✅ The population standard deviation is known ✅ The sampling distribution of the mean is approximately Normal. A large sample can help the sampling distribution become approximately Normal, but having more than 30 observations is only a guideline, not a universal rule. 📌 When the population standard deviation is unknown, a t-test is generally used instead.
- Download
heart_df_cleandata. Load it as pandas dataframe and perform the test for statements as below: Is the average cholestrol different from 150 mg/dL?? # H₀ = Average Cholestrol is 150 # H₁ = Average Cholestrol is not 150import pandas as pdfrom statsmodels.stats.weightstats import ztestheart_df_clean = pd.read_csv("heart_df_clean.csv")cholesterol_data = heart_df_clean["cholesterol"]z_stat, p_value = ztest(cholesterol_data, value=150, alternative="two-sided")if p_value < 0.05:print("Reject H₀: Average cholesterol is not 150 mg/dL")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")Is the average age of stroke patients greater than 30 years? # H₀ = Average age <= 30 years # H₁ = Average age > 30 yearsfilter_mask = heart_df_clean["stroke"] == Trueage_data = heart_df_clean.loc[filter_mask, "age"]z_stat, p_value = ztest(age_data, value=30, alternative="larger")if p_value < 0.05:print("Reject H₀: Average age > 30 years")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")Do males have lower BMI than female? # H₀ = Average BMI of males >= Average BMI of females # H₁ = Average BMI of males < Average BMI of femalesmale_filter = heart_df_clean["gender"] == "Male"male_bmi = heart_df_clean.loc[male_filter, "BMI"]female_filter = heart_df_clean["gender"] == "Female"female_bmi = heart_df_clean.loc[female_filter, "BMI"]z_stat, p_value = ztest(male_bmi, female_bmi, alternative="smaller")if p_value < 0.05:print("Reject H₀: Males have lower BMI than females")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")
6.1 📐 What is a Z-Test?
6.2 When is a Z-Test Appropriate?
6.3 Python Code
7. t-Test
- A t-test is used to test claims about means when the population standard deviation is unknown. It uses the sample standard deviation and the t-distribution.
For a one-sample t-test:
t = (X̄ − μ₀) / (s / √n)where:X̄ = Sample meanμ₀ = Mean stated by H₀s = Sample standard deviationn = Sample size - No. A t-test can be used for both small and large samples when the population standard deviation is unknown.
The distinction is:
Known population standard deviation → Z-testUnknown population standard deviation → Usually t-testSample size affects how closely the t-distribution resembles the Normal distribution. - One-Sample t-Test: Compares one sample mean with a specified value. Example:
Is average revenue different from $5,000?Independent Two-Sample t-Test: Compares the means of two independent groups. Example:Is average BMI different between male and female participants?Paired t-Test: Compares two related measurements. Example:Is average blood pressure different before and after treatment? - Download
heart_df_cleandata. Load it as pandas dataframe and perform the t-test for statements as below assuming data is less than 30: Is the average BMI of stroke patients different from 25? # H₀ = Average BMI = 25 # H₁ = Average BMI ≠ 25import pandas as pdfrom scipy.stats import ttest_1sampheart_df_clean = pd.read_csv("heart_df_clean.csv")bmi_data = heart_df_clean["BMI"]t_stat, p_value = ttest_1samp(bmi_data, 25, alternative="two-sided")if p_value < 0.05:print("Reject H₀: Average BMI is different from 25")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")Is average age of smoker above 18? # H₀ = Average age of smoker <= 18 # H₁ = Average age of smoker > 18smoker_filter = heart_df_clean["currentSmoker"] == Trueage_data = heart_df_clean.loc[smoker_filter, "age"]t_stat, p_value = ttest_1samp(age_data, 18, alternative="larger")if p_value < 0.05:print("Reject H₀: Average age of smoker is above 18")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")Do non-smoker have lower cholesterol levels than smoker? # H₀ = non-smokers cholesterol >= smokers cholesterol # H₁ = non-smokers cholesterol < smokers cholesterolfrom scipy.stats import ttest_indno_smoker_mask = heart_df_clean["currentSmoker"] == Falsesmoker_filter = heart_df_clean["currentSmoker"] == Trueno_smoker_chol = heart_df_clean.loc[no_smoker_mask, "totChol"]smoker_chol = heart_df_clean.loc[smoker_filter, "totChol"]t_stat, p_value = ttest_ind(no_smoker_chol, smoker_chol, alternative="less")if p_value < 0.05:print("Reject H₀: Non-smokers have lower cholesterol levels")else:print("Fail to reject H₀: Not enough evidence to conclude H₁") - Depending on the type of t-test, assumptions may include: - Numeric Outcome Variable - Independent observations - Approximately Normal data or differences - No severe influential outliers - Similar group variances when using the equal-variance version When two independent groups may have unequal variances, Welch's t-test is commonly preferred.
7.1 📊 What is a t-Test?
7.2 Is a t-Test Only for Small Samples?
7.3 Types of t-Tests
7.4 Python Code
7.5 Important Assumptions
8. Chi-Square Test of Independence
- A Chi-square test of independence checks whether two categorical variables are associated. Examples: - Stroke Status and Diabetes Status - Stroke Status and Gender - Product Preference and Customer Region - Purchase Decision and Advertisement Type
- Question:
Is stroke status associated with diabetes status?Hypotheses:H₀: Stroke status and diabetes status are independentH₁: Stroke status and diabetes status are associated - The Chi-square statistic is:
χ² = Σ((O − E)² / E)where:O = Observed frequencyE = Expected frequency under H₀The p-value is calculated using the Chi-square distribution. - A Chi-square test of independence generally requires: - Categorical variables - Frequency counts - Independent observations - Adequate expected cell frequencies 📌 The test compares observed counts with the counts expected if the variables were independent.
- Download
heart_df_cleandata. Load it as pandas dataframe and perform the test for statements as below: Is stroke status associated with diabetes status? # H₀ = Stroke and diabetes are independent # H₁ = Stroke and diabetes are associatedimport pandas as pdfrom scipy.stats import chi2_contingencydf = pd.read_csv("heart_df_clean.csv")table = pd.crosstab(df["stroke"], df["diabetes"])chi2_stat, p_value, dof, expected = chi2_contingency(table)if p_value < 0.05:print("Reject H₀: Stroke and diabetes are associated")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")
8.1 🔲 What is a Chi-Square Test?
8.2 Hypotheses
8.3 Test Statistic
8.4 Important Assumptions
8.5 Python Code
9. Analysis of Variance
- ANOVA stands for Analysis of Variance. One-way ANOVA is used to compare the means of three or more independent groups.
Example:
Is average systolic blood pressure different across young, middle-aged, and older groups? - For three groups:
H₀: μ₁ = μ₂ = μ₃The alternative is:H₁: At least one population mean is different📌 ANOVA does not initially identify which group is different. It tests whether at least one difference exists. - The ANOVA test statistic is:
F = MS_between / MS_withinwhere:MS_between = Variation between group meansMS_within = Variation within groupsThe p-value is calculated using the F-distribution. - One-way ANOVA generally assumes: - Numeric outcome variable - Independent observations - Approximately Normal residuals within groups - Similar group variances
- - Is average systolic blood pressure different across age groups? - Is average glucose different across BMI categories? - Is average customer spending different across three store regions? - Are average examination scores different across four classes?
- Download
heart_df_cleandata. Load it as pandas dataframe and perform the test for statements as below: Is average sysBP different across age groups? # H₀ = sysBP is the same across all age groups # H₁ = sysBP is different across at least one age groupimport pandas as pdfrom scipy.stats import f_onewaydf = pd.read_csv("heart_df_clean.csv")child_bp = df[df["age_group"] == "child"]["sysBP"]adult_bp = df[df["age_group"] == "adult"]["sysBP"]old_bp = df[df["age_group"] == "old"]["sysBP"]f_stat, p_value = f_oneway(child_bp, adult_bp, old_bp)if p_value < 0.05:print("Reject H₀: sysBP is different across age groups")else:print("Fail to reject H₀: Not enough evidence to conclude H₁")
9.1 📚 What is ANOVA?
9.2 Hypotheses
9.3 F-Statistic
9.4 Important Assumptions
9.5 ANOVA Examples
9.6 Python Code
10. Parametric and Non-Parametric Tests
- Parametric tests make assumptions about the population distribution or model parameters.
Examples:
Z-test,t-test,ANOVA. - Non-parametric tests generally make fewer distributional assumptions and often use ranks. Possible alternatives include:
10.1 🔄 What is a Parametric Test?
10.2 What is a Non-Parametric Test?
Possible Non-Parametric Alternatives
| Parametric Test | Possible Non-Parametric Alternative |
|---|---|
| Independent t-test | Mann–Whitney U test |
| Paired t-test | Wilcoxon signed-rank test |
| One-way ANOVA | Kruskal–Wallis test |
11. Statistical and Practical Significance
- A result is statistically significant when:
p-value < αThis means the observed result would be unusual under the null hypothesis at the selected significance level. - Practical significance asks whether the observed effect is large enough to matter in the real world.
Example:
A new treatment reduces blood pressure by 0.2 mmHg.With a very large sample, this difference might be statistically significant but practically unimportant.
11.1 📌 Statistical Significance
11.2 Practical Significance
12. Statistical Test Selection Guide
- A guide for choosing the appropriate statistical test:
12.1 🧭 Choosing a Test
Choosing a Test
| Statistical Question | Suggested Test |
|---|---|
| Compare one sample mean with a known value when σ is known | One-sample Z-test |
| Compare one sample mean with a known value when σ is unknown | One-sample t-test |
| Compare means of two independent groups | Independent t-test |
| Compare two measurements from the same participants | Paired t-test |
| Compare means across three or more independent groups | One-way ANOVA |
| Test association between two categorical variables | Chi-square test of independence |
- Ask:
What type of outcome variable do I have?
Numeric or categorical?How many groups or measurements are involved?One, two, or three or more?Are the observations independent or paired?Different participants or the same participants?Is the population standard deviation known?Known σ or unknown σ?Are the test assumptions reasonably satisfied?Independence, distribution, variance, expected counts📌 Test selection depends on the variable type, study design, number of groups, and assumptions.
12.2 Test Selection Questions
13. Type I and Type II Errors
- A Type I error occurs when we reject a null hypothesis that is actually true. It is also called a
False positive. The probability of a Type I error is represented by:αExample:Concluding that a treatment works when it actually does not. - A Type II error occurs when we fail to reject a null hypothesis that is actually false. It is also called a
False negative. The probability of a Type II error is represented by:βExample:Failing to detect a treatment effect that actually exists. - A summary of the possible outcomes:
13.1 ⚠️ Type I Error
13.2 Type II Error
13.3 Decision Table
| Reality | Reject H₀ | Fail to Reject H₀ |
|---|---|---|
| H₀ is true | Type I error | Correct decision |
| H₀ is false | Correct decision | Type II error |
14. Statistical Power
- Statistical power is the probability of correctly rejecting the null hypothesis when the null hypothesis is false.
Power = 1 − βHigher power means a greater chance of detecting a real effect. - Power is affected by: - Sample Size - Effect Size - Data Variability - Significance Level - Study Design Larger samples and larger effects generally make real differences easier to detect.
14.1 💡 What is Statistical Power?
14.2 What Affects Power?
15. Final Hypothesis-Testing Workflow
- Before completing a hypothesis test, check: - What population question am I asking? - What are H₀ and H₁? - Is the alternative one-tailed or two-tailed? - What significance level will I use? - What type of variables do I have? - Are the groups independent or paired? - Which statistical test is appropriate? - Are the assumptions reasonably satisfied? - What are the test statistic and p-value? - Should I reject or fail to reject H₀? - What does the result mean in context? - Is the effect practically meaningful? 📌 A hypothesis test provides evidence for a decision. It does not prove a hypothesis with absolute certainty.
15.1 ✅ Checklist