
X_train = load('01_x-train.txt');
Y_train = load('01_y-train.txt');

X_test = load('01_x-test.txt');
Y_test = load('01_y-test.txt');

% Find such w, that minimizes L2 error.


[numSamplesTest numFeatures] = size(X_test);
[numSamplesTrain numFeatures] = size(X_train);

trainError = zeros(1, 100);
testError = zeros(1, 100);
w(1:100, 1:numFeatures) = 0;

k = 0;
for d = linspace(0, 5, 100)
    
    k = k + 1;
    
    XtSize = size(X_train);
    w(k, :) = inv(X_train'*X_train + d * eye(XtSize(2))) * X_train'*Y_train;
    
    for i=1:numSamplesTrain;
        trainError(k) = trainError(k) + (X_train(i, :) * w(k, :)' - Y_train(i))^2;
    end
    
    for i=1:numSamplesTest;
        testError(k)  = testError(k)  + (X_test(i, :) * w(k, :)' - Y_test(i))^2;
    end

    testError(k) = testError(k) / numSamplesTest;
    trainError(k) = trainError(k) / numSamplesTrain;
    
end

plot(linspace(0, 5, 100), testError, 'r');
hold on
plot(linspace(0, 5, 100), trainError, 'g');
hold off