The Cos Method, Go and Julia

As I am preparing the third edition of my book (which I plan to complete this month), I am also moving some of the algorithms I coded in the Go language to Julia and at the same time make those public. Among those is the Cos method.

January 18, 2021

As I am preparing the third edition of my book (which I plan to complete this month), I am also moving some of the algorithms I coded in the Go language to Julia and at the same time make those public.

For the Cos method of Fang and Oosterlee, I actually had already written some code in Julia, to provide an implementation that supports arbitrary precision. Recently, I was reviewing some of the book content on vanilla option pricing under the Heston stochastic volatility model, to make it more up-to-date. I noticed that my Go implementation of the Cos method was much faster than the timing I had presented in the book (coming from yet another implementation, in another language), likely because of CPU improvements, but also because the Go language handles complex numbers relatively efficiently. Well, it turns out that Julia is of the same speed.

To price 100 options under the same Heston parameters, using 200 points, I obtain the following timing

Old implementation Go Julia
3000 microsec. 558 microsec. 560 microsec.

To price a single option, discarding the setup time, the Cos method takes only 5 microseconds. I thought that was quite impressive, even if this is a bit cheating since the expensive part is the characteristic function evaluation, which happens in the setup phase for the option maturity in question.

I also found a nice rough but simple rule to imply the number of points for a given accuracy. This, and the simplicity of the implementation make the Cos method quite attractive.