% This is a simulation test for the OU process
% dS = alpha*(mu - S)*dt + sigma*S*dW
clear
M = input('enter the number of paths: ');
N = input('enter the number of steps: ');
S0 = input('enter S0: ');
alpha = input('enter alpha: ');
mu = input('enter mu: ');
sigma = input('enter sigma: ');
T = 1;
dt = T/N;

S = S0*ones(M,N+1);
w = randn(M,N);

t = 0:dt:T;

% generate OU paths:
for k=1:N
    S(:,k+1) = (1-alpha*dt+sigma*sqrt(dt)*w(:,k)).*S(:,k) + alpha*mu*dt;
end

hold on
for l=1:M
    plot(t,S(l,:),'g');
end
hold off
xlabel('time'); 
ylabel('S');
title('OU Paths');

