% 2-param likelihood function. Barnett 2/3/06 % generate some fake data... (you could replace this with real-world data) n = 20; % amount of data to make y = -log(rand(n,1)); % trick to get random samples from exp pdf plot(y, zeros(size(y)), '*'); % examine your 'data' y. (zero vertical coord) % calc likelihood using gamma pdf model... 2 params. A bit slow! r = 0.1:0.03:3; % param r l = 0:0.01:2; % param l = lambda [rr,ll] = meshgrid(r,l); % create rr and ll as their values % over a rectangular 2d grid. L = ones(size(rr)); % L will be over same grid: start value 1 for i=1:n % loop i=1...n taking product of gamma pdfs L = L .* (ll.^rr ./ gamma(rr)) .* exp(-ll.*y(i)) .* y(i).^(rr-1); end % labeled plots... figure; surf(r, l, L); xlabel('r'); ylabel('\lambda'); zlabel('L(r,\lambda)'); figure; contourf(r, l, L); xlabel('r'); ylabel('\lambda'); % done % ------------------------------------------------------------------------- % Note there's a much faster way to compute the L values over the grid % by taking advantage of the fact the y-dep part of L is a product of % r-dep and l-dep factors. You don't even need the meshgrid cmd. Do this: tic; % start the clock lnL = n*kron(log(l)', r) - repmat(l'*sum(y), size(r)) + ... repmat(-n*log(gamma(r))+(r-1)*sum(log(y)), size(l')); L = exp(lnL); toc % read the clock % speed is 0.04 sec vs 2.4 sec for the crude above way!