% Estimating parameters: likelihood function.      Barnett 2/1/06

% generate some fake data... (you could replace this with real-world data)
n = 10;                        % 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)

% plot likelihood using exponential pdf model...
l = 0:0.01:3;                  % param l = lambda
L = (l.^n) .* exp(-l*sum(y));  % likelihood func given the data
figure; plot(l, L, '-'); xlabel('\lambda'); ylabel('L(\lambda)');

% automatically find the peak by finding minimum of -L...
L = @(l) -(l.^n) .* exp(-l*sum(y));     % create 'inline' neg likelihood func
l_e = fminbnd(L,0,3);                   % search in [0,3] for min
hold on; plot(l_e, -L(l_e), 'o');       % show the max

% show Max Lik estimated pdf superimposed on original data...
figure; plot(y, zeros(size(y)), '*'); hold on;
x = 0:0.01:max(y);                      % values to plot using
plot(x, l_e*exp(-l_e*x), '-'); xlabel('y'); ylabel('f_Y(y;l_e)');
