function [sn, sp] = loocv(fname, N,P,varargin)
%LOOCV - leave-one-out cross validation helper function
%
% [SN, SP] = LOOCV(FNAME, N, P)
%
% This function does L-1-O cross-validation using classifier function FNAME.
% 
% INPUTS
%
%  P -- the set of positive examples, each column is a different datapoint, 
%  N -- the set of negative examples, each column is a different datapoint, 
%  FNAME -- a string containing the name of the classifier
%     function, the function is called as follows: 
%         SCORE = FNAME(TESTPOINT,N,P)
%     Where N and P are the positive and negative training set, and
%     TESTPOINT is the test point.  SCORE is the score of the TESTPOINT
%     under the classifier trained on N and P.
%
% OUTPUTS
%
%  SN(I) is the score of the I-th negative example
%  SP(I) is the score of the I-th positive example
%
% Can also be called with additional arguments that are passed
% directly to the classifier function.
%
% Example usage:
% doplot = 1;
% N = randn(10,100);
% P = randn(10,100) + ones(10,100);
% [sn, sp] = loocv('kdeClass', N, P);
% ROCarea(sp, sn, doplot);
%
% (c) copyright Quaid Morris, 2003.

nn = size(N,2);
np = size(P,2);

for ii = 1:nn
  thisN = N(:,[1:ii-1 ii+1:end]);
  sn(ii) = feval(fname,N(:,ii),thisN,P,varargin{:});
end

for ii = 1:np
  thisP = P(:,[1:ii-1 ii+1:end]);
  sp(ii) = feval(fname,P(:,ii),N,thisP,varargin{:});
end
