3

I would like to use MATLAB's chi2gof to perform a chi-square goodness-of-fit test. My problem is that my assumed (i.e., theoretical) distribution is not one of the standard built-in probability distributions in MATLAB. The specific form of my desired distribution is:

p = x^a*exp(-b*x^2)

where a and b are constants. There must be a way to use chi2gof for arbitrary PDFs? I have done an exhaustive Google search, but have come up empty-handed.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • You can use [inverse transform sampling](https://en.wikipedia.org/wiki/Inverse_transform_sampling) to generate numbers from arbitrary distributions. – jadhachem Mar 02 '16 at 06:45
  • Is the actual issue that you don't know how to find the CDF for your distribution (`chi2gof` has an option to the CDF of an arbitrary hypothesized distribution)? What is the name of your distribution, by the way? @jadhachem: I don't understand your comment. I don't think that the issue is with generating random numbers from the distribution, but rather how to check the fit of existing data with the particular distribution. – horchler Mar 02 '16 at 15:52
  • @horchler My bad, I guess I misunderstood the question. – jadhachem Mar 02 '16 at 16:00
  • 1
    @horchler: I have an "analytical" expression for the CDF involving the incomplete gamma function. My issue is how to code the problem. Suppose, for instance, my CDF was given by F(x)=a*exp(-bx-cx^2) [b,c,>0]. (Assume a,b,c are known constants.) What would the call to chi2gof look like? I know it would involve using a function handle, but I am not sure how to code it. – user6006085 Mar 02 '16 at 16:18

1 Answers1

1

You can specify a handle to a function that takes a single argument to chi2gof this way:

a = ...
b = ...
c = ...
F = @(x)a*exp(-b*x-c*x.^2); % Technically this is an anonymous function
[H,P,STATS] = chi2gof(data,'cdf',F)

Or in special cases:

a = ...
b = ...
c = ...
F = @(x,a,b,c)a*exp(-b*x-c*x.^2);
[H,P,STATS] = chi2gof(data,'cdf',{F,a,b,c})

the last line of which is equivalent to

[H,P,STATS] = chi2gof(data,'cdf',@(x)F(x,a,b,c))

If the parameters a, b, and c are estimated (e.g., using some fitting process), then you should specify the number of estimated parameters to chi2gof. In this case:

[H,P, STATS] = chi2gof(data,'cdf',F,'nparams',3)

Please read the documentation to learn about the other options.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Thanks a lot! I will emulate your code with my real F(x). As a sidebar, I was also confused about the difference between a function handle and an anonymous function, but your comment resolves that query. – user6006085 Mar 02 '16 at 17:45