DDQN TensorFlow v2 Upgrade

In my previous blog post I showed how I upgraded my Black Scholes/Monte Carlo notebook to use TensorFlow v2. Today, I am going to show how I was EXTREMELY easily able to convert DDQN to the pre release of TensorFlow v2.

The notebook is located here: DDQN-TFv2.ipynb

Since I was mostly using Keras there were a few library changes but the code ran pretty much as is.

Black Scholes TensorFlow v2 Upgrade

In my previous blog post I showed how to use TensorFlow to price options using Black Scholes and running a Monte Carlo Simulation. Today, I am going to show how I was able to convert that code to the pre release of TensorFlow v2.

The notebook is located here: MonteCarloBlackScholes-TF2.ipynb

The largest change was moving from using Sessions to using Functions with the addition of eager execution. There were some stumbling blocks but once I went through a few changes it all made sense and should be easy for any future upgrades.

TensorFlow and Quants

During this post I am going to go over a topic I found online by Matthias Groncki about using TensorFlow for Monte Carlo simulations using the Black-Scholes model. For sure check out his post but I am going to walk through doing this work as a way to force myself into using features of TensorFlow.

First, if you didn’t know, a Monte Carlo Simulation is a way to find the probability of different outcomes. In investing they are used to help understand the impact of risk and uncertainty. I have seen these done in Excel before but until I read Matthais’ post I never thought to give them a run in TensorFlow.

Second, the Black-Scholes model is a mathematical model that is used for pricing derivative investments.

Third, a plain vanilla call that I will be using is when you buy a call with a certain strike price and a certain expiration date. Buying the call means that you promise to buy that stock at a set price on a set date. Typically, there is something called an option premium that you will pay in order to enter into the contract. For example, let say that Wells Fargo (WFC) is trading at $50 on August 30th. You think it will go up at least 10% before the end of the year. So, you would buy the December 21st expiration with a strike price of $55. For the rights to buy that you would pay $3 in options premium. There are 3 common scenarios. First, the stock stays below $55 and you lose the premium and you don’t get the shares. Second, the stock goes above $55 and you get to purchase those shares for $55. Third, you sell the options to someone else and collect the money.

Tying this back to Black-Scholes, they thought they knew of a formula to correctly price these through the time to maturity. They actually won a noble prize for their creation. For a fun little read, check out Long-Term Capital Management to see how poorly this did when the 1997 Asian and 1998 Russian financial crises hit.

Checkout my Jupyter Notebook from my GitHub page for all the code and a more in depth walk-through: MonteCarloBlackScholes.ipynb. I work through a simple python implementation of Black-Scholes and then first up TensorFlow and run a lot of simulations.