Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Science using Python/Common Hypothesis Tests.ipynb
3074 views
Kernel: Python 3 (ipykernel)

Common Hypothesis Tests:

Hypothesis Testing:

  • Definition: Hypothesis testing is a statistical method used to make inferences about a population based on sample data.

  • Purpose: It helps determine whether there is enough evidence to reject a null hypothesis in favor of an alternative hypothesis.

Steps:

  • Formulate hypotheses: Null hypothesis (H0) assumes no effect or no difference, while the alternative hypothesis (H1) assumes an effect or difference.

  • Select a significance level (α): This represents the probability of rejecting the null hypothesis when it's actually true.

  • Collect and analyze data: Use sample data to compute a test statistic.

  • Make a decision: Compare the test statistic to a critical value or p-value to determine whether to reject the null hypothesis.

  • Draw conclusions: Based on the decision, either reject or fail to reject the null hypothesis and interpret the results accordingly.

In hypothesis testing, the p-value represents the probability of observing a test statistic as extreme as, or more extreme than, the one actually observed under the assumption that the null hypothesis is true. In other words, it indicates the probability of obtaining the observed data if the null hypothesis is correct.

The significance of the p-value lies in its role in decision-making during hypothesis testing:

Comparing with significance level (α): Typically, a significance level (α) is chosen before conducting the test. Common values for α are 0.05 or 0.01. If the p-value is less than or equal to the significance level (p ≤ α), then we reject the null hypothesis. This implies that the observed data is considered statistically significant, suggesting that the null hypothesis is unlikely to be true.

  • Interpretation: If the p-value is very small (e.g., p < 0.05), it suggests strong evidence against the null hypothesis, indicating that the observed effect is unlikely to be due to random chance alone. If the p-value is larger than the chosen significance level (e.g., p > 0.05), it suggests that the observed effect could plausibly occur due to random variability, and we fail to reject the null hypothesis.

  • Decision-making: When the p-value is less than the significance level, we typically reject the null hypothesis in favor of the alternative hypothesis, concluding that there is sufficient evidence to support the claim or hypothesis being tested. When the p-value is greater than the significance level, we fail to reject the null hypothesis, indicating that there is not enough evidence to support the alternative hypothesis.

T-Test:

  • Purpose: Used to determine if there is a significant difference between the means of two groups. Types: Independent t-test (for comparing means of two independent groups) and paired t-test (for comparing means of two related groups).

import statistics data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Example of a t-test sample_mean = 5.5 # Mean of the sample pop_mean = 5 # Population mean sample_std_dev = statistics.stdev(data) # Standard deviation of the sample sample_size = len(data) # Sample size t_statistic = (sample_mean - pop_mean) / (sample_std_dev / (sample_size ** 0.5)) print("t-statistic:", t_statistic)

T-Test Implementation

  • We'll have two groups, 'control' and 'treatment', with their respective data.

  • The data represents the scores of participants in a control group and a treatment group

control_group = [85, 88, 82, 90, 89, 87, 92, 86, 88, 84] treatment_group = [91, 92, 93, 95, 88, 90, 94, 92, 89, 93]
from scipy.stats import ttest_ind # Perform independent t-test t_statistic, p_value = ttest_ind(control_group, treatment_group) # Print results print("T-Test Results:") print("T-Statistic:", t_statistic) print("P-Value:", p_value) # Interpret results alpha = 0.05 if p_value < alpha: print("Reject the null hypothesis: There is a significant difference between the means of the two groups.") else: print("Fail to reject the null hypothesis: There is no significant difference between the means of the two groups.")
T-Test Results: T-Statistic: -3.934835044734194 P-Value: 0.0009710394539475604 Reject the null hypothesis: There is a significant difference between the means of the two groups.

Chi-Square Test:

The Chi-Square Test determines if there's a significant association between categorical variables by comparing observed and expected frequencies. It assesses whether the observed distribution differs from what would be expected by chance alone. Calculating a test statistic, typically Chi-Square (χ²), and comparing it to critical values or obtaining a p-value helps make the decision. If the statistic exceeds critical values or has a p-value below a chosen significance level (e.g., α = 0.05), the null hypothesis is rejected, suggesting a significant association. Otherwise, the null hypothesis is not rejected, indicating no significant association.

Example:

  • We'll have two categorical variables, 'gender' and 'smoking status', with their respective counts.

  • The data represents the counts of individuals categorized by gender and smoking status.

import numpy as np gender = ['Male', 'Female'] smoking_status = ['Smoker', 'Non-Smoker'] observed_counts = np.array([[30, 70], [40, 60]]) # Counts for gender and smoking status
from scipy.stats import chi2_contingency # Perform chi-square test chi2_statistic, p_value, dof, expected = chi2_contingency(observed_counts) # Print results print("\nChi-Square Test Results:") print("Chi-Square Statistic:", chi2_statistic) print("P-Value:", p_value) # Interpret results if p_value < alpha: print("Reject the null hypothesis: There is a significant association between gender and smoking status.") else: print("Fail to reject the null hypothesis: There is no significant association between gender and smoking status.")
Chi-Square Test Results: Chi-Square Statistic: 1.7802197802197803 P-Value: 0.18212234100949093 Fail to reject the null hypothesis: There is no significant association between gender and smoking status.

ANOVA (Analysis of Variance) Test

It is a statistical method used to compare the means of three or more samples to determine if they are significantly different from each other. It partitions the total variance observed in a data set into different components attributed to different sources of variation. ANOVA is typically used when you have one categorical independent variable (with three or more groups) and one continuous dependent variable.

import scipy.stats as stats # Sample data group1 = [18, 20, 16, 22, 19] group2 = [23, 25, 27, 29, 26] group3 = [17, 21, 24, 19, 20] # Perform one-way ANOVA statistic, p_value = stats.f_oneway(group1, group2, group3) # Interpret the result alpha = 0.05 print("Statistic:", statistic) print("p-value:", p_value) if p_value < alpha: print("Reject null hypothesis: There is a significant difference between the means.") else: print("Fail to reject null hypothesis: There is no significant difference between the means.")
Statistic: 12.586826347305376 p-value: 0.0011315560049115865 Reject null hypothesis: There is a significant difference between the means.

Comparision of different Hypothesis tests

Hypothesis TestType of DataResearch QuestionExample
T-Test (Independent/Separate Samples)Continuous (Numerical)Comparing means of two independent groupsComparing the average test scores of two different classes
T-Test (Paired Samples)Continuous (Numerical)Comparing means of two related groupsComparing pre-test and post-test scores of the same group
One-Way ANOVAContinuous (Numerical)Comparing means of three or more independent groupsComparing the average weight loss across different diets
Chi-Square TestCategorical (Nominal/Ordinal)Assessing association between two categorical variablesInvestigating if there's a relationship between smoking status and lung cancer incidence
Pearson's CorrelationContinuous (Numerical)Assessing the strength and direction of linear relationshipExamining the correlation between hours spent studying and exam scores
Mann-Whitney U TestContinuous (Numerical)Comparing distributions of two independent groups when assumptions of t-test are violatedComparing the median income of two neighborhoods when data is not normally distributed
Kruskal-Wallis TestContinuous (Numerical)Comparing distributions of three or more independent groups when assumptions of ANOVA are violatedComparing the median reaction times across different age groups when data is not normally distributed
Z-TestContinuous (Numerical)Comparing a sample mean to a known population meanTesting if the average height of students in a class is significantly different from the national average height