changeset 2779:d05f5c23ec22 octave-forge

[for Martijn van Oosterhout] likelihood and distribution for gamma and fit
author pkienzle
date Sat, 02 Dec 2006 21:16:19 +0000
parents 8d864908605d
children 5fc5f80118d3
files main/statistics/inst/gamfit.m main/statistics/inst/gamlike.m
diffstat 2 files changed, 61 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/statistics/inst/gamfit.m	Sat Dec 02 21:16:19 2006 +0000
@@ -0,0 +1,43 @@
+## -*- texinfo -*-
+## @deftypefn {Function File} {} [A B] = gamfit (@var{R})
+## Finds the maximumlikelihood estimator for the Gamma distribution for R
+## @seealso{gampdf, gaminv, gamrnd, gamlike}
+## @end deftypefn
+
+## This function works by minimizing the value of gamlike for the vector R.
+## Just about any minimization function will work, all it has to do a
+## minimize for one variable. Although the gamma distribution has two
+## parameters, their product is the mean of the data. so a helper function
+## for the search takes one parameter, calculates the other and then returns
+## the value of gamlike.
+
+## Note: Octave uses the inverse scale parameter, which is the opposite of
+## Matlab. To work for Matlab, value of b needs to be inverted in a few
+## places (marked with **)
+
+## Written by Martijn van Oosterhout <kleptog@svana.org> (Nov 2006)
+## This code is public domain
+
+function res = gamfit(R)
+
+avg = mean(R);
+
+# This can be just about any search function. I choose this because it
+# seemed to be the only one that might work in this situaition...
+a=nmsmax( @gamfit_search, 1, [], [], avg, R );
+
+b=a/avg;      # **
+
+res=[a b];
+
+# Helper function so we only have to minimize for one variable. Also to
+# inverting the output of gamlike, incase the optimisation function wants to
+# maximize rather than minimize.
+
+function res = gamfit_search( a, avg, R )
+
+b=a/avg;      # **
+
+res = -gamlike([a b], R);
+
+ 	  	 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/statistics/inst/gamlike.m	Sat Dec 02 21:16:19 2006 +0000
@@ -0,0 +1,18 @@
+## -*- texinfo -*-
+## @deftypefn {Function File} {} X = gamlike ([@var{A} @var{B}], @var{R})
+## Calculates the negative log-likelihood function for the Gamma
+## distribution over vector R, with the given parameters A and B.
+## @seealso{gampdf, gaminv, gamrnd, gamfit}
+## @end deftypefn
+
+## Written by Martijn van Oosterhout <kleptog@svana.org> (Nov 2006)
+## This code is public domain
+
+function res = gamlike(P,K)
+
+a=P(1);
+b=P(2);
+
+res = -sum( log( gampdf(K, a, b) ) )
+
+