  # Chi-square distribution

This program calculates the tail-end and percentile values for points on a Chi-square (X2) distribution curve.

 You must provide the value of X2 and the degrees of freedom. No special instruction or Matlab toolbox is used. Since the summation in the calculation of Z cannot actually extend to infinity, we stop summation when the next term is less than a chosen level of precision. Our precision is limited to approx. 1×10-7.

The Matlab program to accomplish this, is:

% Clears screen and variables in workspace
clc; clear

% Asks the user for the relevant input
v = input('Degrees of freedom: ');
w = input(
'Chi-square: ');

% Calculates the denominator product
r = 1;
for i = v : -2 : 2
r = r*i;

end
% Calculates the numerator product
k = w^(floor((v+1)/2))*exp(-w/2)/r;

% If degrees of freedom are odd, then uses the pi factor
if floor(v/2) == v/2
j = 1;

else
j = sqrt(2/(w*pi));
end
l = 1;
m = 1;
v = v + 2;
m = m*w/v;

% Summation factor
while m >= 1e-7
l = l + m;
v = v + 2;
m = m*w/v;

end

% Displays results
str = ['Tail end value: ' num2str(1-j*k*l)];
disp(str)
str = [
'Percentile: ' num2str(j*k*l)];
disp(str)

### Example - Calculate tail-end and percentile

A X2 statistic for a given study was computed to be 2.571108 with one degree of freedom. What are the tail-end and percentile values?

Running the Matlab program above, we enter and get:

Degrees of freedom: 1
Chi-square: 2.571108
Tail-end value: 0.10883
Percentile: 0.89117

From 'Chi-square' to home

From 'Chi-square' to 'Probability and Stats'  