%
% DEMO.M - demonstrate the use of BLogReg to classifiy the Leukaemia dataset
%

% start from a clean slate

clear all

% load data

load leukaemia

% merge training and test sets

x       = [x_train ; x_test];
t       = [t_train ; t_test];
[t,idx] = sort(t);
x       = x(idx,:);
[ntp,d] = size(x);

% impute missing test data using the mean

idx = find(sum(x==-999));

for i=idx

   x(x(:,i)==-999,i) = mean(x(x(:,i)~=-999,i));

end

% standardise the data and add bias feature

offset = mean(x);
scale  = 1./std(x);
x      = x - repmat(offset, ntp, 1);
x      = x.*repmat(scale, ntp, 1);
x      = [ones(ntp, 1) x];

% perform leave-one-out cross-validation

mu    = zeros(ntp,1);
alpha = zeros(d+1,ntp);
index = 1:ntp;

for i=1:ntp

   alpha(:,i) = blogreg(x(index~=i,:), t(index~=i), 1e-6);

   mu(i) = 1./(1 + exp(-x(i,:)*alpha(:,i)));

end

fprintf(1, 'l-o-o error = %6.2f%%\n', 100*mean((mu>=0.5) ~= (t==+1)));

% bye bye...

