# Chi Square Distribution

 Probability Home Chi Square Distribution

The distribution function of a random variable X distributed according to the chi-square distribution with n ≥ 1 degrees of freedom is a continuous function, F(x) = P(X < x), given by
F(x) =
 0 for x < 0 [ 1 / ( 2n/2 Γ(n/2) ) ] ∫0xt (n-2) / 2e- x/2 dt for 0 ≤ x

where Γ( ) is the gamma function.

The corresponding probability density function, f(x) = dF(x)/dx, is
f(x) =
 0 for x < 0 [ 1 / ( 2n/2 Γ(n/2) ) ] x (n-2) / 2e- x/2 for 0 ≤ x

For a large number of degrees of freedom, n >> 1, the distribution of
 [ X / n - ( 1 - 2 / ( 9n ) ) ] / √[ 2 / ( 9n ) ]
approaches that of a standard normal distribution. The problem of deciding how large n should be depends on the nature of the problem.

#### Function List

• double Chi_Square_Distribution( double x, int n )

This function returns F(x) where F(x) is described above.

• double Chi_Square_Density( double x, int n )

This function returns f(x) where f(x) is described above.

• void Chi_Square_Distribution_Tables( int n, double start, double delta, int nsteps, double *density, double* distribution_function )

This function returns f(x) where f(x) is described above in the array density, i.e. density[i] = f(xi) where xi = start + i delta, i = 0,...,nsteps and returns F(x) where F(x) is described above in the array distribution_function, i.e. distribution_function[i] = F(xi) where xi = start + i delta, i = 0,...,nsteps. Note that density must be declared double density[N] where N ≥ nsteps + 1 in the calling routine and similarly the distribution_function must be declared double distribution_function[N] where
N ≥ nsteps + 1 in the calling routine.

• double Chi_Square_Distribution_Large_dof(double x, int n)

This function returns the asymptotic distribution of F(x) for a large number of degrees of freedom, n.

#### Source Code

C source code is available for these routines:
• The file, chi_square_distribution.c, contains the source code for the function Chi_Square_Distribution().

Dependencies: In addition to the the file chi_square_distribution.c, the function Chi_Square_Distribution() requires the following files:

• The file, chi_square_density.c, contains the source code for the function Chi_Square_Density().

Dependencies: In addition to the the file chi_square_density.c, the function Chi_Square_Density() requires the following files:

• The file, chi_square_dist_tables.c, contains the source code for the function Chi_Square_Distribution_Tables().

Dependencies: In addition to the the file chi_square_dist_tables.c, the function Chi_Square_Distribution_Tables() requires the following files:

• The file chi_square_distribution.c and its dependencies given above.
• The file chi_square_density.c and its dependencies given above.

• The file, chi_square_distribution_large_dof.c, contains the source code for the function Chi_Square_Distribution_Large_dof().

Dependencies: In addition to the chi_square_distribution_large_dof.c, the function Chi_Square_Distribution_Large_dof() requires the following file:

#### C Test Code, Test Results, and Build Shell Script

• The file, testchisqbook.c, contains a test program of Chi_Square_Distribution( ) and Chi_Square_Density() written in C. This test program requires the files chi_square_distribution.c, chi_square_density.c, and their dependencies listed above.

• The file, ChiSquareDistributionBook.txt, contains the results of the test program testchisqbook.c.

• The file, testchisqbook.sh, contains the shell script used to compile, link, and execute the test program testchisqbook.c.

• The file, testchisqdist.c, contains a test program of Chi_Square_Distribution( ) written in C. This test program requires the file chi_square_distribution.c and its dependencies listed above.

• The file, ChiSquareDistributionTest.txt, contains the results of the test program testchisqdist.c.

• The file, testchisqdist.sh, contains the shell script used to compile, link, and execute the test program testchisqdist.c.

• The file, testchisqdensity.c, contains a test program of Chi_Square_Density( ) written in C. This test program requires the file chi_square_density.c and its dependencies listed above.

• The file, ChiSquareDensityTest.txt, contains the results of the test program testchisqdensity.c.

• The file, testchisqdensity.sh, contains the shell script used to compile, link, and execute the test program testchisqdensity.c.

• The file, testchisqtables.c, contains a test program of Chi_Square_Distribution_Tables( ) written in C. This test program requires the files chi_square_distribution.c, chi_square_density, and their dependencies listed above.

• The file, ChiSquareTablesTest.txt, contains the results of the test program testchisqtables.c.

• The file, testchisqtables.sh, contains the shell script used to compile, link, and execute the test program testchisqtables.c.

• The file, testchisqasymptotic.c, contains a test program of Chi_Square_Distribution_Large_dof( ) written in C. This test program requires the files chi_square_distribution_large_dof.c, chi_square_distribution.c and their dependencies listed above.

• The file, ChiSquareDistributionAsymptoticTest.txt, contains the results of the test program testchisqasymptotic.c.

• The file, testchisqasymptotic.sh, contains the shell script used to compile, link, and execute the test program testchisqasymptotic.c.