//
//  EXERCISE 3: STOCHASTIC DIFFERENTIAL EQUATIONS
//

function [r]=randn()
    r=rand(1,"normal");
endfunction

// --------------------------------- 
// I. ORNSTEIN - UHLENBECK PROCESS
// ---------------------------------

kappa=10; theta=1; sigma=0.25; // parameters of the process
x0=0.5; // initial value

dt=0.001; // time step
n=1000;   // number of steps

// Euler-Maruyama discretization 
x(1)=x0;
for i=1:n
    dw=sqrt(dt)*randn();
    dx=kappa*(theta-x(i))*dt+sigma*dw;
    x(i+1)=x(i)+dx;
end;

t=0:dt:n*dt; // time

figure(1);
a=gca();
a.background=color("white");
a=gcf();
a.background=color("white");
plot(t,x);

// // -----------------------------------------
// // EXERCISE:
// // WRITE A FUNCTION which returns a vector of simulated values of the OU process
// function [x]=ou(dt,n,x0,kappa,theta,sigma)
//    
// endfunction
// 
// // The we can create plots in the following way:
// plot(t,ou(dt,n,x0,kappa,theta,sigma))
