Hypothesis testing (ttest, chi2test) in MATLAB - Time & Space Complexity
We want to understand how the time it takes to run hypothesis tests changes as the data size grows.
How does the number of data points affect the work done by ttest and chi2test?
Analyze the time complexity of the following code snippet.
data = randn(n,1); % generate n random numbers
[h,p] = ttest(data, 0); % perform t-test against mean 0
observed = [20 30 50];
expected = [25 25 50];
[h_chi,p_chi] = chi2gof(observed, 'Expected', expected); % chi-square goodness-of-fit test
This code runs a t-test on n data points and a chi-square test comparing observed and expected counts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The t-test loops through all n data points to calculate the mean and variance.
- How many times: Once over all n points for ttest; chi-square test loops over the fixed size of observed categories (usually small and constant).
As the number of data points n grows, the t-test does more work roughly proportional to n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations to process data |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The work grows directly with the number of data points.
Time Complexity: O(n)
This means the time to run the t-test grows in a straight line as the data size increases.
[X] Wrong: "The t-test time grows faster than the data size, like n squared."
[OK] Correct: The t-test only needs to look at each data point once to calculate sums, so it grows linearly, not by multiplying n by itself.
Knowing how test functions scale helps you write code that handles big data without slowing down too much.
"What if we ran the t-test multiple times inside a loop over m different datasets? How would the time complexity change?"
Practice
ttest primarily test?Solution
Step 1: Understand the purpose of
Thettestttestfunction in MATLAB tests if the mean of a sample is equal to a given value.Step 2: Compare with other tests
chi2goftests counts, not means; variance tests are different; normality tests are separate.Final Answer:
Whether the mean of a sample differs from a specified value -> Option CQuick Check:
ttesttests mean difference [OK]
- Confusing ttest with chi-square test
- Thinking ttest checks variance
- Assuming ttest checks distribution shape
X against mean 0?Solution
Step 1: Recall
The syntaxttestsyntaxttest(X, mu)tests if mean of X equals mu.Step 2: Check options
chi2gof(X, 0)uses wrong function;ttest(0, X)reverses arguments;ttest(X)misses mean value.Final Answer:
ttest(X, 0) -> Option BQuick Check:
Correct syntax is ttest(data, mean) [OK]
- Swapping arguments order
- Using chi2gof for mean testing
- Omitting mean parameter
X = [5.1, 5.3, 5.0, 5.2, 5.4];
[h, p] = ttest(X, 5.0);
disp(h); disp(p);
Solution
Step 1: Compute sample statistics
X mean = (5.1 + 5.3 + 5.0 + 5.2 + 5.4)/5 = 5.2; std ≈ 0.158.Step 2: Interpret
t ≈ (5.2 - 5.0) / (0.158 / √5) ≈ 2.83, df=4, p ≈ 0.047 < 0.05, so h=1 (reject null).ttestoutputFinal Answer:
h = 1, p < 0.05 (reject mean = 5.0) -> Option AQuick Check:
mean=5.2, t≈2.83 > 2.776, p<0.05 [OK]
- Assuming data too close to 5.0 for significance (h=0)
- Miscalculating p-value as >0.05
- Confusing h=1 (reject) meaning
observed = [20, 30, 50];
expected = [25, 25, 50];
h = chi2gof(observed, 'ExpectedCounts', expected);
Solution
Step 1: Understand chi2gof input requirements
chi2gofexpects raw sample data, not observed counts directly.Step 2: Check usage of 'ExpectedCounts'
The 'ExpectedCounts' parameter is valid but only used with raw data; providing counts directly leads to incorrect results.Final Answer:
The observed data should be a vector of counts, but chi2gof expects raw data samples -> Option DQuick Check:
chi2gof needs raw data, not counts [OK]
- Passing counts instead of raw data
- Misspelling parameter names
- Assuming chi2gof accepts counts directly
obs = [40, 35, 25] and expected proportions expProp = [0.3, 0.4, 0.3]. How do you correctly perform a chi-square goodness-of-fit test in MATLAB to check if observed counts fit expected proportions?Solution
Step 1: Calculate expected counts from proportions
Multiply total observed count by expected proportions: total = 40+35+25=100; expected counts = 100 * expProp = [30, 40, 30].Step 2: Generate raw data samples for chi2gof
Sincechi2gofneeds raw data, create a vector with repeated category labels matching observed counts.Step 3: Perform chi-square test
Usechi2gofwith raw data and specify expected counts via 'ExpectedCounts' parameter.Final Answer:
Convert expected proportions to expected counts, then usechi2gofwith raw data generated from counts -> Option AQuick Check:
chi2gof needs raw data and expected counts [OK]
- Passing proportions directly as expected counts
- Using ttest for count data
- Not converting counts to raw data samples
Ready to prove it?
Pass the test and this pattern moves unknown
