www.gusucode.com > stats 源码程序 matlab案例代码 > stats/NegativeLoglikelihoodFunctionsExample.m
%% Negative Loglikelihood Functions % % Copyright 2015 The MathWorks, Inc. %% Generate a Random Gamma Distribution Sample rng default; % for reproducibility a = [1,2]; X = gamrnd(a(1),a(2),1e3,1); %% Visualize the Likelihood Surface mesh = 50; delta = 0.5; a1 = linspace(a(1)-delta,a(1)+delta,mesh); a2 = linspace(a(2)-delta,a(2)+delta,mesh); logL = zeros(mesh); % Preallocate memory for i = 1:mesh for j = 1:mesh logL(i,j) = gamlike([a1(i),a2(j)],X); end end [A1,A2] = meshgrid(a1,a2); surfc(A1,A2,logL) %% Locate Minimum Using fminsearch LL = @(u)gamlike([u(1),u(2)],X); % Likelihood given X MLES = fminsearch(LL,[1,2]) %% Locate Minimum Using gamfit ahat = gamfit(X) %% Add MLEs to Surface Plot hold on plot3(MLES(1),MLES(2),LL(MLES),... 'ro','MarkerSize',5,... 'MarkerFaceColor','r')