Thursday, 20 August 2015

European Option Pricing with Python, Java and C++

Please make sure to read the disclaimer at the bottom of the page before continuing reading.

Plain vanilla call and put european options are one of the simplest financial derivatives existing. A european call (put) option is essentially a contract which by paying a fee gives you the right to buy (sell) a stock at a predetermined price, the strike price, at a future date. This kind of contracts was originally intended as an insurance tool for companies to fix the selling price of their goods and hedge the price risk.

Options have some interesting features, for starters, if you are the buyer of an option the payoff is potentially unlimited and the loss is limited to the option price as you can see by the payoff diagram below


of course the reverse is true for the seller (huge downside and limited upside).

An interesting question is: how to price options? What is the ‘fair’ price to be paid for an option? Two answers are possible. The first one is using the Black and Scholes formula and the second one is using the Monte Carlo approach. I am going to attempt to price a european call option using the Monte Carlo approach with Python, Java, and C++.

Assuming the stock can be simulated as I have explained in this article, we can calculate a huge number of payoffs and then take the average value as the expected payoff. The present value of the expected payoff will be the price we are looking for. Then again, we face another tough choice: returns should be assumed normally distributed or should returns distribution be estimated using some statistical method such as KDE? Better to do both right? I am going to use both in Python while in Java and C++ I am going to use the first approach only.

1) Normally distributed returns, Python.
Let’s first start assuming returns are normally distributed and use the Apple stock. The drift and the volatility are estimated using historical prices from Google for a call option which expires on the 7th of August 2015 (price is quoted on the 5th of August).

Immagine 1

Assuming normally distributed returns the Python script is as follows

So far the results seems ok although Google option price for the same option was about 14.65 but there might be some differences related to the risk free rate I assumed and other factors that I did not account for.

2) Estimated returns distribution using KDE, Python.
In this old post I explained how to use Scipy to roughly estimate the probability distribution of a random variable. By using the same approach and some more code, we obtain the following


We can clearly see that the estimated distribution is very different from the standard Normal, and you should bear in mind that the estimated distribution has some probability mass in the tails (not shown in the picture) which is not at all represented by the Normal distribution. Using the auxiliary scripts I used (you can download them here) you can check the high kurtosis and some other interesting statistics.

By using this approach we obtain a price which is closer to the one in Google Finance

A plot of the payoffs could be visually interesting, for instance note that at the bottom of the plot you can see that many payoffs that were 0.


I am pretty satisfied with the result, however now it is time to use C++ which is way faster than Python at executing low level tasks such as loops. The Java implementation can be downloaded here: it includes also put options and straddle (call + put) evaluation although the Black and Scholes formula is implemented for the Call option only. Some amendments should be done to the Java class, my original project was to make a simple library for pricing derivatives however since it is a time consuming process It may take some time before it is completed.

This is my C++ implementation:

This article is for educational purpose only. The author is not responsible for any consequence or loss due to inappropriate use. The article may well contain mistakes and errors. The numbers used might not be accurate. You should never use this article for purposes different from the educational one.

No comments:

Post a Comment