%% matlab_demo_08.m
%%
%% CSS 581, Introduction to Machine Learning, Winter 2014
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-01-31
%% modified by J. Jeffry Howbert, 2014-01-30

%% various methods of regression on prostate cancer data
% use this section for all regression methods
clear all;
close all;
load prostate;
nTrain = size( trainData, 1 );
nTest = size( testData, 1 );

% % standardize each column of training data to 0 mean and variance 1 (optional)
% [ trainData, mu, sigma ] = zscore( trainData );
% % also apply mu and sigma from training zscore to test data
% testData = testData - repmat( mu, size( testData, 1 ), 1 );
% testData = testData ./ repmat( sigma, size( testData, 1 ), 1 );

% global mean model
globalPred = mean( trainResponse );
trainPred = repmat( globalPred, nTrain, 1 );
testPred = repmat( globalPred, nTest, 1 );
meanSqErrorTrain = sum( ( trainPred - trainResponse ) .^ 2 ) / nTrain;
fprintf( 1, 'mean squared error for training samples = %8.4f\n', meanSqErrorTrain ); 
meanSqErrorTest = sum( ( testPred - testResponse ) .^ 2 ) / nTest;
fprintf( 1, 'mean squared error for test samples     = %8.4f\n', meanSqErrorTest ); 

% % linear regression
% % add column of ones so model will have intercept
% trainData = ( [ ones( size( trainData, 1 ), 1 ), trainData ] );
% testData = ( [ ones( size( testData, 1 ), 1 ), testData ] );
% beta = mvregress( trainData, trainResponse );
% trainPred = trainData * beta;
% testPred = testData * beta;
% meanSqErrorTrain = sum( ( trainPred - trainResponse ) .^ 2 ) / nTrain;
% fprintf( 1, 'mean squared error for training samples = %8.4f\n', meanSqErrorTrain ); 
% meanSqErrorTest = sum( ( testPred - testResponse ) .^ 2 ) / nTest;
% fprintf( 1, 'mean squared error for test samples     = %8.4f\n', meanSqErrorTest ); 

% % regression tree
% tr = classregtree( trainData, trainResponse, 'method', 'regression' );
% pruneLevel = 0;     % trained tree has 9 pruning levels
% % %%&& uncomment these two lines to loop over all pruning levels
% % for pr = 0 : 9
% %     pruneLevel = pr;
% % %%&&
% trainPred = eval( tr, trainData, pruneLevel );
% testPred = eval( tr, testData, pruneLevel );
% fprintf( 1, 'pruning level = %d\n', pruneLevel );
% meanSqErrorTrain = sum( ( trainPred - trainResponse ) .^ 2 ) / nTrain;
% fprintf( 1, '   mean squared error for training samples = %8.4f\n', meanSqErrorTrain ); 
% meanSqErrorTest = sum( ( testPred - testResponse ) .^ 2 ) / nTest;
% fprintf( 1, '   mean squared error for test samples     = %8.4f\n', meanSqErrorTest ); 
% % end       %%&& uncomment this line to loop over all regression tree pruning levels

% % nearest neighbor regression
% k = 1;
% % for k = 1 : 25        %%&& uncomment this line to loop over k
% trainPredIdx = knnsearch( trainData, trainData, 'K', k );   %%&& knnsearch() doesn't exist in R2009a!!
% trainPredAll = trainResponse( trainPredIdx );  % get responses of all k-nearest neighbors for each training sample
% trainPred = mean( trainPredAll, 2 );           % take mean of training responses for each training sample
% testPredIdx = knnsearch( trainData, testData, 'K', k );
% testPredAll = trainResponse( testPredIdx );    % get responses of all k-nearest neighbors for each test sample
% testPred = mean( testPredAll, 2 );             % take mean of training responses for each test sample
% fprintf( 1, 'number of nearest neighbors = %d\n', k );
% meanSqErrorTrain = sum( ( trainPred - trainResponse ) .^ 2 ) / nTrain;
% fprintf( 1, '   mean squared error for training samples = %8.4f\n', meanSqErrorTrain ); 
% meanSqErrorTest = sum( ( testPred - testResponse ) .^ 2 ) / nTest;
% fprintf( 1, '   mean squared error for test samples     = %8.4f\n', meanSqErrorTest );
% % end                   %%&& un comment this line to loop over k
