% generates a normalized likelihood matrix over mu = -1 to 1
% and x = .01 to 2, then creates a contour or surface plot of the
% likelihood
% also prints out ML values for mu and x


x = -1:.01:1; %mu
z = .01:.01:2; %x
b = length(x);
c = length(z);
d = 100; % number of samples

a = zeros(b,c,d); %create 3D array with proper dimensions
y = tan (pi*(rand(d,1)-.5)); %samples from the Cauchy with mu = 0 and x = 1
for m = 1:b
       for n = 1:c
           for p = 1:d
           a(m,n,p)=10/pi*(z(1,n))/(z(1,n)^2+(y(p,1)-x(1,m))^2);
           % individual sample likelihood value for a certian mu and x
           end
       end
end

L = zeros(b,c); %likelihood matrix
for m = 1:b
    for n = 1:c
    L(m,n) = prod(a(m,n,1:d)); %multiply all the individual sample likelihoods 
                               % for a certian mu and x
    end
end

k = sum(sum(L))*.01*.01 %volume under L
L = L./k; %normalize L

% finding ML estimates for mu and x:
[C,I]=max(L);
[D,J]=max(C);
MLmu = x(I(1,J))
MLx = z(J)

figure; surf(z,x,L, 'EdgeAlpha',0); xlabel x; ylabel mu; %surface plot
figure; contourf(z,x,L,10); xlabel x; ylabel mu; %contour plot