function radar ( max, p, ps, pn );

%*****************************************************************************80
%
%% RADAR analyzes the problem of radar detection and identification.
%
%  Discussion:
%
%    As the number of observations N increases, compute
%    * PD, the probability of detection.
%    * PFA, the probability of false alarm.
%    Present results graphically.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Parameters:
%
%    Input, integer MAX, the total number of observations.
%
%    Input, real P, the minimum decimal fraction of observations to 
%    declare a target.
%
%    Input, real PS, the 0/1 signal threshold probability.
%
%    Input, real PN, the 0/1 noise threshhold probability.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RADAR:\n' );
  fprintf ( 1, '  As the number of observations N increases, compute\n' );
  fprintf ( 1, '  * PD, the probability of detection.\n' );
  fprintf ( 1, '  * PFA, the probability of false alarm.\n' );
  fprintf ( 1, '  Present results graphically.\n' );

  ops = 1 - ps;
  opn = 1 - pn;

  for n = 1 : max
    m = floor ( n * p ) + 1;
    PD = 0;
    PFA = 0;
    for k = m : n
      factor = binomial ( n, k );
      PD = PD + factor * ( ps ^ k ) * ops ^ ( n - k );
      PFA = PFA + factor * ( pn ^ k ) * opn ^ ( n - k );
    end
    pdv(n) = PD;
    pfav(n) = PFA;
  end
%
%  Graphics.
%
  clf
  x = 1 : max;
  plot ( x, pdv, '-', x, pfav, ':' )
  grid on
  legend('Probability of Detection','Probability of False Alarm')
  xlabel('number of observations')
  ylabel('probability')
  title('Probabilities of Detection and False Alarm for Radar Binary Integration')
  print ( '-dpng', 'radar.png' )

  return
end
