function std = std_compute ( chain_num, gen_index, gen_num, par_num, z )

%*****************************************************************************80
%
%% STD_COMPUTE computes the current standard deviations, for each parameter.
%
%  Discussion:
%
%    The computation encompasses all chains and generations up to the
%    current ones.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 January 2018
%
%  Author:
%
%    Original FORTRAN90 version by Guannan Zhang.
%    MATLAB verbion by John Burkardt.
%
%  Input parameters:
%
%    Input, integer CHAIN_NUM, the total number of chains.
%    3 <= CHAIN_NUM.
%
%    Input, integer GEN_INDEX, the current generation.
%    1 <= GEN_INDEX <= GEN_NUM.
%
%    Input, integer GEN_NUM, the total number of generations.
%    2 <= GEN_NUM.
%
%    Input, integer PAR_NUM, the total number of parameters.
%    1 <= PAR_NUM.
%
%    Input, real Z(PAR_NUM,CHAIN_NUM,GEN_NUM), the Markov chain 
%    sample data.
%
%  Output parameters:
%
%    Output, real STD(PAR_NUM), the standard deviations.
%
  for i = 1 : par_num

    mean = 0.0;
    for k = 1 : gen_index
      for j = 1 : chain_num
        mean = mean + z(i,j,k);
      end
    end
    mean = mean / chain_num / gen_index;

    std(i) = 0.0;
    for k = 1 : gen_index
      for j = 1 : chain_num
        std(i) = std(i) + ( z(i,j,k) - mean ) ^ 2;
      end
    end

    std(i) = sqrt ( std(i) / ( chain_num * gen_index - 1 ) );

  end

  return
end

