% Expected loss of the square error method is obtained
% by going through all possible cases and calculating
% the square error for the optimal guess in each case.
% Finally averaging the sum of losses with the amount
% of cases gives us the expected loss

% Takes in as parameter D_y|x,
% outputs expected loss.

function loss = t3_2(y_x)

loss = 0;
cases = 0;

% again utilize the knowledge that all distributions
% are equal evenly spread among those cases that have
% probability > 0. (All weights set to 1.)

y_x_weights = (y_x > 0);

for n1 = 1:6;
    for n2 = 1:6;
        x = n1 * n2;
        guess = t3(x, y_x_weights);
        
        count = sum(y_x_weights(x, :));
        distance = y_x_weights(x,:).*(1:6);
        
        distance(distance ~= 0) = distance(distance~=0) - guess;
        
        sqrdist = distance.*distance;
        
        loss = loss + sum(sqrdist) / count;
        cases = cases + 1;
    end
end

loss = loss / cases;