This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/statistics/exercises/mlepdffit.m

31 lines
723 B
Matlab

% plot gamma pdfs:
xx = 0.0:0.1:10.0;
shapes = [ 1.0, 2.0, 3.0, 5.0];
cc = jet(length(shapes) );
for i=1:length(shapes)
yy = gampdf(xx, shapes(i), 1.0);
plot(xx, yy, '-', 'linewidth', 3, 'color', cc(i,:), ...
'DisplayName', sprintf('s=%.0f', shapes(i)) );
hold on;
end
% generate gamma distributed random numbers:
n = 50;
x = gamrnd(3.0, 1.0, n, 1);
% histogram:
[h,b] = hist(x, 15);
h = h/sum(h)/(b(2)-b(1));
bar(b, h, 1.0, 'DisplayName', 'data');
% maximum likelihood estimate:
p = mle(x, 'distribution', 'gamma');
yy = gampdf(xx, p(1), p(2));
plot(xx, yy, '-k', 'linewidth', 5, 'DisplayName', 'mle' );
hold off;
xlabel('x');
ylabel('pdf');
legend('show');
savefigpdf(gcf, 'mlepdffit.pdf', 12, 8)