% MATLAB 1. Handout 1.
%
%  Bayes Formula: Updating a prior to a posterior
%-------------------------------------------------
% One out of N coins in the box is "two headed".
% A coin is selected at random from the box and flipped k times.
% In all k flipps the coin landed heads up.
% What is the probability that the two-headed coin
% was originally selected?
% A_k = in all k trials the coin lands heads up.
% H_1 = two headed, H_2 = fair
% P(A_k) = (2^k + N -1)/(2^k N); [total probability]
% P(H_1|A_k) = 2^k/(2^k + N - 1) = 1 - (N-1)/(2^k +N-1).
%--------------------------------------------------
close all
clear all

%-----------------------------------
 disp(' Figure Bayes 1.1: Two Headed Coin')
  lw = 3; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 17;
  msize = 20;
%------------------------------------
N=1000000;
kmin=1; kmax=30;
%---------------
posteriors=[];
for k=kmin:kmax
    posteriors = [posteriors 2^k/(2^k + N -1)];
end
plot( (kmin:kmax), posteriors,'-','linewidth',lw)
hold on
plot( (kmin:kmax), posteriors,'o')
xlabel('number of flips')
ylabel('posterior probability')
%---------------------------
% your path for eps image...
print -depsc 'C:\Brani\Courses\Bayes\Handouts\Figs\twoheaded.eps'

% For N=1000 and k=30,    P(H_1|A_{30}) = 0.99999906960961
% For N=100000 and k=30   P(H_1|A_{30}) = 0.99990687734650
% For N=1000000 and k=30  P(H_1|A_{30}) = 0.99906954490967
%-------------------------------
% ISyE 8843 BVidakovic
% ------------------------------