Bird
Raised Fist0
MATLABdata~7 mins

Hypothesis testing (ttest, chi2test) in MATLAB

Choose your learning style10 modes available
Introduction

Hypothesis testing helps you decide if your data shows a real effect or if it happened by chance.

You want to check if the average of a group is different from a known value.
You want to compare averages of two groups to see if they differ.
You want to test if two categorical variables are related.
You want to check if observed counts match expected counts in categories.
Syntax
MATLAB
h = ttest(x, mu, 'Alpha', alpha)
h = chi2gof(x, 'Alpha', alpha)

ttest tests if the mean of data x is different from mu.

chi2gof tests if data x fits a given distribution using chi-square.

Examples
Test if the mean of x is different from 5.0 at default 5% significance.
MATLAB
x = [5.1, 5.5, 5.3, 5.7, 5.2];
h = ttest(x, 5.0);
Test if mean of x differs from 11 with 1% significance level.
MATLAB
x = [10, 12, 9, 11, 13];
h = ttest(x, 11, 'Alpha', 0.01);
Test if observed counts fit a uniform distribution at 5% significance.
MATLAB
observed = [20, 30, 50];
h = chi2gof(1:3, 'Frequency', observed);
Sample Program

This program tests if the average of x is 5.0 using a t-test. Then it tests if observed counts fit expected counts using a chi-square test.

MATLAB
x = [4.9, 5.0, 5.1, 5.2, 5.0];
mu = 5.0;
alpha = 0.05;

% Perform t-test
[h, p] = ttest(x, mu, 'Alpha', alpha);

if h == 0
    fprintf('Fail to reject null hypothesis: mean is %.1f\n', mu);
else
    fprintf('Reject null hypothesis: mean is not %.1f\n', mu);
end
fprintf('p-value = %.4f\n', p);

% Chi-square goodness of fit test
observed = [18, 22, 20];
expected = [20, 20, 20];
[h_chi2, p_chi2] = chi2gof(1:3, 'Frequency', observed, 'Expected', expected, 'Alpha', alpha);

if h_chi2 == 0
    fprintf('Fail to reject null hypothesis: observed fits expected counts\n');
else
    fprintf('Reject null hypothesis: observed does not fit expected counts\n');
end
fprintf('Chi-square p-value = %.4f\n', p_chi2);
OutputSuccess
Important Notes

The ttest returns h = 1 if it rejects the null hypothesis, otherwise h = 0.

Always check the p-value to understand the strength of evidence.

For chi2gof, you can specify expected counts to compare against observed.

Summary

Hypothesis tests help decide if data shows a real difference or relationship.

ttest checks if a mean equals a value; chi2gof checks if counts fit expected counts.

Use the test result and p-value to make decisions about your data.

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