logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

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;

% 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;

    j = sqrt(2/(w*pi));
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;


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

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'

Chi-Square Calculator Online

Normal distribution

Binomial distribution

Poisson distribution



Random numbers

footer for chi-square page