design the function function loss = detection_error_loss(t_hat, t) that computes the empirical detection-error loss of binary predictions t_hat ith respect to the true targets t in matlab

main.m
function loss = detection_error_loss(t_hat, t)
% This function computes the empirical detection-error loss of binary predictions t_hat with respect to the true targets t.

% Get number of examples
N = length(t);

% Compute the detection error loss for each example
det_error_loss = zeros(N,1);
for i = 1:N
    if t_hat(i) == t(i)
        % Correct detection
        det_error_loss(i) = 0;
    elseif t_hat(i) == 1 && t(i) == 0
        % False positive
        det_error_loss(i) = 1;
    elseif t_hat(i) == 0 && t(i) == 1
        % False negative
        det_error_loss(i) = 1;
    end
end

% Compute the average detection-error loss over all examples
loss = sum(det_error_loss) / N;
683 chars
24 lines

This function takes in two input arguments: t_hat which represents the predicted binary targets and t which represents the true binary targets. It computes the detection error loss for each example using the following rules:

  • If the predicted target is equal to the true target, then there is no detection error and the loss is zero.
  • If the predicted target is 1 and the true target is 0, then there is a false positive and the loss is 1.
  • If the predicted target is 0 and the true target is 1, then there is a false negative and the loss is 1.

The function then computes the average detection-error loss over all the examples and returns it as the output variable loss.

gistlibby LogSnag