% Generate Brownian and geometric Brownian paths:
% dX = mu*dt + sigma*dW
% and
% Y = exp(X)
%
% Jan 2017
clear
M = input('enter the number of paths: ');
N = input('enter the number of steps: ');
mu = input('enter mu: ');
sigma = input('enter sigma: ');
T = 1;
dt = T/N;

x = zeros(M,N+1);
w = randn(M,N);
callpayoff = zeros(M,1);

t = 0:dt:T;

% generate brownian paths:
for k=1:N
    x(:,k+1) = x(:,k) + mu*dt + sigma*sqrt(dt)*w(:,k);
end

% generate geometric brownian paths:
y = exp(x);

fprintf('expected value of x at 0.5 and 1 = %8.6f %8.6f\n',mean(x(:,N/2+1)),mean(x(:,N+1)));
fprintf('variance of x at t=0.5 and 1 = %8.6f %8.6f\n',var(x(:,N/2+1)),var(x(:,N+1)));
fprintf('variance of y at t=0.5 and 1 = %8.6f %8.6f\n',var(y(:,N/2+1)),var(y(:,N+1)));

figure

hold on

for l=1:M
    plot(t,x(l,:),'g');
end
xlabel('time'); 
ylabel('x');
title('Brownian Paths');
%axis([0 T -1 1]);

hold off

figure

hold on

for l=1:M
    plot(t,y(l,:),'r');
end
xlabel('time'); 
ylabel('y');
title('Geometric Brownian Paths');
%axis([0 T 0 5]);

hold off