logo for matrixlab-examples.com
[?] Subscribe To This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Subscribe with Bloglines


Home
Matrixmania Blog
Contact
-> Sitemap <-
Matlab Books
Quick Matlab Guide
Matlab Tutorials
Matlab Examples
Matlab Flow Control
Boolean Algebra
Linear Algebra
Matlab 2D Plots
Matlab 3D Plots
Matlab GUI
Matlab Cookbook I
Matlab Cookbook II
Probability and Stats
Forums and Help
Relevant Links
Fun!
Your own Website?
Terms/Policies
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.

chi-square distribution curve
Chi-square distribution curve

The unshaded area represents the tail-end value of X2. The shaded area represents the percentile. 

The X2 distribution function is calculated using the following formulas: 

with v odd,

chi-square for odd values

with v even,

chi-square for even values

where

v = degrees of freedom

summation to calculate chi-square distribution

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:


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'


footer for chi-square page