[likelihood] improved solution to exercise 1
This commit is contained in:
parent
5e5ea9965c
commit
5d46493c59
@ -3,10 +3,11 @@ sigma =2.0;
|
||||
ns = [50, 1000];
|
||||
for k = 1:length(ns)
|
||||
n = ns(k);
|
||||
fprintf('\nn=%d\n', n);
|
||||
% draw random numbers:
|
||||
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))
|
||||
fprintf(' mean of the data is %.2f\n', mean(x))
|
||||
fprintf(' standard deviation of the data is %.2f\n', std(x))
|
||||
|
||||
% standard deviation as parameter:
|
||||
psigs = 1.0:0.01:3.0;
|
||||
@ -17,8 +18,14 @@ for k = 1:length(ns)
|
||||
p = exp(-0.5*((x-mu)/psig).^2.0)/sqrt(2.0*pi)/psig;
|
||||
lms(:,i) = p;
|
||||
end
|
||||
lm = prod(lms, 1); % likelihood
|
||||
lm = prod(lms, 1); % likelihood
|
||||
loglm = sum(log(lms), 1); % log likelihood
|
||||
maxlm = psigs(lm==max(lm));
|
||||
if length(maxlm) > 1
|
||||
maxlm = maxlm(1);
|
||||
end
|
||||
fprintf(' likelihood: standard deviation of the data is %.2f\n', maxlm)
|
||||
fprintf(' log-likelihood: standard deviation of the data is %.2f\n', psigs(loglm==max(loglm)))
|
||||
|
||||
% plot likelihood of standard deviation:
|
||||
subplot(2, 2, 2*k-1);
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user