How do I use MLE on a shifted gamma distribution?

7 views (last 30 days)
First the Matlab documentation on using the built in distributions is great. I've successfully used gampdf to fit distributions using MLE. However, I would now like to use a custom distribution and I'm running into problems. Error messages such as the "...function returns negative or zero values" are very common when working through. I tried looking at gampdf.m but there's a lot of code in that function: example what is the scaling factor z=x./b?
here is my function below just to start off, any help getting this to work with MLE (in error free form) would be greatly appreciated. Thanks in advance.
function y=mygampdf(x,alpha,beta,x1)
%
% Y = MYGAMPDF(X,ALPHA,BETA,XI);
% This is a shifted gamma function along the x-axis to the right using the
% term XI. All other factors are the same as the usual gamma distribution
% in gampdf.m
for i=1:length(x)
y(i)=0;
end
y=(1/(beta^alpha*gamma(alpha))).*(x-x1).^(alpha-1).*exp(-1.0.*(x-x1)./beta);
end

Accepted Answer

Peter Perkins
Peter Perkins on 11 Sep 2012
Pat, thresholded distributions are typically not easy to fit by maximum likelihood. I am not sure of the details for a 3-param gamma, there may be literature specifically dealing with this, I don't know. But for a 3-param lognormal, the MLE is unbounded and in effect estimates the threshold parameter at the smallest observation and the variance parameter at zero, while for the 3-param Weibull, the MLE is unbounded in some cases and in others there are multiple local optima.
You can try fitting by maximum likelihood, but if you're using the MLE function with a custom PDF function, you at least will need to upper bound the threshold parameter by the smallest observation, and probably that minus a small epsilon. You might consider looking at this
which describes another fitting method that doesn't suffer from the problems that ML has in some thresholded distributions.
By the way, it's not clear to me why you need that loop in your PDF. If you want to preallocate y, use y = zeros(size(x)). But given the line that follows the loop, you shouldn't even need to preallocate. Hope this helps.
  3 Comments
sepideh
sepideh on 5 Dec 2014
I am trying to do the same thing and this link apparently should be purchased? What is the fitting method? I am trying to find it from another source if possible.

Sign in to comment.

More Answers (1)

Matt Tearle
Matt Tearle on 11 Sep 2012
Peter is the expert on statistical matters, so I'd take his advice on whether what you're trying to do is a good idea. But here's one way to make something work (whether or not it's a good idea!):
% define the pdf
shiftgampdf = @(x,a,b,xi) max(realmin,gampdf(x-xi,a,b));
% make some data
a = pi;
b = 2/pi;
y = gamrnd(a,b,200,1)+10;
% do the MLE fit
mle(y,'pdf',shiftgampdf,'start',[2,1,5])
Note that I'm defining the PDF as a function handle that simply calls gampdf with a shifted x value. However, you can get values that are 0 to machine precision during the MLE fitting, which is why I've added the max(realmin... part. This ensures that the values returned are always very slightly positive (avoiding the error messages that you get from mle).
  1 Comment
Pat
Pat on 11 Sep 2012
Thanks Matt! I appreciate your input. I will give your solution a shot, and I agree with you, Peter is certainly the expert. His statements above make me nervous about proceeding but we'll see where this goes. Thanks again.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!