t-test (ttest_ind, ttest_rel) in SciPy - Time & Space Complexity
We want to understand how the time it takes to run a t-test grows as the amount of data increases.
Specifically, how does the test behave when we have more numbers to compare?
Analyze the time complexity of the following code snippet.
from scipy.stats import ttest_ind
# Two independent samples
sample1 = [1, 2, 3, 4, 5]
sample2 = [2, 3, 4, 5, 6]
result = ttest_ind(sample1, sample2)
print(result)
This code runs an independent t-test to compare the means of two groups of numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating sums and variances by going through each number in both samples.
- How many times: Each sample is scanned once to compute statistics.
As the number of data points in each sample grows, the time to calculate sums and variances grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (two samples of 10 each) |
| 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 run the t-test grows linearly with the number of data points.
[X] Wrong: "The t-test time grows with the square of the data size because it compares every pair of points."
[OK] Correct: The t-test only needs to calculate sums and variances, which requires looking at each data point once, not comparing all pairs.
Knowing how the t-test scales helps you explain performance when working with bigger datasets, showing you understand both statistics and efficiency.
"What if we used a paired t-test (ttest_rel) on samples of the same size? How would the time complexity change?"
Practice
What is the main difference between ttest_ind and ttest_rel in SciPy?
Solution
Step 1: Understand the purpose of
ttest_indttest_indis used to compare the means of two independent groups, meaning the groups have no relationship.Step 2: Understand the purpose of
ttest_relttest_relis used for paired samples, where the same subjects are measured twice or matched pairs are compared.Final Answer:
ttest_indcompares two independent groups;ttest_relcompares paired samples. -> Option AQuick Check:
Independent vs paired samples =ttest_indcompares two independent groups;ttest_relcompares paired samples. [OK]
- Confusing which test is for paired samples
- Thinking both tests are interchangeable
- Assuming
ttest_indis for paired data
Which of the following is the correct way to import and run an independent t-test on two arrays a and b using SciPy?
import scipy.stats as stats
# Your code hereSolution
Step 1: Identify the correct function for independent samples
The function for independent samples isttest_ind, which takes two arrays as arguments.Step 2: Check the function call syntax
The correct syntax isstats.ttest_ind(a, b). Using only one array or the paired testttest_relis incorrect here.Final Answer:
stats.ttest_ind(a, b) -> Option AQuick Check:
Independent test with two arrays = stats.ttest_ind(a, b) [OK]
- Using ttest_rel for independent samples
- Passing only one array instead of two
- Forgetting to import scipy.stats correctly
What is the output of this code snippet?
from scipy.stats import ttest_rel
before = [20, 22, 19, 24, 25]
after = [21, 23, 20, 22, 26]
result = ttest_rel(before, after)
print(round(result.pvalue, 3))Solution
Step 1: Understand the paired t-test calculation
Thettest_relcompares paired samples 'before' and 'after'. The differences are small.Step 2: Calculate or trust the p-value output
Running the code gives a p-value approximately 0.531.Final Answer:
0.531 -> Option CQuick Check:
Paired t-test p-value ~0.531 = 0.531 [OK]
- Confusing p-value with t-statistic
- Using ttest_ind instead of ttest_rel
- Rounding errors in p-value
Identify the error in this code that tries to perform an independent t-test:
from scipy.stats import ttest_ind
a = [5, 7, 8]
b = [6, 9]
result = ttest_ind(a)
print(result.pvalue)Solution
Step 1: Check function call parameters
ttest_indrequires two arrays as arguments. Here only one arrayais passed.Step 2: Verify other options
Usingttest_indis correct for independent samples. Different lengths are allowed. Import statement is correct.Final Answer:
Missing second argument forttest_indfunction. -> Option DQuick Check:
Two arrays needed for ttest_ind = Missing second argument forttest_indfunction. [OK]
- Passing only one array to ttest_ind
- Thinking arrays must be same length for independent test
- Confusing ttest_ind and ttest_rel usage
You have test scores of 10 students before and after a training program. You want to check if the training improved scores significantly. Which test should you use and why?
Solution
Step 1: Identify the data type
Scores before and after training are from the same students, so data are paired.Step 2: Choose the correct t-test
ttest_relis designed for paired samples to compare means before and after treatment.Final Answer:
Usettest_relbecause the scores are paired measurements. -> Option BQuick Check:
Paired data needs ttest_rel = Usettest_relbecause the scores are paired measurements. [OK]
- Using ttest_ind for paired data
- Confusing correlation with mean comparison
- Ignoring the pairing in data
Ready to prove it?
Pass the test and this pattern moves unknown
