% Clear the environment to make sure nothing interferes with this script
clear; 

%% Setup Pr(Y | X) 
p = ([1:100]/100)';
prYgX = [ 1-p, p ];
% Pr(Y=0 | X=i) = prYgX(i,1)
% Pr(Y=1 | X=i) = prYgX(i,2)

% Import the 
jars = importdata('jars.csv');


%% Problem 5.1
% Plot the jars 

% First normalize the jars
jars(:,2:4) = jars(:,2:4) ./ repmat(sum(jars(:,2:4)),100,1);

% 5.1.1
figure(1); clf();
set(gca(), 'FontSize', 20);
bar(prYgX(:,2));
title('Pr(Y=1|X)');
colormap('gray');
axis('tight');
xlabel('Thumbtack Id');
ylabel('Pr(Y=1)')

% 5.1.2
figure(2); clf();
set(gca(), 'FontSize', 20);
bar(jars(:,2));
title('Pr(X) In Jar 1');
colormap('gray');
axis('tight');
xlabel('Thumbtack Id');
ylabel('Probability');

% 5.1.3
figure(3); clf();
set(gca(), 'FontSize', 20);
bar(jars(:,3));
title('Pr(X) In Jar 2');
colormap('gray');
axis('tight');
xlabel('Thumbtack Id');
ylabel('Probability');

% 5.1.4
figure(4); clf();
set(gca(), 'FontSize', 20);
bar(jars(:,4));
title('Pr(X) In Jar 3');
colormap('gray');
axis('tight');
xlabel('Thumbtack Id');
ylabel('Probability');


%% Problem 5.2 
% Likelihood of multiple observations (including order)
figure(5); clf();
set(gca(), 'FontSize', 20);
bar(p.^3 .* (1-p).^2);
title('Likelihood of (u, u, d, u, d)');
axis('tight');
colormap('gray');
ylabel('Likelihood');
xlabel('Thumbtack');

% Likelihood of multiple observations (ignoring order)
figure(6); clf();
set(gca(), 'FontSize', 20);
bar(binopdf(3,5,p));
title('Likelihood of 3 ups in 5 trials');
axis('tight');
colormap('gray');
ylabel('Likelihood');
xlabel('Thumbtack');


%% Problem 5.3
% Bayes Rule
% This was not a coding question

%% Problem 5.4 
% Posterior dependence on number of observations
J1 = jars(:,2);
figure(3);

% case 1
figure(7); clf();
set(gca(), 'FontSize', 20);
posterior = (1-p) .* J1;
posterior = posterior / sum(posterior);
bar(posterior);
colormap('gray');
title('1 Down');
axis('tight')
ylabel('Pr(X | Y)');
xlabel('Thumbtack');


figure(8); clf();
set(gca(), 'FontSize', 20);
posterior = (1-p).^3 .* J1;
posterior = posterior / sum(posterior);
bar(posterior);
colormap('gray');
title('3 Down');
axis('tight')
ylabel('Pr(X | Y)');
xlabel('Thumbtack');

figure(9); clf();
set(gca(), 'FontSize', 20);
posterior = p.^3 .* (1-p).^2 .* J1;
posterior = posterior / sum(posterior);
bar(posterior);
colormap('gray');
title('3 Up 2 Down');
axis('tight')
ylabel('Pr(X | Y)');
xlabel('Thumbtack');


figure(10); clf();
set(gca(), 'FontSize', 20);
posterior = p.^25 .* (1-p).^15 .* J1;
posterior = posterior / sum(posterior);
bar(posterior);
colormap('gray');
title('25 Up 15 Down');
axis('tight')
ylabel('Pr(X | Y)');
xlabel('Thumbtack');
[tmp, maxtack] = max(posterior);
disp(['Max posterior: ', num2str(maxtack)]);


%% Problem 5.5 
% Marginalizing 
prJ = [0.25; 0.25; .50];
prbjar = jars(:, 2:4);

figure(11); clf();
set(gca(), 'FontSize', 20);
% Define the prior as 
% Pr(X) = sum_j Pr(X | J = j) Pr(J=j)
prior = prbjar * prJ; 
% Compute new posterior
posterior = p.^8 .* (1-p).^2 .* prior;
% Don't forget to normalize
posterior = posterior / sum(posterior);
bar(posterior );
colormap('gray');
[tmp, maxtack] = max(posterior);
axis('tight');
title(['Pr(X | 8u and 2d) max at ', num2str(maxtack)]);
ylabel('Pr(X | Y)');
xlabel('Thumbtack');


figure(12); clf();
set(gca(), 'FontSize', 20);
% Define the likelihood in terms of jars
% lik = Pr(Y | J) = sum_X Pr(Y | X) Pr(X | J)
lik = (p.^8 .* (1-p).^2)' * prbjar;
prior = prJ;
posterior = lik' .* prior;
posterior = posterior / sum(posterior);
bar(posterior);
colormap('gray');
[tmp, maxjar] = max(posterior);
axis('tight');
title(['Pr(J | 8u and 2d) max at ', num2str(maxjar)]);
ylabel('Pr(J | Y)');
xlabel('Jar');
