function res = MAP_rule_plot();

display('Starting MAP Rule');

function res = p_X_given_H(X,H)
    res = nchoosek(10,X) .* (...
        (H==0) .* (.5.^X .* .5.^(10-X)) + ...
        (H==1) .* (.9.^X .* .1.^(10-X)) + ...
        (H==2) .* (.1.^X .* .9.^(10-X)));
end

    function res = p_H(H);
        res = (H==0) .* 5/7 + (H==1) .* 1/7 + (H==2) .* 1/7;
    end


figure(1);
clf;
    for X = 0:10
        plot(X, p_X_given_H(X,0), 'Xb')
        plot(X, p_X_given_H(X,1), 'Xg')
        plot(X, p_X_given_H(X,2), 'Xr')
        hold on;
    end
    hold off;


function res = p_H_given_X(H,X)
    bottom_sum = (p_H(0) .* p_X_given_H(X, 0)) + ...
        (p_H(1) .* p_X_given_H(X, 1)) + ...
        (p_H(2) .* p_X_given_H(X, 2));
    
    res = p_H(H) .* p_X_given_H(X, H) ./ bottom_sum;
end

p_H_given_X(0,1);
p_H_given_X(1,1);
p_H_given_X(2,1);

figure(2);
clf;
    for X = 0:10
        probs = [p_H_given_X(0,X) p_H_given_X(1,X) p_H_given_X(2,X)];
        max_prob = max(probs);
        class_ind = (probs == max_prob);
        class_dec = find(class_ind == 1,1) - 1;
        plot(X, class_dec, 'X');
        hold on;
    end
    hold off;
    ylim([-.2 2.2]);
end