Option Greeks in Python. JAX for automatic... | by Roman Paolucci | Towards Data Science

Option greeks are essential to every options trader. But, what are the greeks precisely? The greeks are the partial-derivatives of the Black-Scholes equation with respect to each variable...
The Greeks
Delta — Δ — first partial-derivative with respect to the underlying asset
Gamma — γ — 2nd partial-derivative with respect to the underlying asset
Vega — v — partial-derivative with respect to volatility
Theta — θ — partial-derivative with respect to time until expiration
Rho — ρ — partial derivative with respect to the given interest rate
In plain English, the greeks tell us how an option’s price changes when only that parameter is varied (all others are held constant). Trading platforms often compute the greeks automatically for each contract. However, when streaming market data to Python, or with your own pricing models, you will need to compute these values on your own. Though there are closed-form solutions for Black-Scholes greeks (which dramatically speed up their computation). Nevertheless, I thought it would be a cool introduction to the Python library JAX which can be used to automatically compute the gradient for any function.
The beauty of JAX derived greeks is that it can be used for any pricing model, not just the Black-Scholes model making hedging exotics a little less painful.
The gradient vector, or simply the gradient, is the collection of partial-derivatives for any given multivariable function.
Given the following function f...
The gradient — ∇ — is the following...
This notion follows for all functions regardless of dimensionality...
For more information regarding the gradient check out The Gradient Vector.
You’ll notice the intersection of these two concepts lies in the partial-differentiation. The greeks (except gamma) are the first partial-derivatives of the Black-Scholes equation with respect to each variable. If we can derive the gradient from the Black-Scholes equation we will have a function yielding the 4 of the 5 greeks!
JAX is a library developed and maintained by Google, primarily used for machine learning, offering a simple way to automatically compute the gradient function.
Unfortunately as of the writing of this article, JAX is only supported on IOS.
Before we get started, it’s important to note that in order to use JAX we are going to need to reference any linear algebra (numpy) or statistical distributions (scipy) through JAX itself...
I have already built classes for European calls and puts in Python correctly structured for use with JAX...
Often JAX will return errors if the types of the variables are not floats as there are a variety of approximation techniques used to solve for the derivatives. You’ll see throughout the classes above there are references to .astype(‘float’) for this very reason.
Let’s implement JAX’s grad function to automatically compute the greeks...
In the code above after computing each option price I add a field with a call to JAX’s grad function. The grad function takes both a function and a tuple regarding which parameters to include in the gradient. After computing the gradient_func, I use it to assign the values of delta, vega, theta, and rho respectively. You will also notice that we need to make some adjustments as vega and rho are expressed as percents and theta is annualized.
Now let’s use this class to compute the greeks for an example option...
To implement these classes we create a JAX — numpy array of our option’s parameters in the following order...
Asset price
Volatility
Strike price
Time to expiration (annualized)
Risk-free rate (or interest rate)
Then we create an instance of the European option class and feed the JAX — numpy array using .astype(‘float’) to avoid errors computing the gradient. Lastly, we print the values of our greeks derived using the gradient function...
0.6300359 3.1931925 -1.3295598 1.3750976[Finished in 1.253s]