*z*-test

collapse all in page

## Syntax

`h = ztest(x,m,sigma)`

`h= ztest(x,m,sigma,Name,Value)`

`[h,p] =ztest(___)`

`[h,p,ci,zval]= ztest(___)`

## Description

example

`h = ztest(x,m,sigma)`

returnsa test decision for the null hypothesis that the data in the vector `x`

comesfrom a normal distribution with mean `m`

and astandard deviation `sigma`

, using the z-test.The alternative hypothesis is that the mean is not `m`

.The result `h`

is `1`

if the testrejects the null hypothesis at the 5% significance level, and `0`

otherwise.

example

`h= ztest(x,m,sigma,Name,Value)`

returnsa test decision for the *z*-test with additionaloptions 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] =ztest(___)`

also returns the *p*-valueof the test, using any of the input arguments from previous syntaxes.

example

`[h,p,ci,zval]= ztest(___)`

also returns the confidence intervalof the population mean, `ci`

, and the value ofthe test statistic, `zval`

.

## Examples

collapse all

*z*-Test for a Hypothesized Mean

Open Live Script

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

`load examgradesx = grades(:,1);`

Test the null hypothesis that the data comes from a normal distribution with mean `m = 75`

and standard deviation `sigma = 10`

.

[h,p,ci,zval] = ztest(x,75,10)

h = 0

`ci = `*2×1* 73.2191 76.7975

zval = 0.0091

The returned value of `h = 0`

indicates that `ztest`

does not reject the null hypothesis at the default 5% significance level.

### One-Sided *z*-Test

Open Live Script

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

`load examgradesx = grades(:,1);`

Plot a histogram of the exam grades data and fit a normal density function.

histfit(x)xlabel("Grade")ylabel("Frequency")

Test the null hypothesis that the data comes from a normal distribution with mean `m = 65`

and standard deviation `sigma = 10`

, against the alternative hypothesis that the mean is greater than 65.

[h,~,~,zval] = ztest(x,65,10,"Tail","right")

h = 1

zval = 10.9636

The returned value of `h = 1`

indicates that `ztest`

rejects the null hypothesis at the default 5% significance level, in favor of the alternate hypothesis that the population mean is greater than 65.

Plot the standard normal distribution, the returned *z*-statistic, and the critical *z*-value. Calculate the critical *z*-value for the default confidence level of 95% by using `norminv`

.

k = linspace(-15,15,300);y = normpdf(k);zvalpdf = normpdf(zval);zcrit = norminv(0.95);plot(k,y);hold onscatter(zval,zvalpdf,"filled")xline(zcrit,"--")legend(["Standard Normal pdf","z-Statistic", ... "Critical Cutoff"])

The orange dot represents the *z*-statistic and is located to the right of the dashed black line that represents the critical *z*-value.

## Input Arguments

collapse all

`x`

— Sample data

vector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensionalarray.

If

`x`

is specified as a vector,`ztest`

returnsa single value for each output argument.If

`x`

is specified as a matrix,`ztest`

performsa separate*z*-test along each column of`x`

andreturns a vector of results.If

`x`

is specified as a multidimensional array,`ztest`

worksalong the firstnonsingleton dimension of`x`

.

In all cases, `ztest`

treats `NaN`

valuesas missing data and ignores them.

**Data Types: **`single`

| `double`

`m`

— Hypothesized mean

scalar value

Hypothesized mean, specified as a scalar value.

**Data Types: **`single`

| `double`

`sigma`

— Population standard deviation

scalar value

Population standard deviation, specified as a scalar value.

**Data Types: **`single`

| `double`

### Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`

, where `Name`

is the argument name and `Value`

is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

* Before R2021a, use commas to separate each name and value, and enclose* `Name`

*in quotes.*

**Example: **`'Tail','right','Alpha',0.01`

specifiesa right-tailed hypothesis test at the 1% significance level.

`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:

`'both'`

— Test against the alternative hypothesis that the population mean is not`m`

.`'right'`

— Test against the alternative hypothesis that the population mean is greater than`m`

.`'left'`

— Test against the alternative hypothesis that the population mean is less than`m`

.

`ztest`

tests the null hypothesis that the population mean is `m`

against the specified alternative hypothesis.

**Example: **`'Tail','right'`

## Output Arguments

collapse all

`zval`

— Test statistic

nonnegative scalar value

Test statistic, returned as a nonnegative scalar value.

## More About

collapse all

*z*-Test

The *z*-test is a parametrichypothesis test used to determine whether a sample data set comesfrom a population with a particular mean. The test assumes that thesample data comes from a population with a normal distribution anda known standard deviation.

The test statistic is

$$z=\frac{\overline{x}-\mu}{\sigma /\sqrt{n}},$$

where $$\overline{x}$$ is the sample mean, `μ`

isthe population mean, σ is the population standard deviation,and *n* is the sample size. Under the null hypothesis,the test statistic has a standard normal distribution.

### Multidimensional Array

A multidimensional array has more than twodimensions. For example, if `x`

is a 1-by-3-by-4array, then `x`

is a three-dimensional array.

### First Nonsingleton Dimension

The first nonsingleton dimension is the firstdimension of an array whose size is not equal to 1. For example, if `x`

isa 1-by-2-by-3-by-4 array, then the second dimension is the first nonsingletondimension of `x`

.

## Tips

Use sampsizepwr tocalculate:

The sample size that corresponds to specified powerand parameter values;

The power achieved for a particular sample size, giventhe true parameter value;

The parameter value detectable with the specifiedsample size and power.

## Extended Capabilities

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced before R2006a**

## See Also

ttest | ttest2 | sampsizepwr

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom (English)

Contact your local office