## 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.

## TensorFlow 2.0 Testing

Update: 20180228 TensorFlow Probability is now working with the nightly builds.

Today, it was announced that TensorFlow 2.0 preview is available to download and test. I figured I had a few simple projects that use it that I would try and install and see if my results are any different.

Well, I found a bug(ish). I created a ticket and it was quickly solved. It turns out that Windows is dumb and has a limit on how long a file can be. In Win32, this made sense. Today, not so much. After a registry change and a reboot I was up and running. Great work from the TensorFlow team and bad work on the Windows developers. Ha.

Here is what I did:
Created a new environment called ‘tf_daily’ to handle all test: conda create -n tf_daily python=3.6
Activate the environment: activate tf_daily
Install the daily build: pip install tf-nightly-2.0-preview
Install the daily probability library: pip install tfp-nightly
Install pandas: pip install pandas
Install Seaborn: pip install seaborn
Install the plugin to allow me to use this environment: conda install nb_conda

We will see how all of my testing goes. My guess is that I will have some classes get moved around on me.