12
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ I'm starting to doubt the graph given on Wikipedia. The peakedness of the density seems to correspond to a volatility of 0.005. $\endgroup$
    – Bob Jansen
    Commented Dec 2, 2012 at 21:14
  • $\begingroup$ the density function can go to 1.5?? $\endgroup$
    – SRKX
    Commented Dec 3, 2012 at 21:36
  • $\begingroup$ @SRKX the function can certainly go to 1.5 or arbitrarily high with a narrow distribution -- the integral over the $S$ dimension simply has to be 1.0. Think of Dirac delta functions. $\endgroup$
    – Brian B
    Commented Dec 4, 2012 at 4:06
  • $\begingroup$ Or, less exotic, uniform over a very small interval. $\endgroup$
    – Bob Jansen
    Commented Dec 5, 2012 at 6:01

2 Answers 2

2
$\begingroup$

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:enter image description here

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 enter image description here 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"
)
$\endgroup$
2
  • 1
    $\begingroup$ big thanks, but your the code you posted (the long, which follows "generated by") is for what? It just calculates the density of a lognormal in a different way right? So if I inserv mu and sigma equal to 0.1 it is the same right? Thanks a lot again! $\endgroup$ Commented Dec 5, 2012 at 11:03
  • $\begingroup$ Yes, I couldn't find any problems with your implementation, and apparently it is correct. Since dlnorm is just the log normal density in R, with the right $\mu$ and $\sigma$ they are equivalent. $\endgroup$
    – Bob Jansen
    Commented Dec 5, 2012 at 11:18
0
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ thanks, but no: It does not look better. @Brian B $\endgroup$ Commented Dec 4, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.