% HW6: Alex qu C - Cauchy pdf sample mean estimator expt

% i)
y = tan(pi*rand(1,10000) - pi/2);
dx = 0.1;    % choose spacing for histogram
x = -4:dx:4;
figure; hist(y, x); xlabel y; ylabel freq; axis([-3 3 0 400]);
% compare to true Cauchy... (optional)
hold on; plot(x, (dx/pi)*ns(end)./(1+x.^2), '-'); 

% ii)
ns = 10.^(1:7);      % list of n values (note 10^7 causes a few sec wait).
clear m                   % so any old values of m forgotten
for i=1:7
  n = ns(i);                     % do each n value in turn
  y = tan(pi*rand(1,n) - pi/2);     % note: fresh samples each time (optional)
  m(i) = mean(y);                   % note: we fill a list, plot only at end
end
figure; semilogx(ns, m, '+-'); xlabel n; ylabel('sample mean');

% iii)              posterior; we can reuse the y list already sitting there
n = 100;
m = -2:0.01:2;        % choose list of mu values to calc L over (then plot)
L = ones(size(m));
for i=1:n
  L = L .* (1/pi)./(1+(y(i)-m).^2);    % note ./ .^ since m is a list
end
figure; plot(m, L, '-'); xlabel \mu; ylabel L(\mu);

