function [ estimate, exact, error ] = stochastic_integral_ito ( n )

%*****************************************************************************80
%
%% stochastic_integral_ito() approximates the Ito integral of W(t) dW.
%
%  Discussion:
%
%    This function estimates the Ito integral of W(t) dW over 
%    the interval [0,1].
%
%    The estimates is made by taking N steps.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    20 March 2012
%
%  Author:
%
%    Desmond Higham
%
%  Reference:
%
%    Desmond Higham,
%    An Algorithmic Introduction to Numerical Simulation of
%    Stochastic Differential Equations,
%    SIAM Review,
%    Volume 43, Number 3, September 2001, pages 525-546.
%
%  Input:
%
%    integer N, the number of steps to take.
%
%  Output:
%
%    real ESTIMATE, the estimate of the integral.
%
%    real EXACT, the exact value of the integral.
%
%    real ERROR, the error in the integral estimate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'stochastic_integral_ito() approximates an Ito integral.\n' );

% rng ( 'shuffle' );
%
%  Set step parameters.
%
  tmax = 1.0;
  dt = tmax / n;
%
%  Set the increments.
%
  dw = sqrt ( dt ) * randn ( 1, n );
%
%  Set the cumulative sums.
%
  w = cumsum ( dw );
%
%  Approximate the Ito integral.
%
  estimate = sum ( [0,w(1:end-1)] .* dw );
%
%  Compare with the exact solution.
%
  exact = 0.5 * ( w(end)^2 - tmax );
  error = abs ( estimate - exact );

  return
end
