P-values help us decide if a result is likely due to chance or if it shows a real effect.
Checking if a new medicine works better than an old one.
Testing if a marketing campaign increased sales.
Seeing if students' test scores improved after extra tutoring.
Determining if a coin is fair or biased.
Syntax
Data Analysis Python
from scipy import stats
# Calculate p-value from test statistic and degrees of freedom
p_value = stats.t.sf(abs(t_stat), df)*2# Or directly from data samples
stat, p_value = stats.ttest_ind(sample1, sample2)
P-value is a number between 0 and 1.
Lower p-values mean stronger evidence against chance.
Examples
Tests if sample mean is different from 6.
Data Analysis Python
from scipy import stats
# Example 1: One sample t-test
sample = [5, 6, 7, 8, 9]
stat, p_value = stats.ttest_1samp(sample, 6)
print(f"p-value: {p_value:.3f}")
This code compares two groups' exam scores to check if they differ significantly.
Data Analysis Python
from scipy import stats
# Two groups of exam scores
group_A = [85, 87, 90, 88, 86]
group_B = [80, 82, 79, 81, 83]
# Perform t-test to see if scores differ
stat, p_value = stats.ttest_ind(group_A, group_B)
print(f"Test statistic: {stat:.3f}")
print(f"P-value: {p_value:.3f}")
# Decide significance at 0.05 levelif p_value < 0.05:
print("Result is significant: groups differ.")
else:
print("Result is not significant: no clear difference.")
OutputSuccess
Important Notes
A common cutoff for significance is 0.05, meaning 5% chance results are random.
P-values do not measure size of effect, only evidence against chance.
Always consider context and data quality, not just p-value.
Summary
P-values help decide if results are likely real or by chance.
Lower p-values mean stronger evidence against randomness.
Use p-values with a significance level like 0.05 to make decisions.
Practice
(1/5)
1. What does a p-value less than 0.05 usually indicate in a hypothesis test?
easy
A. The results are statistically significant and unlikely due to chance.
B. The results are definitely caused by random chance.
C. The sample size is too small to draw conclusions.
D. The test was performed incorrectly.
Solution
Step 1: Understand the meaning of p-value
A p-value measures how likely the observed data would occur if the null hypothesis were true (random chance).
Step 2: Compare p-value to significance level
If the p-value is less than 0.05 (common threshold), it means the observed result is unlikely due to chance, so we reject the null hypothesis.
Final Answer:
The results are statistically significant and unlikely due to chance. -> Option A
Quick Check:
p-value < 0.05 means significant [OK]
Hint: p-value < 0.05 means strong evidence against chance [OK]
Common Mistakes:
Thinking p-value shows probability null is true
Confusing significance level with p-value
Assuming p-value proves practical importance
2. Which of the following is the correct way to check if a p-value stored in variable p_val is significant at 0.05 level in Python?
easy
A. if p_val == 0.05: print('Significant')
B. if p_val > 0.05: print('Significant')
C. if p_val < 0.05: print('Significant')
D. if p_val <= 0.5: print('Significant')
Solution
Step 1: Recall significance condition
A p-value less than 0.05 means significance, so we check if p_val < 0.05.
Step 2: Match condition to code
if p_val < 0.05: print('Significant') uses < 0.05 correctly. Others check wrong conditions or wrong thresholds.
Final Answer:
if p_val < 0.05: print('Significant') -> Option C
Quick Check:
Use < 0.05 for significance [OK]
Hint: Check if p_val < 0.05 for significance [OK]
Common Mistakes:
Using > instead of < for significance
Checking equality to 0.05 exactly
Using wrong threshold like 0.5
3. Given this Python code snippet, what will be printed?
p_val = 0.07
if p_val < 0.05:
print('Reject null hypothesis')
else:
print('Fail to reject null hypothesis')
medium
A. Reject null hypothesis
B. SyntaxError
C. No output
D. Fail to reject null hypothesis
Solution
Step 1: Check the p-value against 0.05
The p_val is 0.07, which is greater than 0.05, so the condition p_val < 0.05 is False.
Step 2: Determine which branch runs
Since the condition is False, the else block runs, printing 'Fail to reject null hypothesis'.
Final Answer:
Fail to reject null hypothesis -> Option D
Quick Check:
p_val = 0.07 > 0.05 means fail to reject [OK]
Hint: p_val > 0.05 means fail to reject null [OK]
Common Mistakes:
Confusing greater than with less than
Assuming equality means reject
Ignoring else branch
4. This code is intended to print 'Significant' if p-value is less than 0.05, but it always prints 'Not Significant'. What is the error?