# SABR Model Pricing Engine in Python QuantLib

I am looking for a SABR model pricing engine in Python QuantLib setting. I do know that it exists in C++ version, but not sure if available in Python. Any suggestion/feedback with respect to Python source code will be greatly appreciated!. Thanks!

• Are you looking for an implementation of SABR in Python? Or are you looking for QuantLib-Python bindings? Sep 4, 2020 at 14:25
• I am looking for QuantLib-Python bindings. Sep 4, 2020 at 14:31
• What exactly do you mean by Pyhton source code. An example of using the bindings to invoke the SABR functionality in QuantLib? Sep 4, 2020 at 14:32
• Yes I am looking for QuantLib-Python bindings for invoking SABR functionality. Sep 4, 2020 at 14:34
• I have gone through that link Bob. It also provides effective implementation of SABR model in OOP framework. Thanks Bob!! Sep 4, 2020 at 15:03

Here is a simple example that might be useful. Basically finding parameters for a given section. Some of the parameters might be assumed at start instead of calibrated.

import QuantLib as ql
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import minimize

strikes = [105, 106, 107, 108, 109, 110, 111, 112]
fwd = 120.44
expiryTime = 17/365
marketVols = [0.4164, 0.408, 0.3996, 0.3913, 0.3832, 0.3754, 0.3678, 0.3604]

params = [0.1] * 4
def f(params):
vols = np.array([
ql.sabrVolatility(strike, fwd, expiryTime, *params)
for strike in strikes
])
return ((vols - np.array(marketVols))**2 ).mean() **.5

cons=(
{'type': 'ineq', 'fun': lambda x:  0.99 - x[1]},
{'type': 'ineq', 'fun': lambda x: x[1]},
{'type': 'ineq', 'fun': lambda x: x[3]}
)

result = minimize(f, params, constraints=cons)
new_params = result['x']

newVols = [ql.sabrVolatility(strike, fwd, expiryTime, *new_params) for strike in strikes]
plt.plot(strikes, marketVols, marker='o', label="market")
plt.plot(strikes, newVols, marker='o', label="SABR")
plt.legend();


• It works wonders!! Thanks David!! Sep 4, 2020 at 15:01
• Very nice snippet Sep 5, 2020 at 3:22