Hypothesis Testing with matlab
Tags: dataanalysis, matlab, datascience, hypothesistesting
Published: 2021-11-21 14:08:52


Introduction

In this article we will explore some matlab functions for hypothesis testing in data analysis.
 
---

**ttest** = One-sample and paired-sample t-test

h = ttest(x) returns a test decision for the null hypothesis that the data in x comes from a normal distribution with mean equal to zero and unknown variance, using the one-sample t-test. The alternative hypothesis is that the population distribution does not have a mean equal to zero. The result h is 1 if the test rejects the null hypothesis at the 5% significance level, and 0 otherwise.

example
h = ttest(x,y) returns a test decision for the null hypothesis that the data in x – y comes from a normal distribution with mean equal to zero and unknown variance, using the paired-sample t-test.

example
h = ttest(x,y,Name,Value) returns a test decision for the paired-sample t-test with additional options specified by one or more name-value pair arguments. For example, you can change the significance level or conduct a one-sided test.

example
h = ttest(x,m) returns a test decision for the null hypothesis that the data in x comes from a normal distribution with mean m and unknown variance. The alternative hypothesis is that the mean is not m.

---
Example: Test the null hypothesis from sample data A with mean equal to zero at the 1% significance level and calculate confidence interval of mean.

```
clear;
clc; 
A  = [41 46 47 47 48 50 50 50 50 50 50 50 48 50 50 50 50 50 50 50 52 52 53 55 50 50 50 50 52 52 53 53 53 53 53 57 52 52 53 53 53 53 53 53 54 54 55 68]; 
alpha = 0.01; 
[h,p,ci] = ttest(A,0,'Alpha',0.01)
fprintf('CI for mean(A)=[%2.3f,%2.3f] \n', ci(1),ci(2)); 
```

Result:
CI for mean(A)=[50.017,52.817] 








---
 
**vartest** = chi-square variance test

- **h = vartest(x,v)** returns a test decision for the null hypothesis that the data in vector x comes from a normal distribution with variance v, using the chi-square variance test. The alternative hypothesis is that x comes from a normal distribution with a different variance. The result h is 1 if the test rejects the null hypothesis at the 5% significance level, and 0 otherwise.

 
- **h = vartest(x,v,Name,Value)** performs the chi-square variance test with additional options specified by one or more name-value pair arguments. For example, you can change the significance level or conduct a one-sided test.


Input arguments:

```
- x — Sample data (vector | matrix | multidimensional array)
- v — Hypothesized variance (scalar value)
```

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

```
- Alpha — Significance level (0.05 (default) | scalar value in the range (0,1))
- Dim — Dimension,first nonsingleton dimension (default) | positive integer value
```

Output arguments:
```
- h — Hypothesis test result 1 | 0
If h = 1, this indicates the rejection of the null hypothesis at the Alpha significance level.

If h = 0, this indicates a failure to reject the null hypothesis at the Alpha significance level.

- p — p-value
p-value of the test, returned as a scalar value in the range [0,1]. p is the probability of observing a test statistic as extreme as, or more extreme than, the observed value under the null hypothesis. Small values of p cast doubt on the validity of the null hypothesis.

- ci — Confidence interval
Confidence interval for the true variance, returned as a two-element vector containing the lower and upper boundaries of the 100 × (1 – Alpha)% confidence interval.


```
 

Example Program to calculate confidence interval of variance
and standard deviation of a matrix using vartest:

```
 A  = [41 46 47 47 48 50 50 50 50 50 50 50 48 50 50 50 50 50 50 50 52 52 53 55 50 50 50 50 52 52 53 53 53 53 53 57 52 52 53 53 53 53 53 53 54 54 55 68]; 
alpha = 0.05; 
variance = var(A)
[h,pvar,civarV]= vartest(A ,variance, alpha);
fprintf('CI for var(A)=[%2.3f,%2.3f] \n', civarV(1),civarV(2));
fprintf('CI for sigma(A)=[%2.3f,%2.3f] \n',sqrt(civarV(1)),sqrt(civarV(2)));

```
 

<u>Some theory</u>:
Chi-Square Variance Test
The chi-square variance test is used to test whether the variance of a population is equal to a hypothesized value.

The test statistic is

T=(n−1)[(s/ σ)^2]

where n is the sample size, s is the sample standard deviation, and σ is the hypothesized standard deviation. The denominator is the ratio of the sample standard deviation to the hypothesized 


standard deviation. The further this ratio deviates from 1, the more likely you are to reject the null hypothesis. The test statistic T has a chi-square distribution with n – 1 degrees of freedom under the null hypothesis.

---

- **chi2gof** = Chi-square goodness-of-fit test

<u>Some theory</u>:
The Chi-square goodness of fit test is a statistical hypothesis test used to determine whether a variable is likely to come from a specified distribution or not. It is often used to evaluate whether sample data is representative of the full population.


Description:
- h = chi2gof(x) returns a test decision for the null hypothesis that the data in vector x comes from a normal distribution with a mean and variance estimated from x, using the chi-square goodness-of-fit test. The alternative hypothesis is that the data does not come from such a distribution. The result h is 1 if the test rejects the null hypothesis at the 5% significance level, and 0 otherwise.

 
h = chi2gof(x,Name,Value) returns a test decision for the chi-square goodness-of-fit test with additional options specified by one or more name-value pair arguments. For example, you can test for a distribution other than normal, or change the significance level of the test.


Example:

```
clear;
clc; 
 
binomialdist = makedist('Binomial'); 
normaldist = makedist('Normal');
b = random(binomialdist,100,1);
n = random(normaldist,100,1);

%Returns a test decision for the null hypothesis that the data in 
%vector comes from a normal distribution with a mean and variance
 %estimated from the vector
h1 = chi2gof(b);
h2 = chi2gof(n) 
%returns a test decision for the chi-square goodness-of-fit test for a
%binomial distribution
h3 = chi2gof(b,'CDF',binomialdist);

fprintf("h1 = %d\n",h1); % false
fprintf("h2 = %d\n",h2); % true
fprintf("h3 = %d\n",h3); % true
```

Result:
h1 = 1
h2 = 0
h3 = 0