2
$\begingroup$

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!

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

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();

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ It works wonders!! Thanks David!! $\endgroup$ – Desi_Quant Sep 4 at 15:01
  • $\begingroup$ Very nice snippet $\endgroup$ – StackG Sep 5 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.