# ansaribradley

Ansari-Bradley test

## Syntax

## Description

returns
a test decision for the null hypothesis that the data in vectors `h`

= ansaribradley(`x`

,`y`

)`x`

and `y`

comes
from the same distribution, using the Ansari-Bradley test. The alternative
hypothesis is that the data in `x`

and `y`

comes
from distributions with the same median and shape but different dispersions
(e.g., variances). The result `h`

is `1`

if
the test rejects the null hypothesis at the 5% significance level,
or `0`

otherwise.

returns
a test decision for the Ansari-Bradley test with additional options
specified by one or more name-value pair arguments. For example, you
can change the significance level, conduct a one-sided test, or use
a normal approximation to calculate the value of the test statistic.`h`

= ansaribradley(`x`

,`y`

,`Name,Value`

)

## Examples

### Ansari-Bradley Test for Equal Variances

Load the sample data. Create data vectors of miles per gallon (`MPG`

) measurements for the model years 1982 and 1976.

```
load carsmall
x = MPG(Model_Year==82);
y = MPG(Model_Year==76);
```

Test the null hypothesis that the miles per gallon measured in cars from 1982 and 1976 have equal variances.

[h,p,stats] = ansaribradley(x,y)

h = 0

p = 0.8426

`stats = `*struct with fields:*
W: 526.9000
Wstar: 0.1986

The returned value of `h = 0`

indicates that `ansaribradley`

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

### Ansari-Bradley One-Sided Hypothesis Test

Load the sample data. Create data vectors of miles per gallon (`MPG`

) measurements for the model years 1982 and 1976.

```
load carsmall
x = MPG(Model_Year==82);
y = MPG(Model_Year==76);
```

Test the null hypothesis that the miles per gallon measured in cars from 1982 and 1976 have equal variances, against the alternative hypothesis that the variance of cars from 1982 is greater than that of cars from 1976.

[h,p,stats] = ansaribradley(x,y,'Tail','right')

h = 0

p = 0.5787

`stats = `*struct with fields:*
W: 526.9000
Wstar: 0.1986

The returned value of `h = 0`

indicates that `ansaribradley`

does not reject the null hypothesis that the variance in miles per gallon is the same for the two model years, when the alternative is that the variance of cars from 1982 is greater than that of cars from 1976.

## Input Arguments

`x`

— Sample data

vector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array.

If

`x`

and`y`

are specified as vectors, they do not need to be the same length.If

`x`

and`y`

are specified as matrices, they must have the same number of columns.`ansaribradley`

performs separate tests along each column and returns a vector of results.If

`x`

and`y`

are specified as multidimensional arrays,`ansaribradley`

works along the first nonsingleton dimension.`x`

and`y`

must have the same size along all remaining dimensions.

**Data Types: **`single`

| `double`

`y`

— Sample data

vector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array.

If

`x`

and`y`

are specified as vectors, they do not need to be the same length.If

`x`

and`y`

are specified as matrices, they must have the same number of columns.`ansaribradley`

performs separate tests along each column and returns a vector of results.If

`x`

and`y`

are specified as multidimensional arrays,`ansaribradley`

works along the first nonsingleton dimension.`x`

and`y`

must have the same size along all remaining dimensions.

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

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

`Alpha`

— Significance level

`0.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`

— Dimension

first 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) | `'left'`

| `'right'`

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 dispersion parameters
of `x` and `y` are not equal. |

`'right'` | Test the alternative hypothesis that the dispersion parameter
of `x` is greater than that of `y` . |

`'left'` | Test the alternative hypothesis that the dispersion parameter
of `x` is less than that of `y` . |

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

`Method`

— Computation method

`'exact'`

| `'approximate'`

Computation method for the test statistic, specified as the
comma-separated pair consisting of `'Method'`

and
one of the following.

`'exact'` | Compute `p` using an exact calculation of
the distribution of the test statistic `W` . This
is the default if n, the total number of rows in `x` and `y` ,
is 25 or less. Note that n is computed before any `NaN` values
(representing missing data) are removed. |

`'approximate'` | Compute `p` using a normal approximation
for the statistic `W*` . This is the default if n,
the total number of rows in `x` and `y` ,
is greater than 25. |

**Example: **`'Method','exact'`

## Output Arguments

`h`

— Hypothesis test result

`1`

| `0`

Hypothesis test result, returned as `1`

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

scalar 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.

`stats`

— Test statistics

structure

Test statistics for the Ansari-Bradley test, returned as a structure containing:

`W`

— Value of the test statistic, which is the sum of the Ansari-Bradley ranks for the`x`

sample.`Wstar`

— Approximate normal statistic`W*`

.

## More About

### Ansari-Bradley Test

The Ansari-Bradley test is a nonparametric
alternative to the two-sample *F*-test of equal variances.
It does not require the assumption that `x`

and `y`

come
from normal distributions. The dispersion of a distribution is generally
measured by its variance or standard deviation, but the Ansari-Bradley
test can be used with samples from distributions that do not have
finite variances.

This test requires that the samples have equal medians. Under
that assumption, and if the distributions of the samples are continuous
and identical, the test is independent of the distributions. If the
samples do not have the same medians, the results can be misleading.
In that case, Ansari and Bradley recommend subtracting the median,
but then the distribution of the resulting test under the null hypothesis
is no longer independent of the common distribution of `x`

and `y`

.
If you want to perform the tests with medians subtracted, you should
subtract the medians from `x`

and `y`

before
calling `ansaribradley`

.

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

.

## Version History

**Introduced before R2006a**

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

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### 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
- United Kingdom (English)