function [ n, x_sum, x_min, x_mean, x_max, x_var, x_std ] = stats ( x )

%*****************************************************************************80
%
%% stats() does stats.
%
%  Discussion:
%
%    This function computes statistical quantities for a sequence of values
%    X which are supplied one at a time, like a data stream.  The statistical
%    quantities are updated every time a new value of X is supplied.
%
%    Because we are dealing with a data stream, and not saving the previous
%    data values, it is not possible to compute the median and mode.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 May 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, the next item in the sequence.
%    If X is set to Inf, then its value is ignored, and the current data
%    is returned.
%    If no argument is specified, then all internal data is reset to 0.
%
%  Output:
%
%    integer N: the number of items in the sequence.
%
%    real X_SUM: the sum of the values.
%
%    real X_MIN, X_MEAN, X_MAX: the minimum, mean, and maximum values.
%
%    real X_VAR, X_STD: the variance and standard deviation.
%
  persistent n_internal
  persistent x_max_internal
  persistent x_mean_internal
  persistent x_min_internal
  persistent x_std_internal
  persistent x_sum_internal
  persistent x_var_internal

  if ( isempty ( n_internal ) )
    n_internal = 0;
    x_max_internal = -Inf;
    x_mean_internal = 0.0;
    x_min_internal = Inf;
    x_std_internal = 0.0;
    x_sum_internal = 0.0;
    x_var_internal = 0.0;
  end
%
%  No input.  Reset internal data to 0.
%
  if ( nargin < 1 )
    n_internal = 0;
    x_max_internal = -Inf;
    x_mean_internal = 0.0;
    x_min_internal = Inf;
    x_std_internal = 0.0;
    x_sum_internal = 0.0;
    x_var_internal = 0.0;
%
%  Input that is not Inf.  Process X.
%
  elseif ( x ~= Inf )
    n_internal = n_internal + 1;
    x_max_internal = max ( x_max_internal, x );
    x_min_internal = min ( x_min_internal, x );
    x_sum_internal = x_sum_internal + x;
    x_mean_old = x_mean_internal;
    x_mean_internal = x_sum_internal / n_internal;
    if ( n_internal == 1 )
      x_var_internal = 0.0;
      x_std_internal = 0.0;
    else
      x_var_internal = ( x_var_internal * ( n_internal - 2 ) ...
          + ( x - x_mean_old ) * ( x - x_mean_internal ) ) / ( n_internal - 1 );
      x_std_internal = sqrt ( x_var_internal );
    end
  end
%
%  Set output values.
%
  n      = n_internal;
  x_max  = x_max_internal;
  x_min  = x_min_internal;
  x_sum  = x_sum_internal;
  x_mean = x_mean_internal;
  x_var  = x_var_internal;
  x_std  = x_std_internal;

  return
end

