//
//  EXERCISE 2: STOCHASTIC PROCESSES
//

// -------------------------------------
// IV. STOCK PRICE DISTRIBUTION
// ------------------------------------- 

// Probability distribution of the stock price S(t)
// when S(0)=s0 is given
// and S is modelled as S(t)=*exp(mu*t+sigma*w(t))

function [pdf]=densityS(s,mu,sigma,s0,t)
    mu_distr=log(s0)+mi*t;  
    sigma2_distr=t*sigma^2;  
    pdf=exp(-(log(s)-mu_distr).^2/(2*sigma2_distr))./(s*sqrt(2*%pi*sigma2_distr)); 
endfunction

// Plot
// Left: exact density, right: histogram of simulated values 

figure;

// Left subplot
subplot(1,2,1);
s= // x-axis: suitable range of stock price 
f= // y-axis: densityS(...)
plot(s,f);

// Right subplot
subplot(1,2,2);
n01=rand(1,1000,"normal");
Ssim= // simulated values
      // use 'randn()' in a loop or in a vector form 'rand(1,1000,"normal")'
// Histogram in Scilab: 'histplot(n,data)', 'n' = number of intervals


// -------------------------------------
// V. COMPUTING PROBABILITIES
// ------------------------------------- 

// Useful function: CDF of N(0,1) distribution
// (simplification of the original Scilab function)
function [cdf]=normcdf(x)
    cdf=cdfnor("PQ",x,0,1);
endfunction
