function will_you_be_alive_test ( )

%*****************************************************************************80
%
%% will_you_be_alive_test tests will_you_be_alive.
%
%  Modified:
%
%    08 May 2019
%
%  Author:
%
%   John Burkardt
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
% 
  addpath ( '../will_you_be_alive' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'will_you_be_alive_test\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '  Test will_you_be_alive.\n' );

  airplane_seat ( 1000, 100 );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'BEFORE:\n' );
  fprintf ( 1, '  Probability of flipping 4 heads before 7 tails.\n' );
  prob = 0.45;
  p = before ( prob );
  fprintf ( 1, '  If probability of one head is %g, probability of success = %g\n', prob, p );
  prob = 0.50;
  p = before ( prob );
  fprintf ( 1, '  If probability of one head is %g, probability of success = %g\n', prob, p );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'BLACK + DRAW:\n' );
  fprintf ( 1, '  Draw one marble at a time from a bag of B black and W white marbles.\n' );
  fprintf ( 1, '  What is the probability the last marble is black?\n' );
  fprintf ( 1, '  Exact result is 1/2!\n' );
  b = 1;
  w = 1;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 1;
  w = 10;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 2;
  w = 3;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 3;
  w = 3;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 5;
  w = 4;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 7;
  w = 9;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 18;
  w = 13;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );
  b = 15;
  w = 39;
  p = black ( b, w );
  fprintf ( 1, '  %2d  %2d  %g\n', b, w, p );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'DD:\n' );
  fprintf ( 1, '  Two darts land randomly in the unit circle.\n' );
  fprintf ( 1, '  What is the probability that they are at least 1 unit apart?\n' );
  fprintf ( 1, '\n' );
  p = dd ( );
  exact = 3.0 * sqrt ( 3.0 ) / 4.0 / pi;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  p = ds ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'DS:\n' );
  fprintf ( 1, '  Expect to toss a fair die %g times in order to see\n' );
  fprintf ( 1, '  two consecutive 6''s\n' );

  final ( );

  n = 9;
  p = 0.1;
  flips ( n, p );

  n = 9;
  p = 0.99;
  flips ( n, p );

  galileo ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'GOLF:\n' );
  fprintf ( 1, '  Probability a golf ball randomly landing in unit square\n' );
  fprintf ( 1, '  is closer to center than to any edge.\n' );
  fprintf ( 1, '\n' );
  p = golf ( );
  exact = ( 4.0 * sqrt ( 2.0 ) - 5.0 ) / 3.0;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  gr ( 9, 1, 0.50 );
  gr ( 90, 10, 0.50 );
  gr ( 9, 1, 0.45 );
  gr ( 99, 1, 0.40 );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'INSIDE\n' );
  fprintf ( 1, '  What is the probability that, choosing three points at random\n' );
  fprintf ( 1, '  on the unit circle, the origin is inside the resulting triangle?\n' );
  fprintf ( 1, '\n' );
  p = inside ( );
  exact = 0.25;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'JB:\n' );
  fprintf ( 1, '  Players A and B toss a fair die, until a 1 is rolled.\n' );
  fprintf ( 1, '  On round 1, A gets 1 toss, then B gets 1 toss.\n' );
  fprintf ( 1, '  On round k, A gets k tosses, then B gets k tosses.\n' );
  fprintf ( 1, '  What is the probability that A wins?\n' );
  fprintf ( 1, '\n' );
  p = jb ( );
  exact = 0.596794;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  n = 41;
  p1 = 0.99;
  p2 = liar ( n, p1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'LIAR:\n' );
  fprintf ( 1, '  Each person lies with probability %g\n', p1 );
  fprintf ( 1, '  There are %d persons\n', n );
  fprintf ( 1, '  Probability a true statement is transmitted as true is %g\n', p2 );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'LONG:\n' );
  fprintf ( 1, '  A unit stick is randomly broken in two.\n' );
  fprintf ( 1, '  Then the longer piece is randomly broken again.\n' );
  fprintf ( 1, '  What is the probability that the three pieces can form a triangle?\n' );
  fprintf ( 1, '\n' );
  p = long ( );
  exact = 2.0 * log ( 2.0 ) - 1.0;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'MARKS:\n' );
  fprintf ( 1, '    N    Prob  Exact\n' );
  fprintf ( 1, '\n' );
  for n = 2 : 9
    p = marks ( n );
    fprintf ( 1, '  %2d  %g  %g\n', n, p, 2.0 / ( n + 1 ) );
  end

  newton ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'OBTUSE1:\n' );
  fprintf ( 1, '  Triangle has side 1 of length 1, other two side lengths\n' );
  fprintf ( 1, '  chosen uniformly unit random.  What are chances that the\n' );
  fprintf ( 1, '  triangle is obtuse?\n' );
  fprintf ( 1, '\n' );
  p = obtuse1 ( );
  exact = 0.5 * pi - 1.0;
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'OBTUSE2:\n' );
  fprintf ( 1, '  A stick of unit length is broken into three pieces.\n' );
  fprintf ( 1, '  What are chances that an obtuse triangle is formed?\n' );
  fprintf ( 1, '\n' );
  p = obtuse2 ( );
  exact = 9.0 - 12.0 * log ( 2.0 );
  fprintf ( 1, '  Estimate = %g, exact is %g\n', p, exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'PLUMS:\n' );
  fprintf ( 1, '  Expected distance to surface of closest of n plums\n' );
  fprintf ( 1, '  in a unit spherical pudding.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  N  Estimate  Exact\n' );
  fprintf ( 1, '\n' );
  n = 5;
  s = plums ( n );
  fprintf ( 1, '  %2d  %g  %g\n', n, s, 1.0 / ( 3 * n + 1 ) );
  n = 9;
  s = plums ( n );
  fprintf ( 1, '  %2d  %g  %g\n', n, s, 1.0 / ( 3 * n + 1 ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'PP:\n' );
  fprintf ( 1, '  Probability of winning pingpong, if a player has.\n' );
  fprintf ( 1, '  probability P of winning a single point.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  P  Estimate  Exact\n' );
  fprintf ( 1, '\n' );
  p = 0.3;
  prob = pp ( p );
  exact = 0.0264;
  fprintf ( 1, '  %g  %g  %g\n', p, prob, exact );
  p = 0.4;
  prob = pp ( p );
  exact = 0.1744;
  fprintf ( 1, '  %g  %g  %g\n', p, prob, exact );
  p = 0.5;
  prob = pp ( p );
  exact = 0.5;
  fprintf ( 1, '  %g  %g  %g\n', p, prob, exact );
  p = 0.6;
  prob = pp ( p );
  exact = 0.8256;
  fprintf ( 1, '  %g  %g  %g\n', p, prob, exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'RATIO1:\n' );
  fprintf ( 1, '  Probability a random ratio is greater than K\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  K  Estimate  Exact\n' );
  fprintf ( 1, '\n' );
  for k = 2 : 9
    p = ratio1 ( k );
    fprintf ( 1, '  %2d  %g  %g\n', k, p, 1 / k );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, 'RATIO2:\n' );
  fprintf ( 1, '  Probability a random ratio is greater than K\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  K  Estimate\n' );
  fprintf ( 1, '\n' );
  for k = 2 : 9
    p = ratio2 ( k );
    fprintf ( 1, '  %2d  %g\n', k, p );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, 'SPAGHETTI:\n' );
  fprintf ( 1, '  Expected number of loops when randomly joining spaghetti ends\n' );
  n = 100;
  e = spaghetti ( n );
  fprintf ( 1, '  Using %d spaghetti strands, expected number of loops is %g\n', n, e );
  n = 1000;
  e = spaghetti ( n );
  fprintf ( 1, '  Using %d spaghetti strands, expected number of loops is %g\n', n, e );
  n = 10000;
  e = spaghetti ( n );
  fprintf ( 1, '  Using %d spaghetti strands, expected number of loops is %g\n', n, e );

  k = 3;
  n = 11;
  steve2 ( k, n );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'SQUARE_ADJ:\n' );
  fprintf ( 1, '  Expected distance between random points on adjacent\n' );
  fprintf ( 1, '  sides of a unit square.\n' );
  s = square_adj ( );
  fprintf ( 1, '  Exact value is 0.7652\n' );
  fprintf ( 1, '  Estimate is    %g\n', s );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'SQUARE_ANY:\n' );
  fprintf ( 1, '  Expected distance between random points in\n' );
  fprintf ( 1, '  a unit square.\n' );
  s = square_any ( );
  fprintf ( 1, '  Exact value is 0.5214\n' );
  fprintf ( 1, '  Estimate is    %g\n', s );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'SQUARE_STS:\n' );
  fprintf ( 1, '  Expected distance between random points on opposite\n' );
  fprintf ( 1, '  sides of a unit square.\n' );
  s = square_sts ( );
  fprintf ( 1, '  Exact value is 1.0766\n' );
  fprintf ( 1, '  Estimate is    %g\n', s );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'SQUASH:\n' );
  fprintf ( 1, '  What is the probability of winning at squash:\n' );
  fprintf ( 1, '  P: if you serve first,\n' );
  fprintf ( 1, '  Q: if you serve second.\n' );
  fprintf ( 1, '\n' );
  [ p, q ] = squash ( );
  p_exact = 0.534863456;
  q_exact = 0.465136543;
  fprintf ( 1, '  P Estimate = %g, exact is %g\n', p, p_exact );
  fprintf ( 1, '  Q Estimate = %g, exact is %g\n', q, q_exact );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TOP:\n' );
  fprintf ( 1, '  Winning ratios for dreidel\n' );
  fprintf ( 1, '\n' );
  for k = 2 : 5
    winnings = top ( k );
    fprintf ( 1, '  %2d:  ', k );
    for i = 1 : k
      fprintf ( 1, '  %g', winnings(k) );
    end
    fprintf ( 1, '\n' );
  end

  p = twins ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TWINS\n' );
  fprintf ( 1, '  Estimate the probability 2 twins in 20 students will be\n' );
  fprintf ( 1, '  randomly assigned the same lab group of 4 students.\n' );
  fprintf ( 1, '  Estimate is %g, exact is %g\n', p, 3/19 );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'will_you_be_alive_test\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../will_you_be_alive' );

  return
end
