I was going to simulate a geometric brownian motion in matlab, when I recognized that I didnt fully understand the underlying Wiener process. Following the instuctions here I am starting from the form:
Where the $dW_t$ denotes the Wiener process. So $E(dW_t)=0$ and $Var(dW_t) = dt$, right? Could I also write $dW_t = W_{dt}$? Becasue I find the latter more intuitively. So I can simulate $dW_t$ by simulating $X \sim N(0,1)$ and then $dW_t = X\sqrt{dt}$?.
The next step is the discretization of the differential term. So I write: $S_{t+dt} = S_t + S_t \mu dt + S_t \sigma X \sqrt{dt}$
$S_{t+dt} = S_t*(1+ \mu dt + \sigma X \sqrt{dt})$
$S_{t+dt} = S_t*(1+ r_{dt})$
$S_{T} = S_0 *(1+r_1)*(1+r_2)*...*(1+r_T)$
Where $r_{dt}$ means the rate after each time-step. $T$ is the ending time. So I wrote the following matlab-code. Could some one verify this code please?
function [x,y]= brown_data(T, dt,sigma, mu, y0)
x = 0:dt:T;
y = zeros(size(x));
dws = normrnd(0,1,1,numel(x)-1);
tic
ratesPlus1 = [y0 ,1 + mu*dt + sigma*dws*sqrt(dt) ];
y = cumprod(ratesPlus1);
toc
end