Bird
Raised Fist0
MATLABdata~5 mins

Hypothesis testing (ttest, chi2test) in MATLAB - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Hypothesis testing (ttest, chi2test)
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

As the number of data points n grows, the t-test does more work roughly proportional to n.

Input Size (n)Approx. Operations
10About 10 operations to process data
100About 100 operations
1000About 1000 operations

Pattern observation: The work grows directly with the number of data points.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the t-test grows in a straight line as the data size increases.

Common Mistake

[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.

Interview Connect

Knowing how test functions scale helps you write code that handles big data without slowing down too much.

Self-Check

"What if we ran the t-test multiple times inside a loop over m different datasets? How would the time complexity change?"

Practice

(1/5)
1. What does the MATLAB function ttest primarily test?
easy
A. Whether the data follows a normal distribution
B. Whether two samples have the same variance
C. Whether the mean of a sample differs from a specified value
D. Whether the counts in categories match expected counts

Solution

  1. Step 1: Understand the purpose of ttest

    The ttest function in MATLAB tests if the mean of a sample is equal to a given value.
  2. Step 2: Compare with other tests

    chi2gof tests counts, not means; variance tests are different; normality tests are separate.
  3. Final Answer:

    Whether the mean of a sample differs from a specified value -> Option C
  4. Quick Check:

    ttest tests mean difference [OK]
Hint: Remember: ttest checks means, chi2 tests counts [OK]
Common Mistakes:
  • Confusing ttest with chi-square test
  • Thinking ttest checks variance
  • Assuming ttest checks distribution shape
2. Which of the following is the correct syntax to perform a one-sample t-test in MATLAB on data vector X against mean 0?
easy
A. chi2gof(X, 0)
B. ttest(X, 0)
C. ttest(0, X)
D. ttest(X)

Solution

  1. Step 1: Recall ttest syntax

    The syntax ttest(X, mu) tests if mean of X equals mu.
  2. Step 2: Check options

    chi2gof(X, 0) uses wrong function; ttest(0, X) reverses arguments; ttest(X) misses mean value.
  3. Final Answer:

    ttest(X, 0) -> Option B
  4. Quick Check:

    Correct syntax is ttest(data, mean) [OK]
Hint: Syntax: ttest(data, mean) for one-sample test [OK]
Common Mistakes:
  • Swapping arguments order
  • Using chi2gof for mean testing
  • Omitting mean parameter
3. What is the output of the following MATLAB code?
X = [5.1, 5.3, 5.0, 5.2, 5.4];
[h, p] = ttest(X, 5.0);
disp(h); disp(p);
medium
A. h = 1, p < 0.05 (reject mean = 5.0)
B. h = 1, p > 0.05 (fail to reject mean = 5.0)
C. h = 0, p < 0.05 (reject mean = 5.0)
D. h = 0, p > 0.05 (fail to reject mean = 5.0)

Solution

  1. Step 1: Compute sample statistics

    X mean = (5.1 + 5.3 + 5.0 + 5.2 + 5.4)/5 = 5.2; std ≈ 0.158.
  2. Step 2: Interpret ttest output

    t ≈ (5.2 - 5.0) / (0.158 / √5) ≈ 2.83, df=4, p ≈ 0.047 < 0.05, so h=1 (reject null).
  3. Final Answer:

    h = 1, p < 0.05 (reject mean = 5.0) -> Option A
  4. Quick Check:

    mean=5.2, t≈2.83 > 2.776, p<0.05 [OK]
Hint: Sample mean 5.2 sufficiently differs from 5.0 (t>crit) -> h=1, p<0.05 [OK]
Common Mistakes:
  • Assuming data too close to 5.0 for significance (h=0)
  • Miscalculating p-value as >0.05
  • Confusing h=1 (reject) meaning
4. Identify the error in this MATLAB code snippet for a chi-square goodness-of-fit test:
observed = [20, 30, 50];
expected = [25, 25, 50];
h = chi2gof(observed, 'ExpectedCounts', expected);
medium
A. The 'Expected' parameter is misspelled
B. The vectors observed and expected must be the same length
C. chi2gof cannot be used with expected counts
D. The observed data should be a vector of counts, but chi2gof expects raw data samples

Solution

  1. Step 1: Understand chi2gof input requirements

    chi2gof expects raw sample data, not observed counts directly.
  2. Step 2: Check usage of 'ExpectedCounts'

    The 'ExpectedCounts' parameter is valid but only used with raw data; providing counts directly leads to incorrect results.
  3. Final Answer:

    The observed data should be a vector of counts, but chi2gof expects raw data samples -> Option D
  4. Quick Check:

    chi2gof needs raw data, not counts [OK]
Hint: chi2gof needs raw data, not counts vector [OK]
Common Mistakes:
  • Passing counts instead of raw data
  • Misspelling parameter names
  • Assuming chi2gof accepts counts directly
5. You have observed counts 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?
hard
A. Convert expected proportions to expected counts, then use chi2gof with raw data generated from counts
B. Directly use chi2gof(obs, 'Expected', expProp)
C. Use ttest(obs, expProp) to compare counts and proportions
D. Use chi2gof with observed counts and expected counts vectors

Solution

  1. 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].
  2. Step 2: Generate raw data samples for chi2gof

    Since chi2gof needs raw data, create a vector with repeated category labels matching observed counts.
  3. Step 3: Perform chi-square test

    Use chi2gof with raw data and specify expected counts via 'ExpectedCounts' parameter.
  4. Final Answer:

    Convert expected proportions to expected counts, then use chi2gof with raw data generated from counts -> Option A
  5. Quick Check:

    chi2gof needs raw data and expected counts [OK]
Hint: Convert proportions to counts, create raw data for chi2gof [OK]
Common Mistakes:
  • Passing proportions directly as expected counts
  • Using ttest for count data
  • Not converting counts to raw data samples
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard