The chi square distribution, written as is a continuous probability distribution. Here, I will introduce the Chi Square by code example from a SAS point of view. A with degrees of freedom has the Probability Density Function

Here, denotes the Gamma Function, of which the is a special case. In the opposite case, for . Let be independent, standard normal variables. Then their sum of squares follows a distribution with degrees of freedom, written as . For a thorough introduction to the chi square distribution, watch the Chi Square Distribution Introduction at Khan Academy.

To the right, I have plotted probability density functions and their corresponding cumulative densities for three different distributions. I have plotted them for degrees of freedom equal to 1, 2 and 3. Take a look at the Chi Square Probability Density Function with 1 degree of freedom (the blue line). It obviously has a very high probability close to zero. This makes sense because if you draw a single sample from a standard normal distribution and square it, it will likely be close to zero. You can download the SAS code creating these plots here.

Chi Square Distribution SAS Code Example

Below, I have written a SAS code example, that lets you play around with the degrees of freedom in the distribution and plots the Chi Squared probability density function corresponding to the degrees of freedom you set. I encourage you to set to different values and look what happens with the PDF. You know from above what happens when is small. What happens to the density if is very large? Try it out yourself.

%let k = 3;
/* Chi Squared PDF Curves */data ChiSquared_PDF;
dox=0 to 8by0.01;
ChiSquared_PDF = pdf('chisquare', x, &k);
output;
end;
run;
/* Draw PDF Curve */title"Chi Squared Density For k = &k";
proc sgplotdata=ChiSquared_PDF noautolegend;
series x=x y=ChiSquared_PDF / lineattrs = (thickness=2) legendlabel="Chi Squared PDF";
keylegend "Chi Squared PDF" / position=NE location=inside across=1 noborder valueattrs=(Size=12 Weight=Bold)
titleattrs = (Size=12 Weight=Bold);
xaxis label='x' labelattrs = (size=12 weight=Bold);
yaxis display=(nolabel)label='PDF' labelattrs = (size=12 weight=Bold);
run;
title;

%let k = 3; /* Chi Squared PDF Curves */ data ChiSquared_PDF; do x=0 to 8 by 0.01; ChiSquared_PDF = pdf('chisquare', x, &k); output; end; run; /* Draw PDF Curve */ title "Chi Squared Density For k = &k"; proc sgplot data=ChiSquared_PDF noautolegend; series x=x y=ChiSquared_PDF / lineattrs = (thickness=2) legendlabel="Chi Squared PDF"; keylegend "Chi Squared PDF" / position=NE location=inside across=1 noborder valueattrs=(Size=12 Weight=Bold) titleattrs = (Size=12 Weight=Bold); xaxis label='x' labelattrs = (size=12 weight=Bold); yaxis display=(nolabel) label='PDF' labelattrs = (size=12 weight=Bold); run; title;