# ttest

One-sample and paired-sample t-test

## Description

example

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

h = ttest(x,m,Name,Value) returns a test decision for the one-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,p] = ttest(___) also returns the p-value, p, of the test, using any of the input arguments from the previous syntax groups.

example

[h,p,ci,stats] = ttest(___) also returns the confidence interval ci for the mean of x, or of x – y for the paired t-test, and the structure stats containing information about the test statistic.

## Examples

collapse all

### Test for a Mean Equal to Zero

Load the sample data. Create a vector containing the third column of the stock returns data.

x = stocks(:,3);

Test the null hypothesis that the sample data comes from a population with mean equal to zero.

[h,p,ci,stats] = ttest(x)
h =
1

p =

0.0106

ci =
-0.7357
-0.0997

stats =
tstat: -2.6065
df: 99
sd: 1.6027

The returned value h = 1 indicates that ttest rejects the null hypothesis at the 5% significance level.

### Test Hypothesis at a Different Significance Level

Load the sample data. Create a vector containing the third column of the stock returns data.

x = stocks(:,3);

Test the null hypothesis that the sample data are from a population with mean equal to zero at the 1% significance level.

h = ttest(x,0,'Alpha',0.01)
h =
0

The returned value h = 0 indicates that ttest does not reject the null hypothesis at the 1% significance level.

### Paired-Sample t-Test

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students' grades on two exams.

Test the null hypothesis that the pairwise difference between data vectors x and y has a mean equal to zero.

[h,p] = ttest(x,y)
h =
0

p =
0.9805

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the default 5% significance level.

### Paired-Sample t-Test at a Different Significance Level

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students' grades on two exams.

Test the null hypothesis that the pairwise difference between data vectors x and y has a mean equal to zero at the 1% significance level.

[h,p] = ttest(x,y,'Alpha',0.01)
h =
0

p =
0.9805

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the 1% significance level.

### Test for a Hypothesized Mean

Load the sample data. Create a vector containing the first column of the students' exam grades data.

Test the null hypothesis that sample data comes from a distribution with mean m = 75.

h = ttest(x,75)
h =
0

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the 5% significance level.

### One-Sided Hypothesis Test

Load the sample data. Create a vector containing the first column of the students' exam grades data.

Test the null hypothesis that the data comes from a population with mean equal to 65, against the alternative that the mean is greater than 65.

h = ttest(x,65,'Tail','right')
h =
1

The returned value of h = 1 indicates that ttest rejects the null hypothesis at the 5% significance level, in favor of the alternate hypothesis that the data comes from a population with a mean greater than 65.

## Input Arguments

collapse all

### x — Sample datavector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array. ttest performs a separate t-test along each column and returns a vector of results. If y sample data is specified, x and y must be the same size.

Data Types: single | double

### y — Sample datavector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array. If y sample data is specified, x and y must be the same size.

Data Types: single | double

### m — Hypothesized population mean0 (default) | scalar value

Hypothesized population mean, specified as a scalar value.

Data Types: single | double

### Name-Value Pair 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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Tail','right','Alpha',0.01 conducts a right-tailed hypothesis test at the 1% significance level.

### 'Alpha' — Significance level0.05 (default) | scalar value in the range (0,1)

Significance level of the hypothesis test, specified as the comma-separated pair consisting of 'Alpha' and a scalar value in the range (0,1).

Example: 'Alpha',0.01

Data Types: single | double

### 'Dim' — Dimensionfirst nonsingleton dimension (default) | positive integer value

Dimension of the input matrix along which to test the means, specified as the comma-separated pair consisting of 'Dim' and a positive integer value. For example, specifying 'Dim',1 tests the column means, while 'Dim',2 tests the row means.

Example: 'Dim',2

Data Types: single | double

### 'Tail' — Type of alternative hypothesis'both' (default) | 'right' | 'left'

Type of alternative hypothesis to evaluate, specified as the comma-separated pair consisting of 'Tail' and one of the following.

 'both' Test the alternative hypothesis that the population mean is not m. 'right' Test the alternative hypothesis that the population mean is greater than m. 'left' Test the alternative hypothesis that the population mean is less than m.

Example: 'Tail','right'

## Output Arguments

collapse all

### h — Hypothesis test result1 | 0

Hypothesis test result, returned as a logical value.

• 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-valuescalar value in the range [0,1]

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 intervalvector

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

### stats — Test statisticsstructure

Test statistics, returned as a structure containing the following:

• tstat — Value of the test statistic.

• df — Degrees of freedom of the test.

• sd — Estimated population standard deviation. For a paired t-test, this is the standard deviation of x – y.

collapse all

### One-Sample t-Test

The one-sample t-test is a parametric test of the location parameter when the population standard deviation is unknown.

The test statistic is

$t=\frac{\overline{x}-\mu }{s/\sqrt{n}},$

where $\overline{x}$ is the sample mean, μ is the hypothesized population mean, s is the sample standard deviation, and n is the sample size. Under the null hypothesis, the test statistic has Student's t distribution with n – 1 degrees of freedom.

### Multidimensional Array

A multidimensional array has more than two dimensions. For example, if x is a 1-by-3-by-4 array, then x is a three-dimensional array.

### First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1. For example, if x is a 1-by-2-by-3-by-4 array, then the second dimension is the first nonsingleton dimension of x.