% This file can be run as is in matlab.
% Answers cases a) and b) of assignment 2.

% Creates variables 'x_', that corresponds to D_x,
% 'x_y', corresponding to D_x|y,
% 'y_x', corresponding to D_y|x.

% 2 a)
% X = {1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 30, 36}
% Y = {1, 2, 3, 4, 5, 6}
% Y' = Y
% L = 0 if correct, 1 otherwise.



% 2 b)

% find D_x
x_(1:36) = 0;
for r=1:6;
    for g= 1:6;
        x_(r*g) = x_(r*g) + 1;
    end
end

% normalize D_x
x_ = x_ / sum(x_);


% Calculating D_y|x starts here.
% Initialize a table of y|x to zero.
% First index is x, second is y.
y_x(1:36, 1:6) = 0;


% Find events where there is a possibile value for the green dice
% so that green * y = x.
for x=1:36;
    for y=1:6;
        if (mod(x, y) == 0) && (x / y < 7);
            y_x(x, y) = y_x(x, y) + 1;
        end
    end
end


% normalize results to create D_y|x probability distributions.

for x=1:36;
    % sum up the amount of events in a row.
    line_sum = sum(y_x(x, :));
    
    % if the sum of events is not zero, (to avoid division by zero).
    if line_sum ~= 0;
        % normalize.
        y_x(x, :) = y_x(x, :) / line_sum;
    end
end



% Find possible events for distribution D_x|y
x_y(1:36, 1:6) = 0;

for x = 1:6; % possible outcomes of green dice.
    for y = 1:6; % possible outocmes of red dice.
            x_y(x*y, y) = x_y(x*y, y) + 1;
    end
end

% normalize results to obtain probability distributions.
for y = 1:6;
    x_y(:, y) = x_y(:, y) / sum(x_y(:, y));
end
