views:

133

answers:

1

Hi there. 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.

+5  A: 

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.

woodchips
That's terrific, thanks for the detailed answer. The fzero function is great! I'm a little bit dismayed that there's no solution, though, since the author of a paper I'm trying to reproduce said he used a distribution with exactly these characteristics as a prior distribution for an MCMC simulation. I don't suppose you're familiar with MCMC, are you? If you are, would you mind taking a quick look at page 11 of this paper and letting me know if I've misunderstood something? Here's the URL for the paper: http://bit.ly/9uWlLN .
jefflovejapan
The prior I gave the details for above is the first one in the chart, tau.
jefflovejapan
I read through the paper, and then did some more playing. I needed to use better starting values for fzero, plus an interval that brackets the solution to gain convergence.
woodchips
Thank you so much! You're a lifesaver!
jefflovejapan

related questions