Bird
Raised Fist0
MATLABdata~10 mins

Hypothesis testing (ttest, chi2test) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Hypothesis testing (ttest, chi2test)
Start: Define Hypotheses
↓
Collect Sample Data
↓
Choose Test Type
↓
ttest
↓
Calculate Test Statistic
↓
Find p-value
↓
Compare p-value with alpha
↓
Reject H0
↓
End
This flow shows how to perform hypothesis testing: define hypotheses, collect data, choose test, calculate statistic, find p-value, then decide to reject or not reject the null hypothesis.
Execution Sample
MATLAB
data1 = [5.1, 4.9, 5.0, 5.2, 5.1];
data2 = [5.5, 5.7, 5.6, 5.8, 5.7];
[h,p] = ttest2(data1, data2);
chi2stat = 10.5;
pval = 1 - chi2cdf(chi2stat, 4);
This code runs a two-sample t-test on two data sets and calculates a chi-square test p-value from a test statistic.
Execution Table
StepActionVariable/ExpressionValue/ResultExplanation
1Define data1data1[5.1, 4.9, 5.0, 5.2, 5.1]Sample data group 1
2Define data2data2[5.5, 5.7, 5.6, 5.8, 5.7]Sample data group 2
3Run two-sample t-test[h, p] = ttest2(data1, data2)[0, 0.0003]h=0 means reject null, p=0.0003 is p-value
4Define chi-square statchi2stat10.5Given chi-square test statistic
5Calculate p-valuepval = 1 - chi2cdf(chi2stat, 4)0.033p-value from chi-square distribution with 4 degrees of freedom
6Decision t-testp < 0.05Truep-value less than alpha 0.05, reject null hypothesis
7Decision chi2-testpval < 0.05Truep-value less than alpha 0.05, reject null hypothesis
8End--Tests complete
💡 Execution stops after calculating p-values and making decisions based on alpha = 0.05
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
data1undefined[5.1,4.9,5.0,5.2,5.1][5.1,4.9,5.0,5.2,5.1][5.1,4.9,5.0,5.2,5.1][5.1,4.9,5.0,5.2,5.1][5.1,4.9,5.0,5.2,5.1][5.1,4.9,5.0,5.2,5.1]
data2undefinedundefined[5.5,5.7,5.6,5.8,5.7][5.5,5.7,5.6,5.8,5.7][5.5,5.7,5.6,5.8,5.7][5.5,5.7,5.6,5.8,5.7][5.5,5.7,5.6,5.8,5.7]
hundefinedundefinedundefined0000
pundefinedundefinedundefined0.00030.00030.00030.0003
chi2statundefinedundefinedundefinedundefined10.510.510.5
pvalundefinedundefinedundefinedundefinedundefined0.0330.033
Key Moments - 2 Insights
Why does h equal 0 mean we reject the null hypothesis in the t-test?
In the execution_table row 3, h=0 means the test found a significant difference, so we reject the null hypothesis. MATLAB's ttest2 returns h=1 to accept null, h=0 to reject.
How is the p-value for the chi-square test calculated from the test statistic?
Row 5 shows pval = 1 - chi2cdf(chi2stat, 4). This means we find the area to the right of the test statistic under the chi-square curve with 4 degrees of freedom.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does the p-value 0.0003 tell us about the t-test?
AThe difference between data1 and data2 is statistically significant
BThe difference between data1 and data2 is not statistically significant
CThe test failed to run
DThe sample sizes are too small
💡 Hint
Check the p-value column at step 3 and compare it to alpha=0.05 in the table
At which step does the chi-square p-value get calculated?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the row where pval is assigned using chi2cdf in the execution_table
If the chi2stat was 2.0 instead of 10.5, how would the p-value change?
AIt would increase, making it less likely to reject the null
BIt would decrease, making it more likely to reject the null
CIt would stay the same
DIt would become zero
💡 Hint
Recall that higher chi-square stats give lower p-values; check the pval calculation in variable_tracker
Concept Snapshot
Hypothesis testing steps:
1. Define null and alternative hypotheses.
2. Collect sample data.
3. Choose test: t-test for means, chi2-test for categorical.
4. Calculate test statistic and p-value.
5. Compare p-value to alpha (e.g., 0.05).
6. Reject null if p <= alpha, else fail to reject.
Full Transcript
This visual execution shows how hypothesis testing works in MATLAB using t-test and chi-square test. First, we define two data samples and run a two-sample t-test with ttest2. The function returns h and p, where h=0 means reject null hypothesis and p is the p-value. Then, we calculate a chi-square test p-value from a given test statistic using chi2cdf. We compare both p-values to the significance level 0.05 to decide if we reject the null hypothesis. The variable tracker shows how data and results change step by step. Key moments clarify why h=0 means reject and how p-values are computed. The quiz tests understanding of p-values and steps. The snapshot summarizes the hypothesis testing process simply.

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