main.m747 chars32 lines
Explanation:
The function mse_vs_m takes four input arguments: x_tr, t_tr, x_te, and t_te. These arguments correspond to the training and test data inputs and targets, respectively.
The function first defines a range of complexity levels, m_range, which is a vector of integers from 1 to 10.
Then, the function initializes the output matrix, out, as a matrix of zeros with dimensions equal to the length of m_range.
Next, the function iterates through all the complexity levels in m_range, and for each level, trains a polynomial regression model using the training data x_tr and t_tr. The model is trained using the polyfit function with degree m.
The function then computes the predictions of the trained model on the test data x_te, and computes the mean squared error (MSE) between these predictions and the actual test targets t_te.
The computed MSE for each complexity level is stored in the corresponding element of the output matrix out.
Finally, the function plots the MSE vs. model complexity curve using the plot function. The x-axis represents the model complexity (i.e., degree of the polynomial regression model), while the y-axis represents the computed MSE. The plot should help in identifying the optimal complexity level that minimizes the test error.
gistlibby LogSnag