vartestn(x) returns
a summary table of statistics and a box plot for a Bartlett test of
the null hypothesis that the columns of data vector x come
from normal distributions with the same variance. The alternative
hypothesis is that not all columns of data have the same variance.

vartestn(x,Name,Value) returns
a summary table of statistics and a box plot for a test of unequal
variances with additional options specified by one or more name-value
pair arguments. For example, you can specify a different type of hypothesis
test or change the display settings for the test results.

vartestn(x,group) returns
a summary table of statistics and a box plot for a Bartlett test of
the null hypothesis that the data in each categorical group comes
from normal distributions with the same variance. The alternative
hypothesis is that not all groups have the same variance.

vartestn(x,group,Name,Value) returns
a summary table of statistics and a box plot for a test of unequal
variances with additional options specified by one or more name-value
pair arguments. For example, you can specify a different type of hypothesis
test or change the display settings for the test results.

Test the null hypothesis that the variances are equal
across the five columns of data in the students' exam grades
matrix, grades.

vartestn(grades)

The low p-value, p = 0,
indicates that vartestn rejects the null hypothesis
that the variances are equal across all five columns, in favor of
the alternative hypothesis that at least one column has a different
variance.

Test the null hypothesis that the variances in miles per
gallon (MPG) are equal across different model years.

vartestn(MPG,Model_Year)

The high p-value, p = 0.83269,
indicates that vartestn does not reject the null
hypothesis that the variances in miles per gallon (MPG)
are equal across different model years.

Use Levene's test to test the null hypothesis that
the variances in miles per gallon (MPG) are equal
across different model years.

p = vartestn(MPG,Model_Year,'TestType','LeveneAbsolute')

p =
0.6320

The high p-value, p = 0.63195,
indicates that vartestn does not reject the null
hypothesis that the variances in miles per gallon (MPG)
are equal across different model years.

Test the null hypothesis that the variances are equal
across the five columns of data in the students' exam grades
matrix, grades, using the Brown-Forsythe test.
Suppress the display of the summary table of statistics and the box
plot.

The low p-value, p = 1.3121e-06,
indicates that vartestn rejects the null hypothesis
that the variances are equal across all five columns, in favor of
the alternative hypothesis that at least one column has a different
variance.

Sample data, specified as a matrix or vector. If a grouping
variable group is specified, x must
be a vector. If a grouping variable is not specified, x must
be a matrix. In either case, vartestn treats NaN values
as missing values and ignores them.

Grouping variable, specified as a categorical array, logical
or numeric vector, or cell array of strings with one row for each
element of x. Each unique value in a grouping
variable defines a group.

For example, if Gender is a cell array of
strings with values 'Male' and 'Female',
you can use Gender as a grouping variable to test
your data by gender.

Example: Gender

Data Types: single | double | logical | cell | char

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: 'TestType','BrownForsythe','Alpha',0.01,'Display','off' specifies
a Brown-Forsythe test at the 1% significance level, and omits the
plot of the results.

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 variances,
specified as the comma-separated pair consisting of 'Dim' and
a positive integer value. For example, specifying 'Dim',1 tests
the column variances, while 'Dim',2 tests the row
variances.

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.

Bartlett's test is used to test whether
multiple data samples have equal variances, against the alternative
that at least two of the data samples do not have equal variances.

The test statistic is

where is the variance of the ith
group, N is the total sample size, N_{i} is
the sample size of the ith group, k is
the number of groups, and is
the pooled variance. The pooled variance is defined as

The test statistic has a chi-square distribution with k –
1 degrees of freedom under the null hypothesis.

Bartlett's test is sensitive to departures from normality.
If your data comes from a nonnormal distribution, Levene's
test could provide a more accurate result.

The Levene, Brown-Forsythe, and O'Brien
tests are used to test if multiple data samples have equal variances,
against the alternative that at least two of the data samples do not
have equal variances.

The test statistic is

where N_{i} is
the sample size of the ith group, and k is
the number of groups. Depending on the type of test specified with
the TestType name-value pair arguments, Z_{ij} can
have one of four definitions:

If you specify LeveneAbsolute, vartestn uses , where is the mean of the ith
subgroup.

If you specify LeveneQuadratic, vartestn uses , where is the mean of the ith
subgroup.

If you specify BrownForsythe, vartestn uses , where is the median of the ith
subgroup.

If you specify OBrien, vartestn uses

where n_{i} is
the size of the ith group, σ_{i}^{2} is
its sample variance.

In all cases, the test statistic has an F-distribution
with k – 1 numerator degrees of freedom,
and N – k denominator
degrees of freedom.

The Levene, Brown-Forsythe, and O'Brien tests are less
sensitive to departures from normality than Bartlett's test,
so they are useful alternatives if you suspect the samples come from
nonnormal distributions.