# Differences between functions ezfit and Matlab's gamfit in fitting data to the gamma distribution?

3 views (last 30 days)
Natalia Demidova on 9 Sep 2021
Commented: Natalia Demidova on 15 Sep 2021
Main questions:
1) How does Matlab's gamfit function estimate fit parameters for input data set?
2) Gamfit uses MLEs while another fitting function, ezfit, uses the least-squares method (minimizing sum of squared residuals). If the parameters I get from fitting the data with either of these functions are not the same, this means that, obviously, MLE and LSE methods are not equivalent in this case? So based on this inherent difference in data fitting, I cannot expect to relate the obtained parameters in some way? (i.e. multiplying fit parameters found by gamfit by a constant, squaring, or performing some other consistent math manipulation to get to the values found by ezfit?
Background (all discussed functions are attached):
On gamfit: As I understood, the data input to gamfit is first approximated to the Weibull distribution, with those parameters then entered into function statsfminbx on lines 256-259 and said function then "maximizes the log-likelihood with respect to a and b". This is not clear to me, could someone explain why Weibull estimation is necessary? Compared to the function below, it is not clear which PDF of the gamma distribution is used by Matlab, does anyone know where I can find this?
On ezfit: With this function, you specify exactly the PDF of the distribution you'd like to estimate the fit parameters for given your data, and it computes them by minimizing the sum of squared residuals (pretty straight forward, using Matlab's fminsearch function). This is the PDF of gamma distribution I am inputting to fit data with: Note: Comparing the two functions (ezfit vs gamfit), the resulting shape parameters (alpha) are relatively on the same scale of magnitude, while scale parameter beta found with ezfit is on the order of 10e-3, while with gamfit it is on the order of 10e2.

Jeff Miller on 10 Sep 2021
There are two somewhat different parameterizations of the gamma distribution. In one (which I think is used by gamfit), the mean of the distribution is alpha*beta. In the other (which I think is used in the PDF you are passing to ezfit), the mean of the distribution is alpha/beta. So, I think the discrepancy you are seeing is due to these different parameterizations.
So, I expect that the alpha parameters will be quite similar for the two methods but the betas will be (approximately) inverses of each other. As you say, though, the MLE and LSE methods are not equivalent, so the parameters are unlikely to correspond exactly. (With a very large number of data points, though, I would expect the deviations to be minimal.)
No idea what is going on with the Weibull--sorry.
Hope that helps a little.
Natalia Demidova on 15 Sep 2021
Thanks, that did help! Good to hear the same thoughts from someone else also (I've been breaking my head at this for a while). I figured it may be an inverse relationship because of the inverse gamma dist being used by gamfit.

R2021a

### Community Treasure Hunt

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

Start Hunting!