Statistics helps us decide if an idea about data is likely true or just happened by chance.
Checking if a new medicine works better than the old one.
Deciding if a marketing campaign increased sales.
Testing if students learn more with a new teaching method.
Seeing if a machine part lasts longer after a design change.
Syntax
Data Analysis Python
1. State a hypothesis (an idea to test).
2. Collect data.
3. Use a statistical test to get a p-value.
4. Compare p-value to a threshold (like 0.05).
5. Decide if data supports the hypothesis ornot.
The p-value is the probability of observing the data (or more extreme) if the null hypothesis is true.
A small p-value provides evidence against the null hypothesis.
Examples
This checks if 60 heads out of 100 flips is unusual for a fair coin.
Data Analysis Python
# Example: Test if coin is fair# Hypothesis: Coin is fair (50% heads)# Data: 60 heads in 100 flipsfrom scipy import stats
# Perform binomial test
res = stats.binomtest(60, n=100, p=0.5, alternative='two-sided')
p_value = res.pvalue
print(f"p-value: {p_value:.3f}")
This tests if the sample average height is different from 170 cm.
Data Analysis Python
# Example: Test if average height differs# Hypothesis: Average height is 170 cm# Data: Sample heightsimport numpy as np
from scipy import stats
heights = np.array([168, 172, 171, 169, 173])
# Perform one-sample t-test
stat, p_value = stats.ttest_1samp(heights, 170)
print(f"p-value: {p_value:.3f}")
Sample Program
This program tests if the average score with the new method is higher than 75.
Data Analysis Python
import numpy as np
from scipy import stats
# Suppose we want to test if a new teaching method improves test scores# Hypothesis: New method improves scores (mean > 75)# Sample scores from students using new method
scores = np.array([78, 82, 85, 79, 90, 88, 76, 84])
# Perform one-sample t-test against 75
stat, p_value = stats.ttest_1samp(scores, 75)
# Since we want mean > 75, use one-sided p-value
p_value_one_sided = p_value / 2if stat > 0else1 - p_value / 2print(f"Test statistic: {stat:.3f}")
print(f"One-sided p-value: {p_value_one_sided:.3f}")
# Decide if new method improves scores at 0.05 significanceif p_value_one_sided < 0.05:
print("We have evidence that the new method improves scores.")
else:
print("We do not have enough evidence to say the new method improves scores.")
OutputSuccess
Important Notes
Always define your hypothesis clearly before testing.
Statistical tests give probabilities, not absolute truths.
Choosing the right test depends on your data and question.
Summary
Statistics helps check if data supports an idea or if results are by chance.
We use tests and p-values to make decisions about hypotheses.
Small p-values indicate stronger evidence against the null hypothesis.
Practice
(1/5)
1. What does a small p-value indicate in hypothesis testing?
easy
A. The data is random and unimportant
B. Strong evidence against the null hypothesis
C. The hypothesis is definitely true
D. The sample size is too small
Solution
Step 1: Understand p-value meaning
A p-value measures how likely the observed data would occur if the null hypothesis were true.
Step 2: Interpret small p-value
A small p-value means the observed data is unlikely under the null hypothesis, so we have strong evidence against it.
Final Answer:
Strong evidence against the null hypothesis -> Option B
Quick Check:
Small p-value = reject null hypothesis [OK]
Hint: Small p-value means reject null hypothesis [OK]
Common Mistakes:
Thinking small p-value proves hypothesis true
Confusing p-value with sample size
Assuming p-value shows data randomness
2. Which Python library is commonly used to perform hypothesis testing?
easy
A. matplotlib
B. seaborn
C. scipy.stats
D. pandas
Solution
Step 1: Identify libraries for statistics
matplotlib and seaborn are for plotting; pandas is for data handling.
Step 2: Recognize scipy.stats role
scipy.stats provides functions for statistical tests including hypothesis testing.
Final Answer:
scipy.stats -> Option C
Quick Check:
scipy.stats = stats tests [OK]
Hint: Use scipy.stats for hypothesis tests in Python [OK]
Common Mistakes:
Choosing plotting libraries for testing
Confusing data handling with testing
Not knowing scipy.stats functions
3. What is the output of this Python code snippet?
from scipy import stats
sample = [5, 7, 8, 6, 9]
result = stats.ttest_1samp(sample, 7)
print(round(result.pvalue, 3))
medium
A. 0.317
B. 0.045
C. 0.500
D. 0.001
Solution
Step 1: Understand t-test function
stats.ttest_1samp tests if sample mean differs from 7.
Step 2: Calculate p-value
Running the code gives p-value approximately 0.317, meaning no strong evidence against mean=7.
Final Answer:
0.317 -> Option A
Quick Check:
ttest p-value ≈ 0.317 [OK]
Hint: Run code to get exact p-value [OK]
Common Mistakes:
Assuming p-value is always below 0.05
Mixing up test statistic and p-value
Rounding errors in p-value
4. Identify the error in this hypothesis test code:
from scipy import stats
sample = [10, 12, 9, 11]
p_value = stats.ttest_1samp(sample, 10)
if p_value < 0.05:
print("Reject null hypothesis")
else:
print("Fail to reject null hypothesis")
medium
A. p_value is a tuple, should use p_value.pvalue
B. Comparison operator should be == not <
C. Sample size is too small for t-test
D. Missing import for numpy
Solution
Step 1: Check return type of ttest_1samp
stats.ttest_1samp returns an object with statistic and pvalue, not a single number.
Step 2: Correct p-value access
We must use p_value.pvalue to get the p-value number for comparison.
Final Answer:
p_value is a tuple, should use p_value.pvalue -> Option A
Quick Check:
Use .pvalue attribute to get p-value [OK]
Hint: Use .pvalue attribute from ttest result [OK]
Common Mistakes:
Comparing whole result object directly
Wrong comparison operator
Assuming numpy import needed here
5. You want to test if a new teaching method improves scores. You collect scores from 30 students before and after. Which statistical test best validates your hypothesis?
hard
A. Independent two-sample t-test
B. Chi-square test
C. One-sample z-test
D. Paired t-test
Solution
Step 1: Identify data type and design
Scores are from same students before and after, so data is paired.
Step 2: Choose correct test for paired data
Paired t-test compares means of related samples to check improvement.
Final Answer:
Paired t-test -> Option D
Quick Check:
Paired data needs paired t-test [OK]
Hint: Use paired t-test for before-after same subjects [OK]
Common Mistakes:
Using independent t-test for paired data
Choosing chi-square for numeric scores
Using z-test without population variance
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.