design the function function out = mse_vs_m(x_tr, t_tr, x_te, t_te) that uses all given samples in the split data sets and then computes the empirical mse test loss column vector in matlab

main.m
function out = mse_vs_m(x_tr, t_tr, x_te, t_te)
% x_tr and t_tr are the training dataset
% x_te and t_te are the testing dataset
% out is the column vector of the empirical mse test loss for different values of m

% Find the size of x_te and t_te, as we need it for iteration
[n_te, d] = size(x_te);

% Create an empty vector to store the mse test loss for different values of m
out = zeros(d, 1);

% Iterate over different values of m
for i=1:d
    % Compute the weight matrix
    W = ((x_tr(:, 1:i)'*x_tr(:, 1:i))\x_tr(:, 1:i)')*t_tr;
    
    % Compute the predictions
    y_te = x_te(:, 1:i)*W;
    
    % Compute the mse loss
    out(i) = sum((t_te-y_te).^2)/n_te;
end
end
678 chars
24 lines

This function takes in the training and testing datasets x_tr, t_tr, x_te, and t_te, and returns the empirical mse test loss column vector out for different values of m. It computes the weight matrix using the training dataset for different values of m and then uses it to make predictions on the testing dataset. It then calculates the MSE loss between the predicted values and ground truth values and stores it in out for later analysis.

related categories

gistlibby LogSnag