Skip to content
All library documents

Plotting GBM Density with a Lognormal Model in R

Article Quant Q&A · Author: user1690846

Summary

The document explains how to visualize the time-varying probability density of a geometric Brownian motion (GBM) as a three-dimensional surface, with stock price and time on the horizontal axes and density on the vertical axis. Its R approach evaluates a lognormal density using the GBM mean-log and standard-deviation parameters, then passes the resulting grid to a perspective plot.

The replies suggest that the original code is sound and that the apparent mismatch with a reference graph comes from parameter and plotting choices, especially volatility and grid resolution. They compare a much smaller volatility with the stated value and point out that the wider price range better displays the dispersion implied by the latter. The discussion offers no independent validation of the reference plot, and the claim that it is wrong is tentative. Readers should distinguish density shape and scale from simulated price paths.

Key ideas

  • GBM prices at a fixed time have a lognormal density whose parameters depend on time.
  • A three-dimensional density surface can be formed by evaluating that distribution over price and time grids.
  • The plotted shape depends strongly on volatility and on the chosen price range and grid resolution.
  • A narrow price range or coarse grid can make a valid density plot appear misleading.

Tags

Full text
# GBM 3d plot with R


# GBM 3d plot with R












I want to plot the density of the GBM in a 3d plot. So I have on one axis the stock price, on the other the time and on the z axis the density. At the end I want to produce this graph.

The formula I tried to implement can be found on Wikipedia.

Here is my approach:

```
mu <- 0.1
sigma <- 0.1
S0 <- 100

color <- rgb(85, 141, 85, maxColorValue=255)

x <- seq(100, 112, length=40)
y <- seq(0.25, 1.1, length=25)

f <- function(s, t) {
  dlnorm(s, meanlog=log(S0) + ((mu - 1/2 * sigma^2) * t), 
         sdlog=sigma * sqrt(t))
}

z <- outer(x, y, f)

persp(x, y, z, theta=160, phi=25, expand=0.75, col=color,
      ticktype="detailed", xlab="s", ylab="time", zlab="density"
)
```

But it looks clearly wrong. So where is my mistake?

## Answer by Bob Jansen (score 2, accepted)

https://quant.stackexchange.com/a/4685

I think there is no mistake on your part, if you set `sigma <- 0.0045` and

```
x <- seq(100, 112, length=100) // Lower values produce jagged edges
y <- seq(0.25, 1.1, length=60)
```

you'll get this:

With these parameters the density has about the same peak and the maximum of the density function also has a similar direction. Alas, a number of things are wrong with this plot: the sigma parameter has been changed and the maximum of the density function seems to decrease more slowly. However, the code produced is correct, since we can assume that plnorm is implemented correctly and the sdlog parameter is obviously correct. The mean parameter is also correct, the proof of that is left as an exercise ;)

I can imagine you're not satisfied with the above argument but the plot from Wikipedia must be wrong. The volatility of a lognormal is given by $\sqrt{(e^{\sigma^2}-1) e^{2 \mu + \sigma^2}}$. For $t=1$ this evaluates to $11.08$, this is clearly much wider than the plot on Wikipedia, maybe the author forgot to include the stock price in his calculation of $\mu$. Compare with this generated by

```
mu <- 0.1
sigma <- 0.1

S0 <- 100

color <- rgb(85, 141, 85, maxColorValue=255)

x <- seq(80, 130, length=100)
y <- seq(0.25, 1.1, length=60)

f <- function(s, t) {
  dlnorm(s, meanlog=log(S0) + ((mu - 1/2 * sigma^2) * t), 
         sdlog=sigma * sqrt(t))
}

z <- outer(x, y, f)

persp(x, y, z, theta=180, phi=25, expand=0.75, col=color,
      ticktype="detailed", xlab="s", ylab="time", zlab="density"
)
```

## Answer by Brian B (score 0)

https://quant.stackexchange.com/a/4673

It looks like the wikipedia graph was made with a much smaller value for the volatility $\sigma$. Try 0.01.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.