## chisqbounds

Return the boundaries containing 100% p points drawn from a chi-squared distribution with r degrees of freedom. The region can be 1-sided, such that 100% p values are less than the upper bound or 2-sided, such that 100% p values are between the lower and upper bounds.

bounds = chisqbounds(r, p)
bounds = chisqbounds(r, p, sidedness)

## Inputs

r Degrees of freedom (positive integer) (e.g., 2) Fraction of data within bounds (e.g., 0.95 for 95%) 1 for 1-sided (default) or 2 for 2-sided region

## Outputs

bounds Bounds containing p fraction of data (e.g., [0 5.99] for 1-sided or [0.051 7.38] for 2-sided

## Example

We'll make sure that the theoretical and empircal bounds match up reasonably well for some random draws.

% Generate 10,000 samples from a 2 degree of freedom chi^2 distribution.
n = 10000;
x = sum(randn(2, n).^2, 1);

% Get the theoretical 95% 1-sided bound.
b = chisqbounds(2, 0.95, 1);

% See what percentage of the data are within bounds.
sum(x < b(2)) / n

% Get the theoretical 95% 2-sided bounds.
b = chisqbounds(2, 0.95, 2);

% See what percentage of the data are within bounds.
sum(b(1) < x & x < b(2)) / n
ans =
0.9477
ans =
0.9472