1 ## Author: Martijn van Oosterhout <kleptog@svana.org>
2 ## This program is granted to the public domain.
5 ## @deftypefn {Function File} {} [A B] = gamfit (@var{R})
6 ## Finds the maximumlikelihood estimator for the Gamma distribution for R
7 ## @seealso{gampdf, gaminv, gamrnd, gamlike}
10 ## This function works by minimizing the value of gamlike for the vector R.
11 ## Just about any minimization function will work, all it has to do a
12 ## minimize for one variable. Although the gamma distribution has two
13 ## parameters, their product is the mean of the data. so a helper function
14 ## for the search takes one parameter, calculates the other and then returns
15 ## the value of gamlike.
17 ## Note: Octave uses the inverse scale parameter, which is the opposite of
18 ## Matlab. To work for Matlab, value of b needs to be inverted in a few
19 ## places (marked with **)
21 function res = gamfit(R)
29 # This can be just about any search function. I choose this because it
30 # seemed to be the only one that might work in this situaition...
31 a=nmsmax( @gamfit_search, 1, [], [], avg, R );
38 # Helper function so we only have to minimize for one variable. Also to
39 # inverting the output of gamlike, incase the optimisation function wants to
40 # maximize rather than minimize.
41 function res = gamfit_search( a, avg, R )
43 res = -gamlike([a 1/b], R);