52 lines
1.3 KiB
Matlab
52 lines
1.3 KiB
Matlab
% 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')
|