%
% Demo of linear regression on auto-mpg dataset
%

% Fitting a least-squares linear regression is easily accomplished in MATLAB
% using the backslash operator: '\'.
clear all;

data = load('Data/auto-mpg.data');

% Plot pairs of feature
figure(1)
plot(data(:,2),data(:,1),'*');
title('Visualization of Mpg dependency on Cylinders feature.')
figure(2)
plot(data(:,4),data(:,1),'o');
title('Visualization of Mpg dependency on Horsepower feature.')

% divide training and testing
[trainin, testin] = divideset(data, 30);
% specify features and target columns
traintgt= trainin(:,1);
trainin = trainin(:,2:end);
% normalize training data
[meantrtgt,stdtrtgt,traintgt_norm]= normalize(traintgt,[],[]);
[meantr,stdtr,trainin_norm] = normalize(trainin,[],[]);

testtgt= testin(:,1);
testin = testin(:,2:end);
% normalize testing data
[meantstgt,stdvtgt,testtgt_norm]= normalize(testtgt,[],[]);
[meants,stdts,testin_norm] = normalize(testin,[],[]);

% append a column of ones
trainin = [ones(size(trainin,1),1) trainin];
testin = [ones(size(testin,1),1) testin];
trainin_norm = [ones(size(trainin_norm,1),1) trainin_norm];
testin_norm = [ones(size(testin_norm,1),1) testin_norm];

% recover weights vector
beta = trainin\traintgt;
testout = testin*beta;
error = sum((testout-testtgt).^2)/size(testtgt,1)

% Test to check if normalization help to improve accuracy
beta_norm = trainin_norm\traintgt_norm;
testout_norm = testin_norm*beta_norm;
error_norm = sum((testout_norm-testtgt_norm).^2)/size(testtgt_norm,1)

return