Geometric Brownian Motion

As we all know, the SDE(Stochastic Differential Equation) is also an important tool in finance, like the famous Black-Scholes Equation is also derived by the SDE, and its root is the equation we’ll talk about today.

The derivation process

The GBM could be like this form. $$ \mathrm{d} S_t = \mu S_t \mathrm{d}t + \sigma S_t \mathrm{d}W_t $$

In this equation, $S_t$ is the random viable in $t$ point, $\mu$ is the average increase rate in unit time, and the $\sigma$ is the volatility rate of random viable, and the $W_t$ is the basic Brownian Motion, which has $\mu$ with 1 and $\sigma$ with $t$.

We could solve this equation with the method of solving stochastic differential equations. so we could have the general answer of this SDE like this. $$ S_t = S_0 + \int_0^t \mu S_i \mathrm{d}i + \int_0^t \sigma S_i \mathrm{d}W_i $$

According to the Ito’s lemma, we could solve this equation with this form. $$ \frac{\mathrm{d}S_t}{S_t} = \mu \mathrm{d}t + \sigma \mathrm{d}W_t $$

Then we integrate the equation on both sides. $$ \ln S_t - \ln S_0 = (\mu - \frac{1}{2}\sigma^2)t = \sigma W_t $$

We could time the $e^x$ on both side then the formula would be like this. $$ S_t = S_0 \exp [(\mu - \frac{1}{2}\sigma^2)t + \sigma W_t] $$

Now we have solved this SDE and the GBM could be stimulated, and the Probability Density Function would be like this. $$ p(S) = \frac{1}{S \sigma \sqrt{2\pi t}}\exp [-\frac{[\ln(\frac{S}{S_0})-(\mu-\frac{\sigma^2}{2})t]^2}{2\sigma^2t}] $$

So what we need to do is like this.

flowchart LR a["Data"] --> b["$\mu$"] & c["$\sigma$"]--> d[GBM with Monte Carlo] --> e[Prediction]

Program

If we know this process well, we would get the program easily, first of all we need to get $\mu$ and $\sigma$.

We could get these number by the code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
InFile = 'dell.txt';
DeltaT = 1/250;
data = textread(InFile,'%f');
NumObs = size(data)
Last = data(1);
for i=2:NumObs
    Returns(i-1) = log(data(i)/Last);
    Last = data(i);
end
MeanReturn = mean(Returns);
StdDevReturn = std(Returns);
StartPrice = data(1)
Volatility = StdDevReturn/sqrt(DeltaT)
AppRate = (MeanReturn/DeltaT)+(Volatility^2)/2

After running this code we could get these two parameters and the next thing we should do is construct the GBM and use Monte Carlo to give the much path.

We could achieve this goal by this code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
clear;
StartPrice = 83.58;
DeltaT = 1/250;
AppRate = 0.3196;
Volatility = 0.1625 ;
NumPeriods = 30;
NumReps = 10000;
Samples = zeros(NumReps, NumPeriods);
MeanVal = (AppRate - (Volatility^2)/2)*DeltaT;
PartialS = Volatility * sqrt(DeltaT);
for i = 1: NumReps
    % initialize wiht the starting price
    Samples(i,1) = StartPrice;
    % Simulate forward in time
    for j = 2:NumPeriods
        y = MeanVal + PartialS * randn;
        Samples(i,j) = Samples(i,j-1)*exp(y);
    end
end
figure
hist(Samples(:,NumPeriods),30)
title(['Distribution of asset price at period ', num2str(NumPeriods)]);
mean(Samples(:,NumPeriods))
std(Samples(:,NumPeriods))
figure
for j=1:10000
    %figure(j+1);
    i = ceil(rand * NumReps);
    plot(Samples(i,:))
    hold on
    %title(['Asset price sample path for rep. ', num2str(i)]);
end

Then we could get the histogram like this.

And the stimulated path would there and we could get all path on the figure.

I have stimulated this GBM for 10000 times and actually it is up to you.

Then we could use mean method to get the prediction or any other method.

At last, I think this method is a classic way which combines the physic and finance, if the physical theory would be used in financial market analysis, it could be so usefull.