clc, clear

S(1) =  1.0;
mu  = 0.05;
sigma = 0.435;
% sigma = sigma / sqrt(252);

dt = 1/252;
max_i = 500;


for i = 1:max_i-1
 
    dX(i) = normrnd(0,sqrt(dt));
    dS(i) = sigma*dX(i)*S(i) + mu*dt*S(i);
    S(i+1) = S(i) + dS(i);
    
end

N = max_i;
sig_sum = 0;
for i = 2:N
    sig_sum = sig_sum + (1/N)*( ((log(S(i)) - log(S(i-1)))^2) / dt );
end

sig_sum = sig_sum - (1/N)*( ((log(S(end)) - log(S(1)))^2) / (dt*N) );

sig = sqrt(sig_sum); %correct


plot(S)