Asked by John
on 21 Dec 2011

Hi there,

I have question regarding the arguments X and V in the Chi-square probability density function.

http://www.mathworks.co.uk/help/toolbox/stats/chi2pdf.html

Say for example I have a column vector, D with 1000 distance values.

Is the degrees of freedom, V, calculated by 1000-1 = 999?

So would the function be

Y = chi2pdf(D,999)

I am trying to achieve a chart like this, where the red line is the Chi-square probability density function

http://img713.imageshack.us/img713/8279/distancechart.jpg

Many thanks for your help

*No products are associated with this question.*

Answer by bym
on 21 Dec 2011

no, degrees of freedom would be 1000. You are not calculating another statistic with the data ('using up a DOF') before calling the chi2pdf function

John
on 22 Dec 2011

Thanks for replying,

I still don't understand how to use the function though.

Are you saying that if I a have a column vector, D, containing 1000 values, then I also need another column vector,V, containing 1000 values that are all 1000 - because it says that the vectors have to be the same size.

Thank you

So would the function be

Y = chi2pdf(D,V)

Answer by Wayne King
on 22 Dec 2011

Hi John, the chi-square density is characterized by 1 parameter, the degrees of freedom. You do not want to say that because you have 1000 values, the chi-square density has dof=999. You can have a chi-square RV with 1000 values, and varying degrees of freedom.

For example:

To simulate some chi-square random vectors:

% 2 dof R2 = chi2rnd(2,1000,1); % 5 dof R5 = chi2rnd(5,1000,1);

What you want to do is take your data and estimate the dof parameter based on your data.

One thing you can do is to use fitdist() with 'gamma'.

A chi-square density is a gamma density with a=v/2 and b=2 where v is the degrees of freedom.

R2 = chi2rnd(2,1000,1); ksd = fitdist(R2,'gamma');

You should see that ksd.b is close to 2. to get an estimate of the dof characterizing the chi-square density you can do:

round(ksd.a*2)

John
on 23 Dec 2011

Hi Wayne,

Thank you the reply and for explaining that to me.

So I should load in my distance value data, then use fitdist() with 'gamma' to estimate the dof

x = load('distance.txt');

ksd = fitdist(x,'gamma');

round(ksd.a*2)

The answer is 2

So then it would be

Y = chi2pdf(x,v),

where v is a column vector the same size as x and which all elements are 2?

Is this correct?

Thank you

John

Answer by Wayne King
on 23 Dec 2011

Hi John, No, v is the degrees of freedom. It is a scalar. You have estimated your degrees of freedom as 2.

By the way did you also check that ksd.b is close to 2? If ksd.b is not close to 2, then your data are better approximated perhaps by a more general gamma distribution. The chi-square density is a special case of a more general gamma family. Let's assume ksd.b is close to 2.

Then construct an 'x' vector that matches the range of your data

Let's assume that your distances run from 0 to 10. The fitted pdf is:

x = 0:.01:10; y = chi2pdf(x,2); plot(x,y);

If ksd.b is not close to 2, then use gampdf()

x = 0:0.01:10; y = gampdf(x,ksd.a,ksd.b); plot(x,y);

If you want to overlay the fitted distribution over the histogram:

I'll use R as my distance measures, substitute your data.

R = gamrnd(2,2,100,1); binWidth = 2; %These aren't hard and fast x0 = round(min(R)); xend = ceil(max(R)); binCtrs = 1:binWidth:19; %These aren't hard and fast n=length(R); counts = hist(R,binCtrs); prob = counts / (n * binWidth); h = bar(binCtrs,prob,'hist'); set(h,'FaceColor',[.9 .9 .9]); x=x0:.01:xend; hold on; ksd = fitdist(R,'gamma'); y = gampdf(x,ksd.a,ksd.b); %gamma pdf plot(x,y,'k','linewidth',2);

John
on 28 Dec 2011

Hi Wayne,

Thanks very much for that. I checked ksd.b and it was not close to 2. It was 19. As you suggested the chi-square density plot does not fit the data.

I used the distribution fitting tool instead to model the data and got a good fit.

http://img526.imageshack.us/img526/1663/pdfplot.jpg

Thanks for the help

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments