Hypothesis testing helps you decide if your data shows a real effect or if it happened by chance.
Hypothesis testing (ttest, chi2test) in 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.
x is different from 5.0 at default 5% significance.x = [5.1, 5.5, 5.3, 5.7, 5.2]; h = ttest(x, 5.0);
x differs from 11 with 1% significance level.x = [10, 12, 9, 11, 13]; h = ttest(x, 11, 'Alpha', 0.01);
observed = [20, 30, 50]; h = chi2gof(1:3, 'Frequency', observed);
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.
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);
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.
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
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
