Search code examples
matlabprobabilitygamma-distribution

MATLAB | calculating parameters of gamma dist based on mean and probability interval


I have a system of 2 equations in 2 unknowns that I want to solve using MATLAB but don't know exactly how to program. I've been given some information about a gamma distribution (mean of 1.86, 90% interval between 1.61 and 2.11) and ultimately want to get the mean and variance. I know that I could use the normal approximation but I'd rather solve for A and B, the shape and scale parameters of the gamma distribution, and find the mean and variance that way. In pseudo-MATLAB code I would want to solve this:

gamcdf(2.11, A, B) - gamcdf(1.61, A, B) = 0.90;
A*B = 1.86;

How would you go about solving this? I have the symbolic math toolbox if that helps.


Solution

  • The mean is A*B. So can you solve for perhaps A in terms of the mean(mu) and B?

    A = mu/B
    

    Of course, this does no good unless you knew B. Or does it?

    Look at your first expression. Can you substitute?

    gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) = 0.90
    

    Does this get you any closer? Perhaps. There will be no useful symbolic solution available, except in terms of the incomplete gamma function itself. How do you solve a single equation numerically in one unknown in matlab? Use fzero.

    Of course, fzero looks for a zero value. But by subtracting 0.90, that is resolved.

    Can we define a function that fzero can use? Use a function handle.

    >> mu = 1.86;
    >> gamfun = @(B) gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) - 0.90;
    

    So try it. Before we do that, I always recommend plotting things.

    >> ezplot(gamfun)
    

    Hmm. That plot suggests that it might be difficult to find a zero of your function. If you do try it, you will find that good starting values for fzero are necessary here.

    Sorry about my first try. Better starting values for fzero, plus some more plotting does give a gamma distribution that yields the desired shape.

    >> B = fzero(gamfun,[.0000001,.1])
    B =
            0.0124760672290871
    >> A = mu/B
    A =
              149.085442218805
    >> ezplot(@(x) gampdf(x,A,B))
    

    In fact this is a very "normal", i.e, Gaussian, looking curve.