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.

Create a vector from the first column of the data matrix,
which contains the lifetime in hours of the lightbulbs.

x = lightbulb(:,1);

Test the null hypothesis that the data in x comes
from a population with a Weibull distribution. Use fitdist to
create a probability distribution object with A and B parameters
estimated from the data.

pd = fitdist(x,'Weibull');
h = chi2gof(x,'CDF',pd)

h =
1

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

h =
0
p =
0.4654
st =
chi2stat: 2.5550
df: 3
edges: [1x6 double]
O: [6 16 10 12 6]
E: [7.0429 13.8041 13.5280 8.8383 6.0284]

The returned value h = 0 indicates that chi2gof does
not reject the null hypothesis at the default 5% significance level.
The vector E contains the expected counts for each
bin under the null hypothesis, and O contains the
observed counts for each bin.

Sample data for the hypothesis test, specified as a vector.

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: 'NBins',8,'Alpha',0.01 pools the
data into eight bins and conducts the hypothesis test at the 1% significance
level.

Number of bins to use for the data pooling, specified as the
comma-separated pair consisting of 'NBins' and
a positive integer value. If you specify a value for NBins,
do not specify a value for Ctrs or Edges.

Bin centers, specified as the comma-separated pair consisting
of 'Ctrs' and a vector of center values for each
bin. If you specify a value for Ctrs, do not specify
a value for NBins or Edges.

Bin edges, specified as the comma-separated pair consisting
of 'Edges' and a vector of edge values for each
bin. If you specify a value for Edges, do not specify
a value for NBins or Ctrs.

The cdf of the hypothesized distribution, specified as the comma-separated
pair consisting of 'CDF' and a probability distribution
object, function handle, or cell array.

If CDF is a probability distribution
object, the degrees of freedom account for whether you estimate the
parameters using fitdist or specify
them using makedist.

If CDF is a function handle, the
distribution function must take x as its only argument.

If CDF is a cell array, the first
element must be a function handle, and the remaining elements must
be parameter values, one per cell. The function must take x as
its first argument, and the other parameters in the array as later
arguments.

If you specify a value for CDF, do not specify
a value for Expected.

Expected counts for each bin, specified as the comma-separated
pair of 'Expected' and a vector of nonnegative
values. If Expected depends on estimated parameters,
use NParams to ensure that chi2gof correctly
calculates the degrees of freedom. If you specify a value for Expected,
do not specify a value for CDF.

Number of estimated parameters used to describe the null distribution,
specified as the comma-separated pair consisting of 'NParams' and
a positive integer value. This value adjusts the degrees of freedom
of the test based on the number of estimated parameters used to compute
the cdf or expected counts.

The default value for NParams depends on
how you specify the null distribution:

If you specify CDF as a probability
distribution object, NParams is equal to the number
of estimated parameters used to create the object.

If you specify CDF as a function
name or handle, the default value of NParams is 0.

If you specify CDF as a cell
array, the default value of NParams is the number
of parameters in the array.

If you specify Expected, the
default value of NParams is 0.

Minimum expected count per bin, specified as the comma-separated
pair consisting of 'EMin' and a nonnegative integer
value. If the bin at the extreme end of either tail has an expected
value less than EMin, it is combined with a neighboring
bin until the count in each extreme bin is at least 5. If any interior
bins have a count less than 5, chi2gof displays
a warning, but does not combine the interior bins. In that case, you
should use fewer bins, or provide bin centers or edges, to increase
the expected counts in all bins. Specify EMin as 0 to
prevent the combining of bins.

Frequency of data values, specified as the comma-separated pair
consisting of 'Frequency' and a vector of nonnegative
integer values that is the same length as the vector x.

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.

The chi-square goodness-of-fit test determines
if a data sample comes from a specified probability distribution,
with parameters estimated from the data.

The test groups the data into bins, calculating the observed
and expected counts for those bins, and computing the chi-square test
statistic

where O_{i} are
the observed counts and E_{i} are
the expected counts based on the hypothesized distribution. The test
statistic has an approximate chi-square distribution when the counts
are sufficiently large.

chi2gof compares the value of the test statistic
to a chi-square distribution with degrees of freedom equal to nbins -
1 - nparams, where nbins is
the number of bins used for the data pooling and nparams is
the number of estimated parameters used to determine the expected
counts. If there are not enough degrees of freedom to conduct the
test, chi2gof returns the p-value
as NaN.