Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

..Help Plotting a Chi-Square Distribution..

192 views
Skip to first unread message

Sharon Elizabeth Daliere

unread,
Apr 29, 2003, 10:14:13 AM4/29/03
to
Ok this seems to be a simple one (though I can't seem to figure it
out). Say I have 10,000 random numbers with 9 degrees of freedom. How
do I generate the Chi-Square Distribution?


There is an example in the Matlab help files:


x = 0:0.2:15;
y = chi2pdf(x,4);
plot(x,y)


This routine generates a nice curve. How come I get a weird output
when I substitute my random numbers for the variable x? example:


x = randint(1,10000,[1 10]);
x = x./10;


%so that x is a vector of 10,000 random numbers btween 0.1 and 1. Any
help I can get will be seriously appreciated! Thanks.

Ken Davis

unread,
Apr 29, 2003, 12:04:54 PM4/29/03
to
"Sharon Elizabeth Daliere" <eliz...@yahoo.com> wrote in message
news:eebd1...@WebX.raydaftYaTP...

Hello,

If you want an estimate of the pdf of chi-squared variates, generate
chi-squared variates and then histogram them. AFAIK, chi2pdf does not do
this.

Ken


Peter Perkins

unread,
Apr 29, 2003, 1:02:03 PM4/29/03
to
Hi Sharon -

> Ok this seems to be a simple one (though I can't seem to figure it
> out). Say I have 10,000 random numbers with 9 degrees of freedom. How
> do I generate the Chi-Square Distribution?

Do you need to fit a distribution to those data, or are you trying to plot the
data, or plot the PDF, or ... ?

> x = 0:0.2:15;
> y = chi2pdf(x,4);
> plot(x,y)
>
> This routine generates a nice curve. How come I get a weird output
> when I substitute my random numbers for the variable x? example:

The x's in the above example are just a grid of points on which to plot the
probability density function. If your object is to make a plot of the
chi-squared PDF, there's not really any point in choosing them randomly, and
if you did choose them randomly, you'd get a pretty funny-looking plot unless
you sorted the x values.

> x = randint(1,10000,[1 10]);
> x = x./10;

These are not chi-squared random values, they are uniform. You could sort
them, then plot, as in

x = sort(x)
plot(x,chi2pdf(x,4))

but as I mentioned above, there's no real point in choosing the x values for a
PDF plot randomly. If you know your data are from a chi-squared with 4 d.f.,
and you want to plot your data along with a PDF, you might like the following:

n = 10000;
df = 4;
x = chi2rnd(df,n,1);

binwidth = 1;
bins = 0:binwidth:15;
binctrs = bins(1:end-1) + binwidth./2;
count = histc(x,bins); count = count(1:end-1);
xgrid = 0:.1:15;
plot(xgrid,chi2pdf(xgrid,df),'-');
hold on
h = bar(binctrs,count./(n.*binwidth),1);
set(h,'FaceColor',[.9 .9 .9]);
hold off

Hope this helps.

- Peter Perkins
The MathWorks, Inc.

Amol Naik

unread,
Jun 2, 2009, 4:00:03 AM6/2/09
to
Peter Perkins <pper...@RemoveThis.mathworks.com> wrote in message <b8mb6b$pel$1...@ginger.mathworks.com>...
Thanks Peter,

This is exactly what I needed...Good Info!

0 new messages