function x = gradient_descent_stochastic ( f, df, x, it_max, alpha )

%*****************************************************************************80
%
%% gradient_descent_stochastic(): stochastic gradient descent.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 July 2022
%
%  Author:
%
%    John Burkardt.
%
%  Input:
%
%    real value = F(X): evaluates the function.
%
%    real value = DF(X): evaluates the derivative of the function.
%
%    real X(N): the initial guess for the minimizer.
%
%    integer IT_MAX: the maximum number of steps.
%
%    real ALPHA: the learning rate.
%
%  Output:
%
%    real X(N): the estimated minimizer.
%
  n = length ( x );

  print_out = true;

  if ( print_out )
    fprintf ( 1, '\n' );
    fprintf ( 1, '   it    j      ||x||             ||f(x)||       ||J(x)||\n' );
    fprintf ( 1, '\n' );
  end

  for it = 0 : it_max

    if ( 0 < it )
      j = randi ( [ 1, n ], 1 );
      A = df ( x );
      x(j) = x(j) - alpha * A(:,j)' * f ( x );
    else
      j = 0;
    end

    if ( print_out )
      fprintf ( 1, '  %3d  %3d  %12.8g  %12.8g  %12.8g\n', ...
        it, ...
        j, ...
        norm ( x ), ...
        0.5 * ( norm ( f ( x ) ) )^2, ...
        norm ( df ( x ), 'fro' ) );
    end

  end

  return
end


