Added exercises for mle
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
% draw random numbers:
|
||||
n = 500;
|
||||
n = 100;
|
||||
mu = 3.0;
|
||||
sigma =2.0;
|
||||
x = randn(n,1)*sigma+mu;
|
||||
@@ -19,33 +19,11 @@ lm = prod(lms, 1); % likelihood
|
||||
loglm = sum(log(lms), 1); % log likelihood
|
||||
|
||||
% plot likelihood of mean:
|
||||
subplot(2, 2, 1);
|
||||
subplot(1, 2, 1);
|
||||
plot(pmus, lm );
|
||||
xlabel('mean')
|
||||
ylabel('likelihood')
|
||||
subplot(2, 2, 2);
|
||||
subplot(1, 2, 2);
|
||||
plot(pmus, loglm );
|
||||
xlabel('mean')
|
||||
ylabel('log likelihood')
|
||||
|
||||
% standard deviation as parameter:
|
||||
psigs = 1.0:0.01:3.0;
|
||||
% matrix with the probabilities for each x and psigs:
|
||||
lms = zeros(length(x), length(psigs));
|
||||
for i=1:length(psigs)
|
||||
psig = psigs(i);
|
||||
p = exp(-0.5*((x-mu)/psig).^2.0)/sqrt(2.0*pi)/psig;
|
||||
lms(:,i) = p;
|
||||
end
|
||||
lm = prod(lms, 1); % likelihood
|
||||
loglm = sum(log(lms), 1); % log likelihood
|
||||
|
||||
% plot likelihood of standard deviation:
|
||||
subplot(2, 2, 3);
|
||||
plot(psigs, lm );
|
||||
xlabel('standard deviation')
|
||||
ylabel('likelihood')
|
||||
subplot(2, 2, 4);
|
||||
plot(psigs, loglm);
|
||||
xlabel('standard deviation')
|
||||
ylabel('log likelihood')
|
||||
@@ -1,27 +0,0 @@
|
||||
% 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;
|
||||
legend('show');
|
||||
@@ -1,29 +0,0 @@
|
||||
m = 2.0; % slope
|
||||
sigma = 1.0; % standard deviation
|
||||
n = 100; % number of data pairs
|
||||
|
||||
% data pairs:
|
||||
x = 5.0*rand(n, 1);
|
||||
y = m*x + sigma*randn(n, 1);
|
||||
|
||||
% fit:
|
||||
slope = mleslope(x, y);
|
||||
fprintf('slopes:\n');
|
||||
fprintf('original = %.2f\n', m);
|
||||
fprintf(' fit = %.2f\n', slope);
|
||||
|
||||
% lines:
|
||||
xx = 0.0:0.1:5.0; % x-axis values
|
||||
yorg = m*xx;
|
||||
yfit = slope*xx;
|
||||
|
||||
% plot:
|
||||
plot(xx, yorg, '-r', 'linewidth', 5);
|
||||
hold on;
|
||||
plot(xx, yfit, '-g', 'linewidth', 2);
|
||||
plot(x, y, 'ob');
|
||||
hold off;
|
||||
legend('data', 'original', 'fit', 'Location', 'NorthWest');
|
||||
legend('boxoff')
|
||||
xlabel('x');
|
||||
ylabel('y');
|
||||
@@ -1,6 +0,0 @@
|
||||
function slope = mleslope(x, y )
|
||||
% Compute the maximum likelihood estimate of the slope
|
||||
% of a line through the origin
|
||||
% given the data pairs in the vectors x and y.
|
||||
slope = sum(x.*y)/sum(x.*x);
|
||||
end
|
||||
Reference in New Issue
Block a user