Added matlab code to mle chapter
This commit is contained in:
51
statistics/code/mlemeanstd.m
Normal file
51
statistics/code/mlemeanstd.m
Normal file
@@ -0,0 +1,51 @@
|
||||
% draw random numbers:
|
||||
n = 500;
|
||||
mu = 3.0;
|
||||
sigma =2.0;
|
||||
x = randn(n,1)*sigma+mu;
|
||||
fprintf(' mean of the data is %.2f\n', mean(x))
|
||||
fprintf('standard deviation of the data is %.2f\n', std(x))
|
||||
|
||||
% mean as parameter:
|
||||
pmus = 2.0:0.01:4.0;
|
||||
% matrix with the probabilities for each x and pmus:
|
||||
lms = zeros(length(x), length(pmus));
|
||||
for i=1:length(pmus)
|
||||
pmu = pmus(i);
|
||||
p = exp(-0.5*((x-pmu)/sigma).^2.0)/sqrt(2.0*pi)/sigma;
|
||||
lms(:,i) = p;
|
||||
end
|
||||
lm = prod(lms, 1); % likelihood
|
||||
loglm = sum(log(lms), 1); % log likelihood
|
||||
|
||||
% plot likelihood of mean:
|
||||
subplot(2, 2, 1);
|
||||
plot(pmus, lm );
|
||||
xlabel('mean')
|
||||
ylabel('likelihood')
|
||||
subplot(2, 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')
|
||||
27
statistics/code/mlepdffit.m
Normal file
27
statistics/code/mlepdffit.m
Normal file
@@ -0,0 +1,27 @@
|
||||
% 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');
|
||||
29
statistics/code/mlepropfit.m
Normal file
29
statistics/code/mlepropfit.m
Normal file
@@ -0,0 +1,29 @@
|
||||
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');
|
||||
6
statistics/code/mleslope.m
Normal file
6
statistics/code/mleslope.m
Normal file
@@ -0,0 +1,6 @@
|
||||
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