0

Hi have a set of observations obs = https://drive.google.com/file/d/0B3vXKJ_zYaCJVlhqd3FJT0xtWFk/view?usp=sharing

I would like to prove that they come from a Gamma distributions.

To do that I:

%estimate parameters gamma distribution    
paramEsts_gamma = gamfit(obs);   
% estimate cdf gamma distribution (objects)
gamma_cdf=makedist('Gamma','a',paramEsts_gamma(1),'b',paramEsts_gamma(2));

% test with kstest if data comes from a gamma distribution
    [h_gamma_ks,p_gamma_ks,kstat_gamma_ks,cv_gamma_ks] = kstest(obs,'CDF',gamma_cdf)

% test with chi2gofif data comes from a gamma distribution
    pd_gamma = fitdist(obs,'Gamma');
    [h_gamma_chi,p_gamma_chi,st_gamma_chi] = chi2gof(obs,'CDF',pd_gamma)

My problem is that I get NaN for the pvalue p_gamma_chi.... Where do I make a mistake? Thanks

Here some code to check visually the distributions

%% Plot cdf
% empirical cdf
[f_emp,x_values] = ecdf(obs);
f_gamma = gamcdf(x_values,paramEsts_gamma(1),paramEsts_gamma(2));

     figure
     hold on;
     F = plot(x_values,f_emp);
     set(F,'LineWidth',2);

     G = plot(x_values,f_gamma,'r-');
     set(G,'LineWidth',2);


     legend([F G],...
        'Empirical CDF','Gamma CDF',...
        'Location','SE');
gabboshow
  • 5,359
  • 12
  • 48
  • 98
  • 2
    How this question is different from your [previous one](http://stackoverflow.com/questions/26962457/calculate-goodness-of-fit-matlab)? On the other hand you cannot prove it in a mathematical way, at best you can find some probabilistic support. – rozsasarpi Nov 17 '14 at 10:56
  • I added the kstest for the goodness of fit. with this test I get a number for the pvalue, with the chisquare test I get NaN...I would like to know how to proper use chi2gof. Not sure I understood the second part of your comment.. – gabboshow Nov 17 '14 at 11:00
  • If you do not get the second part then I guess your problem is with understanding of chi-square test and not about Matlab built-in function, in this case your question is off-topic – rozsasarpi Nov 17 '14 at 11:05
  • Well...I was just asking for some help...and I continue not getting your point... – gabboshow Nov 17 '14 at 11:14
  • In general, if you ask a question but later find that more information/a better explanation of the problem is needed: Please edit the existing question rather than asking a new similar one. – Dennis Jaheruddin Nov 17 '14 at 11:26

1 Answers1

0

As the output of your code shows st_gamma_chi.df = 0, which means 0 degrees-of-freedom (dof).

dof = N - n - 1

where:
N is the number of frequencies, in your case N = length(st_gamma_chi.edges)-1 = 3;
n is the number of fitted parameters, in your case n = 2.

Thus you get 0 dof with the default options, you can ameliorate this issue for example by increasing the number of bins where the frequencies are calculated:

[h_gamma_chi,p_gamma_chi,st_gamma_chi] = chi2gof(obs,'CDF',pd_gamma, 'NBins', 20)

But this will not exempt you from understanding chi-squared test.

rozsasarpi
  • 1,621
  • 20
  • 34