P-values and significance in Data Analysis Python - Time & Space Complexity
We want to understand how the time to calculate p-values changes as the amount of data grows.
How does the work needed grow when we have more data points?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy import stats
def calculate_p_value(data1, data2):
t_stat, p_val = stats.ttest_ind(data1, data2)
return p_val
# data1 and data2 are lists or arrays of numbers
This code calculates the p-value from two groups of data using a t-test.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The t-test function internally processes each data point in both groups.
- How many times: Each data point in both data1 and data2 is visited once to compute means and variances.
As the number of data points increases, the time to compute the p-value grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (10 in each group) |
| 100 | About 200 |
| 1000 | About 2000 |
Pattern observation: Doubling the data roughly doubles the work needed.
Time Complexity: O(n)
This means the time to calculate the p-value grows linearly with the total number of data points.
[X] Wrong: "Calculating a p-value takes the same time no matter how much data there is."
[OK] Correct: The calculation must look at each data point to find averages and variances, so more data means more work.
Understanding how data size affects calculation time helps you explain your approach clearly and shows you know what happens behind the scenes.
"What if we used a bootstrap method with 1000 resamples to estimate the p-value? How would the time complexity change?"
Practice
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 AQuick Check:
p-value < 0.05 means significant [OK]
- Thinking p-value shows probability null is true
- Confusing significance level with p-value
- Assuming p-value proves practical importance
p_val is significant at 0.05 level in Python?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 CQuick Check:
Use < 0.05 for significance [OK]
- Using > instead of < for significance
- Checking equality to 0.05 exactly
- Using wrong threshold like 0.5
p_val = 0.07
if p_val < 0.05:
print('Reject null hypothesis')
else:
print('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 DQuick Check:
p_val = 0.07 > 0.05 means fail to reject [OK]
- Confusing greater than with less than
- Assuming equality means reject
- Ignoring else branch
p_val = 0.03
if p_val > 0.05:
print('Significant')
else:
print('Not Significant')Solution
Step 1: Analyze the condition logic
The code checks if p_val > 0.05 to print 'Significant', but significance means p_val < 0.05.Step 2: Identify the fix
Changing the operator to < will correctly print 'Significant' when p_val is 0.03.Final Answer:
The comparison operator should be '<' instead of '>' -> Option AQuick Check:
Use < 0.05 for significance check [OK]
- Using '>' instead of '<' for significance
- Swapping print statements without fixing condition
- Assuming variable is undefined
Solution
Step 1: Understand significance level
The significance level is set to 0.01, meaning p-values must be less than 0.01 to reject the null hypothesis.Step 2: Compare p-value to significance level
The p-value 0.049 is greater than 0.01, so we fail to reject the null hypothesis at this stricter level.Final Answer:
Fail to reject the null hypothesis because 0.049 > 0.01 -> Option BQuick Check:
p-value > significance level means fail to reject [OK]
- Using default 0.05 without checking chosen level
- Confusing less than and greater than
- Ignoring stricter significance level
Ready to prove it?
Pass the test and this pattern moves unknown
