%
% SBMLR_DEMO - demonstrate sparse multinomial logistic regression
%

% start from a clean slate

clear all

% load data

load wine

% cross-validate

fprintf(1, 'performing cross-validation...\n');

folds     = 10;
[ntp,d]   = size(x);
[ntp,c]   = size(t);
y         = zeros(size(t));
partition = mod(1:ntp, folds) + 1;

for i=1:folds

   alpha = sbmlr(x(partition~=i,:), t(partition~=i,:)); 

   y(partition==i,:) = x(partition==i,:)*alpha;

   nw(i) = sum(alpha(:)==0);

end

y = exp(y);
y = y./repmat(sum(y,2), 1, c);

% compute performance statistics

fprintf(1, 'evaluating performance statistics...\n');

logL             = sum(log(y(t(:)==1)));
[foo,predicted]  = max(y');
[foo,true]       = max(t'); 
confusion_matrix = zeros(c);
idx              = sub2ind([c c], predicted, true);

for i=1:length(idx)

   confusion_matrix(idx(i)) = confusion_matrix(idx(i)) + 1;

end

fprintf(1, 'log likelihood = %f\n', logL);
fprintf(1, 'mean sparsity  = %6.2f%%\n', 100*mean(nw)/(c*d));

confusion_matrix

% bye bye...

