function aircon ( )

%*****************************************************************************80
%
%% AIRCON does an exercise in logistic regression.
%
%  Modified:
%
%    03 Ocober 2018
%
  fprintf ( '\n' );
  fprintf ( 'AIRCON:' );
  fprintf ( '  Seek parameters for logistic regression example.\n' );
  fprintf ( '  We have M examples of N-dimensional data X.\n' );
  fprintf ( '  For a model\n' );
  fprintf ( '    h(x) = 1 / ( 1 + exp ( - w(1) * x(1) - w(2) * x(2) - b )\n' );
  fprintf ( '  estimate parameters w(1:2) and b.\n' );
  fprintf ( '\n' );
  fprintf ( '  Use gradient descent\n' );
%
%  Read the data: (humidity, temperature, comfortable?)
%
  data = load ( 'aircon.txt' );
  [ m, n ] = size ( data );
  fprintf ( '\n' );
  fprintf ( '  Input data has dimensions M = %d, N = %d\n', m, n );
%
%  Rename things.
%
  x(:,1:2) = data(:,1:2);
  y(:,1) = data(:,3);

  xmin = min ( x );
  xmax = max ( x );

  x(:,1) = ( x(:,1) - xmin(1) ) / ( xmax(1) - xmin(2) );
  x(:,2) = ( x(:,2) - xmin(2) ) / ( xmax(2) - xmin(2) );
  x = [ x, ones(m,1) ];
  n = 3;
%
%  Select alpha, and initial w and b.
%
  alpha = 1.0;
  w = zeros ( 1, n );
  kmax = 10000;

  for k = 1 : kmax

    y2 = 1.0 ./ ( 1 + exp ( - x * w' ) );

    for j = 1 : n
      w(j) = w(j) - ( alpha / m ) .* ( y2 - y )' * x(:,j);
    end

    J(k) = ( 1 / m ) * sum ( - y' * log ( y2 ) - ( 1 - y )' * log ( 1 - y2 ) );

  end
  fprintf ( '\n' );
  fprintf ( '  Using ALPHA = %g\n', alpha );
  fprintf ( '  Final value of J = %g\n', J(kmax) );
  fprintf ( '  Final W = (%g,%g), B = %g\n', w(1:2), w(3) );
%
%  Display the data.
%
  i0 = find ( y(:) == 0 );
  i1 = find ( y(:) == 1 );

  ymin = min(x(:,2));
  ymax = max(x(:,2));

  clf
  hold on
  plot ( x(i0,1), x(i0,2), 'r.', 'markersize', 30 );
  plot ( x(i1,1), x(i1,2), 'b.', 'markersize', 30 );
  xlabel ( '<-- Humidity -->' );
  ylabel ( '<-- Temperature -->' );
  title ( 'Blue = Comfortable, Red = Not' );
  grid ( 'on' );
  plot ( [-w(3)/w(1), (-w(3)-w(2))/w(1)], [0,1], 'k-', 'linewidth', 3 );
  grid ( 'on' )
  hold off

  return
end
