Fixed Top Ad (Local Preview)800x50 • Slot 6608427872

Hypothesis Testing

1. Introduction to Hypothesis Testing

    1.1 🧪 What is Hypothesis Testing?
    1. 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.
    1.2 Example
    1. 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.
    1.3 Main Steps
    1. 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.

2. Null and Alternative Hypotheses

    2.1 🎯 Null Hypothesis
    1. 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
    2.2 Alternative Hypothesis
    1. 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
    2.3 Example Hypothesis Pair
    1. Question: Is the average revenue different from $5,000? Hypotheses: H₀: μ = 5000H₁: μ ≠ 5000 Here: μ = Population mean revenue

3. One-Tailed and Two-Tailed Tests

    3.1 ↔️ Two-Tailed Test
    1. A two-tailed test checks whether a parameter is different in either direction. H₀: μ = 5000H₁: μ ≠ 5000Example: Is average monthly revenue different from $5,000?
    3.2 ➡️ Right-Tailed Test
    1. 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?
    3.3 ⬅️ Left-Tailed Test
    1. 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.

4. Significance Level and p-Value

    4.1 ⚖️ What is the Significance Level?
    1. 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.01 For example, α = 0.05 means we are using a 5% significance level. The significance level represents the chosen maximum probability of making a Type I error.
    4.2 What is a p-Value?
    1. 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.
    4.3 Decision Rule
    1. 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.
    4.4 Example
    1. Suppose: α = 0.05p-value = 0.02 Since: 0.02 < 0.05 Decision: Reject H₀ Suppose instead: p-value = 0.18 Since: 0.18 ≥ 0.05 Decision: Fail to reject H₀
    4.5 Important p-Value Interpretations
    1. 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.

5. Test Statistic

    5.1 📏 What is a Test Statistic?
    1. A test statistic measures how far the sample result is from what the null hypothesis predicts. It usually compares: Observed difference with Expected sampling variationExamples - Z-statistic - t-statistic - Chi-square statistic - F-statistic. 📌 A larger absolute test statistic generally indicates stronger disagreement with the null hypothesis.

6. Z-Test

    6.1 📐 What is a Z-Test?
    1. 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 size The p-value is calculated using the standard Normal distribution.
    6.2 When is a Z-Test Appropriate?
    1. 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.
    6.3 Python Code
    1. Download heart_df_clean data. 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 150 import 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 years filter_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 females male_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₁")

7. t-Test

    7.1 📊 What is a t-Test?
    1. 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
    7.2 Is a t-Test Only for Small Samples?
    1. 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-test Sample size affects how closely the t-distribution resembles the Normal distribution.
    7.3 Types of t-Tests
    1. 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?
    7.4 Python Code
    1. Download heart_df_clean data. 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 ≠ 25 import 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 > 18 smoker_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 cholesterol from 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₁")
    7.5 Important Assumptions
    1. 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.

8. Chi-Square Test of Independence

    8.1 🔲 What is a Chi-Square Test?
    1. 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
    8.2 Hypotheses
    1. Question: Is stroke status associated with diabetes status? Hypotheses: H₀: Stroke status and diabetes status are independentH₁: Stroke status and diabetes status are associated
    8.3 Test Statistic
    1. 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.
    8.4 Important Assumptions
    1. 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.
    8.5 Python Code
    1. Download heart_df_clean data. 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 associated import 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₁")

9. Analysis of Variance

    9.1 📚 What is ANOVA?
    1. 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?
    9.2 Hypotheses
    1. 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.
    9.3 F-Statistic
    1. The ANOVA test statistic is: F = MS_between / MS_within where: MS_between = Variation between group meansMS_within = Variation within groups The p-value is calculated using the F-distribution.
    9.4 Important Assumptions
    1. One-way ANOVA generally assumes: - Numeric outcome variable - Independent observations - Approximately Normal residuals within groups - Similar group variances
    9.5 ANOVA Examples
    1. - 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?
    9.6 Python Code
    1. Download heart_df_clean data. 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 group import 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₁")

10. Parametric and Non-Parametric Tests

    10.1 🔄 What is a Parametric Test?
    1. Parametric tests make assumptions about the population distribution or model parameters. Examples: Z-test, t-test, ANOVA.
    10.2 What is a Non-Parametric Test?
    1. Non-parametric tests generally make fewer distributional assumptions and often use ranks. Possible alternatives include:
Possible Non-Parametric Alternatives
Parametric TestPossible Non-Parametric Alternative
Independent t-testMann–Whitney U test
Paired t-testWilcoxon signed-rank test
One-way ANOVAKruskal–Wallis test

11. Statistical and Practical Significance

    11.1 📌 Statistical Significance
    1. 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.
    11.2 Practical Significance
    1. 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.

12. Statistical Test Selection Guide

    12.1 🧭 Choosing a Test
    1. A guide for choosing the appropriate statistical test:
Choosing a Test
Statistical QuestionSuggested Test
Compare one sample mean with a known value when σ is knownOne-sample Z-test
Compare one sample mean with a known value when σ is unknownOne-sample t-test
Compare means of two independent groupsIndependent t-test
Compare two measurements from the same participantsPaired t-test
Compare means across three or more independent groupsOne-way ANOVA
Test association between two categorical variablesChi-square test of independence
    12.2 Test Selection Questions
    1. 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.

13. Type I and Type II Errors

    13.1 ⚠️ Type I Error
    1. 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.
    13.2 Type II Error
    1. 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.
    13.3 Decision Table
    1. A summary of the possible outcomes:
RealityReject H₀Fail to Reject H₀
H₀ is trueType I errorCorrect decision
H₀ is falseCorrect decisionType II error

14. Statistical Power

    14.1 💡 What is Statistical Power?
    1. 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.
    14.2 What Affects Power?
    1. 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.

15. Final Hypothesis-Testing Workflow

    15.1 ✅ Checklist
    1. 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.
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. Z-Test and T-test

    Question 1 of 3

      Load heart_diseases_cleaned dataset and test:
      1. Is average age of heart disease patients greater than 50 years?
      2. Is average cholesterol level of heart disease patients different from 200 mg/dL?
      3. Is average sysBP of heart disease patients lesser than 120 mmHg?
      4. Is average sysBP different between stroke vs non-stroke patients?
      5. Do people with diabetes have higher glucose levels than non-diabetics?
  2. Chi-Square Test

    Question 2 of 3

      Load heart_diseases_cleaned dataset and test:
      1. Association between heart stroke and diabetes.
      2. Association between heart stroke and gender.
      3. Association between heart stroke and smoking status.
      4. Association between diabetes and age group (child, adult, old).
      5. Association between smoking status and age group (child, adult, old).
  3. ANOVA

    Question 3 of 3

      Load heart_diseases_cleaned dataset and test:
      1. Is average sysBP different across age groups (child, adult, old)?
      2. Is average cigsPerDay different across exercise frequencies?
      3. Is average sysBP different across academic levels?

End of Statistics

Next module: Machine Learning

Ad PlaceholderSlot: 5413242224