function Pneg = PrNeg(X,Pr,m,n)
% Recursive Octave/MATLAB function to compute the
% probability of hitting a negative element in a
% non-recombining tree by depth-first search--DFS.
% INPUTS:                                (Example)
%   X = data NRT              ([1 2 -1 3 -4 -5 6])
%   Pr = prob. NRT       ([1 .49 .51 .1 .2 .3 .4])
%   m = index of the current root              (1)
%   N = remaining tree depth, must be >0       (2)
% OUTPUTS:
%   Pneg = probability of first negatives in DFS
% EXAMPLE:
%  Pneg = PrNeg([1  2  -1  3  -4  -5  6],...
%    [1 .49 .51 .1 .2 .3 .4], 1,2) % Pneg=0.7100
%
  Pneg=0; % default value for several cases
  if n<0   % leaf termination condition for DFS
    return % zero contribution from below level N
  end  % ...so n>=0 hereafter
  if X(m)<0  % then this nonleaf is negative
    Pneg=Pr(m); % ...so count its probability
  else % otherwise this nonleaf is nonnegative
    P0=PrNeg(X,Pr,2*m,n-1);   % so check path 2m
    P1=PrNeg(X,Pr,2*m+1,n-1); % ...and path 2m+1
    Pneg=P0+P1; % ...and add their probabilities 
  end
  return % and pass the current Pneg value upwards
end
