Write a function to implement regularization based training procedure for 1D reg
ID: 3866974 • Letter: W
Question
Write a function to implement regularization based training procedure for 1D regression. The function should have the following form: function a = mytrain(train_x, train_y,n, lambda), where train_x is a column vector containing all the input points in the training, train_y is a column vector containing all the output points in the training, n is the order of the polynomial model, lambda is a non-negative regularization parameter, and a is a column vector containing coefficients of the polynomial model. Write a function to implement testing procedure for 1D regression. The function should have the following form: function test_y = mytest(test_x, a), where test_x is a column vector containing all the input points in the test, a is a column vector containing coefficients of the polynomial model, and test_y is a column vector containing all the output points in the test. Write a script to do the following experiment Create 10 input points for training. They should be evenly distributed between 0 and 1. Create 10 corresponding output points for training as follows: cos(2* pi*x) + 0.3 randn(10, 1) Create 100 input points for testing. They should be evenly distributed between 0 and 1. Generate models for lambda = 0, and n = 1, 2, 3, 4, 5, 6, 7, 8, 9, respectively. Generate models for n = 3, and lambda = 1e -8, 1e-5, 1e-2, 1, respectively. Generate models for n = 9, and lambda = 1e-8, 1e-5, 1e-2, 1, respectively. For each model, superimpose the testing results onto cos(2*pi*) Write a report to summarize and discuss your resultsExplanation / Answer
function model = linReg(X, t, lambda)
% Fit linear regression model y=w'x+w0
% Input:
% X: d x n data
% t: 1 x n response
% lambda: regularization parameter
% Output:
% model: trained model structure
if nargin < 3
lambda = 0;
end
d = size(X,1);
idx = (1:d)';
dg = sub2ind([d,d],idx,idx);
xbar = mean(X,2);
tbar = mean(t,2);
X = bsxfun(@minus,X,xbar);
t = bsxfun(@minus,t,tbar);
XX = X*X';
XX(dg) = XX(dg)+lambda; % 3.54 XX=inv(S)/beta
% w = XX(X*t');
U = chol(XX);
w = U(U'(X*t')); % 3.15 & 3.28
w0 = tbar-dot(w,xbar); % 3.19
model.w = w;
model.w0 = w0;
model.xbar = xbar;
%% for probability prediction
beta = 1/mean((t-w'*X).^2); % 3.21
% alpha = lambda*beta; % lambda=a/b P.153 3.55
% model.alpha = alpha;
model.beta = beta;
model.U = U;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.