Bird
Raised Fist0
MATLABdata~20 mins

Hypothesis testing (ttest, chi2test) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hypothesis Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a one-sample t-test in MATLAB
What is the output of the following MATLAB code snippet?
MATLAB
data = [5.1, 5.5, 5.3, 5.7, 5.2];
[h,p,ci,stats] = ttest(data, 5.0);
disp(h);
disp(round(p,3));
A
0
0.012
B
0
0.500
C
1
0.012
D
1
0.500
Attempts:
2 left
💡 Hint
Recall that h=0 means fail to reject the null hypothesis at default 5% significance.
❓ Predict Output
intermediate
2:00remaining
Chi-square test output for independence
What is the output of the following MATLAB code snippet?
MATLAB
observed = [20 30; 15 35];
[h,p,stats] = chi2gof([1 2 1 2], 'Ctrs', [1 2], 'Frequency', observed(:));
disp(h);
disp(round(p,3));
A
1
0.003
B
0
0.003
C
0
0.500
D
1
0.500
Attempts:
2 left
💡 Hint
Check if the p-value is less than 0.05 to reject the null hypothesis.
🧠 Conceptual
advanced
1:30remaining
Understanding MATLAB ttest output variables
Which of the following correctly describes the variable stats returned by MATLAB's ttest function?
AA structure containing the t-statistic and degrees of freedom
BA vector of p-values for each data point
CA scalar indicating the test decision (0 or 1)
DA matrix of confidence intervals
Attempts:
2 left
💡 Hint
Check MATLAB documentation for the output arguments of ttest.
❓ Predict Output
advanced
2:00remaining
Result of two-sample t-test with unequal variances
What is the output of the following MATLAB code snippet?
MATLAB
x1 = [5.1, 5.5, 5.3, 5.7, 5.2];
x2 = [4.9, 5.0, 5.1, 4.8, 5.0];
[h,p] = ttest2(x1, x2, 'Vartype', 'unequal');
disp(h);
disp(round(p,3));
A
0
0.002
B
0
0.500
C
1
0.500
D
1
0.002
Attempts:
2 left
💡 Hint
Check if p-value is less than 0.05 to reject the null hypothesis.
❓ Predict Output
expert
2:30remaining
Chi-square test error due to input format
What error does the following MATLAB code produce?
MATLAB
observed = [10 20 30];
expected = [15 15 30];
[h,p] = chi2gof(observed, 'Expected', expected);
disp(h);
Ah = 1
BError: 'chi2gof' requires data vector, not frequency counts
Ch = 0
DError: Dimensions of observed and expected must match
Attempts:
2 left
💡 Hint
Check the input requirements of chi2gof in MATLAB.

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