
function DAT = hot_spot(LC,h,num)

% This program identifies local 'hot spots'.
% INPUTS:
%     (i)   LC = location-class file [LC(i)=(Xi, Yi, Ci)]
%           [NOTE: Ci = 1 or 2, depending on class of point].
%     (ii)  h   = fixed radius 
%	   (iii) num = number of points within radius h
% OUTPUTS: DAT = (Nx6) matrix with rows (Pi,Xi,Yi,Ci,Ri,Ni)
%                of the form:
%               Pi = event prob for point i
%               (Xi,Yi) = location of i
%               Ci = class of i
%               Ri = rank probability of i
%               Ni = total point count in radius h
       


[N,n] = size(LC); 

%Class count

n1 = 0 ;

i = 1 ;

while i <= N
   
   if LC(i,3) == 1
      
      n1 = n1 + 1 ;
      
   end
   
   i = i + 1 ; 
   
end

      
% Compute and Count Pairwise Distances.

D = zeros(N*(N-1)/2, 3) ;i = 1 ;

C = zeros(N,1) ;

C2 = zeros(N,1) ;

i = 1 ;

k = 1 ;  %counter for D.

while i < N
   
   j = i + 1 ;
   
   while j <= N
      
      D(k,1) = norm(LC(i,1:2) - LC(j,1:2));

      D(k,2) = i ;
      
      D(k,3) = j ;
      
      if D(k,1) <= h 
         
         C(i) = C(i) + 1 ;
         
         C(j) = C(j) + 1 ;
         
         if (LC(i,3)==2)&(LC(j,3)==2)
            
            C2(i) = C2(i) + 1 ;
            
            C2(j) = C2(j) + 1 ;
            
         end
         
         
      end
      
      j = j + 1 ;
      
      k = k + 1 ;
      
   end
   
   i = i + 1 ;
   
end


%Compute clustering probabilities

Pr = zeros(N,1) ;

i = 1 ;

while i <= N
   
   if C(i) < num
      
      Pr(i) = 0 ;
      
   else 
      
      k = min([C(i) n1 - 1]); %there are n1 - 1 other points      
      j = num ;
      
      while j <= k
         
         Pr(i) = Pr(i) + hyper(j, C(i), n1 - 1, N - 1) ;
         
         j = j + 1 ;
         
      end
            
   end
   
   i = i + 1 ;
   
end

DT = zeros(N,7) ; %last three columns are used later

DT(:,1) = Pr ;

DT(:,2:4) = LC ;

DT(:,6) = C2 ;

%Count nonzero probabilities

i = 1 ;

NUM = 0 ;

while i <= N,
   
   x = DT(i,1) ;
   
   if x > 0
      
      NUM = NUM + 1 ;
      
   end
   
   DT(i,7) = i ;
   
   i = i + 1 ;
   
end

%Compute final Probabilities

DT = sortrows(DT,1) ;


i = N - NUM + 1 ;

while i <= N   
   
   DT(i,5) = (i - N + NUM)/NUM ;
   
   i = i + 1 ;
   
end

DT = sortrows(DT,7) ; 

DAT = DT(:,1:6) ;
