]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/gamfit.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / gamfit.m
1 ## Author: Martijn van Oosterhout <kleptog@svana.org>
2 ## This program is granted to the public domain.
3
4 ## -*- texinfo -*-
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}
8 ## @end deftypefn
9
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.
16
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 **)
20
21 function res = gamfit(R)
22
23   if (nargin != 1)
24     print_usage;
25   endif
26
27   avg = mean(R);
28
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 );
32
33   b=a/avg;      # **
34
35   res=[a 1/b];
36 endfunction
37
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 )
42   b=a/avg;      # **
43   res = -gamlike([a 1/b], R);
44 endfunction