function [RR, FP] = ROCIII(x, sigma, M)

% [RR, FP] = ROCIII(x, sigma, filenanem)
%
% Compute Rate of Classification (ROC) curves, smoothed using
% Parzen Estimation (with gaussian kernel, standard deviation = sigma).
%
% x 		= [1,N], the domain of pos and neg.
% sigma  = [scalar], the width of the parzen kernel.
%
% M is a matrix of data columns with the following format:
%
% idnum  weight positive(0,1) spectra? image? metal? P(X|spec) P(X|image) P(X|met)....
% P(X|image,spec)  P(X|image, met)  P(X|spec,met)  P(X|image,spec,met).
%
% Only compute ROC curves using those samples for which all relevant sensors are available.
%
% RR, FP	= matrices of Recognition Rate and False Positives. (Each column a result).
%
% Liam Pedersen, Kiva Han (CMU), Feb 1999.
% version 1.0
% Liam Pedersen, NASA ARC, August 1999
% version 2.0

% specify columns of M and dependencies
[N,C] = size(M);
PosC = 3;
WeightC = 2;
spectraC = 4; 
imageC = 5; 
metalC = 6;
% dependency masks:
%              1  2  3  4  5  6  7  8  9 10 11 12 13
image_mask = ~[0  0  0  0  0  0  0  1  0  1  1  0 1];
spec_mask  = ~[0  0  0  0  0  0  1  0  0  1  0  1 1];
met_mask   = ~[0  0  0  0  0  0  0  0  1  0  1  1 1];

% Seperate matrix M into positive (pos) and negative (neg) cases, and create weight 
% vectors for each column
ip = 1; 
in = 1;
for i = 1:N
   if (M(i,PosC)==1)   % positive example.
      pos(ip,:) = M(i,:); 
      mask = (M(i,spectraC)|spec_mask) & (M(i,imageC)|image_mask) & (M(i,metalC)|met_mask);
      pW(ip,:) = M(i,WeightC)* mask;
      ip = ip + 1;
   else                % negative example
      neg(in,:) = M(i,:);
      mask = (M(i,spectraC)|spec_mask) & (M(i,imageC)|image_mask) & (M(i,metalC)|met_mask);
      nW(in,:) = M(i,WeightC)* mask;

      in = in + 1;
   end
end

[Np, T] = size(pos);
[Nn, T] = size(neg);
dx = [diff(x) 0]';

for c = 7:C
   pos_pdf = parzen_pdf(x, pos(:,c), sigma, pW(:,c));
   neg_pdf = parzen_pdf(x, neg(:,c), sigma, nW(:,c));
   
   RR(:,c-6) = 1 - cumsum(dx.*pos_pdf);
   FP(:,c-6) = 1 - cumsum(dx.*neg_pdf);
end
