//
//  EXERCISE 2: STOCHASTIC PROCESSES
//

// -------------------
// I. WIENER PROCESS
// ------------------- 

// Definitions

function [r]=randn()
// random number from N(0,1) distribution
    r=rand(1,"normal");
endfunction

function [w]=wiener(dt,n)    
// Sample path of a Wiener process
// with step 'dt', while making 'n' steps
    w(1)=0; 
    for i=1:n
        dw=sqrt(dt)*randn();
        w(i+1)=w(i)+dw;
    end;
    w=w';
endfunction

// Plot: sample paths of a Wiener process

dt=0.001; n=1000;
time=0:dt:n*dt;

figure;  

a=gca();
a.background=color("white");
a=gcf();
a.background=color("white");
a=gda();
a.labels_font_size=3;

plot(time,wiener(dt,n),"b");
plot(time,wiener(dt,n),"r");
plot(time,wiener(dt,n),"g");
plot(time,0,"k");

xlabel("time","fontsize",3);
ylabel("Wiener process","fontsize",3);

legend("sample path 1", "sample path 2", "sample path 3","expected value");

// ---------------------
// II. BROWNIAN MOTION
// ---------------------

// Basic outline

// mu=5; sigma=1;
// dt=0.001; n=1000;
// time=0:dt:n*dt;
// plot(time,mu*time+sigma*wiener(dt,n));
  
// --------------------------------
// III. GEOMETRIC BROWNIAN MOTION
// --------------------------------

// Basic outline

// x0= ; mu= ; sigma= ;
// plot(time,x0*exp(mu*time+sigma*wiener(dt,n));
