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 in matlab

main.m
function out = mse_vs_m(x_tr, t_tr, x_te, t_te)
%Function to compute empirical MSE test loss
%Inputs:
%x_tr: Training input data matrix
%t_tr: Training target data vector
%x_te: Test input data matrix
%t_te: Test target data vector
%Outputs:
%out: Empirical MSE test loss

n_tr = length(t_tr); %Number of training samples
n_te = length(t_te); %Number of test samples
out = zeros(1, n_te); %Initialize the output variable

%For each value of m, compute the corresponding MSE value on test data
for m = 1:n_tr
    x_m = x_tr(1:m, :); %Training input data matrix for m samples
    t_m = t_tr(1:m); %Training target data vector for m samples
    
    %Compute weight vector using training data
    w = (x_m'*x_m)\x_m'*t_m;
    
    %Compute predicted target values for test data
    y_te = x_te*w;
    
    %Compute the MSE value for this value of m
    out(m) = sum((t_te-y_te).^2)/n_te;
end
end
893 chars
30 lines

In this function, we first compute the number of training and test samples using the length function. We initialize the output variable out as a zero vector with length equal to the number of test samples. We then loop over each value of m, where m is the number of training samples to be used for learning. For each value of m, we extract the first m samples from the training data and compute the weight vector w using the closed-form solution. We then predict the target values for the test data and compute the corresponding MSE value using the sum-square error formula. Finally, we store this value in the out vector. The function returns the out vector which contains the empirical MSE test loss for each value of m.

gistlibby LogSnag