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.
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
> 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.
This is exactly what I needed...Good Info!