|
| 1 | +/* |
| 2 | + * PyQuantLib: Python bindings for QuantLib |
| 3 | + * https://github.com/quantales/pyquantlib |
| 4 | + * |
| 5 | + * Copyright (c) 2025 Yassine Idyiahia |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + * See LICENSE for details. |
| 8 | + * |
| 9 | + * --- |
| 10 | + * QuantLib is Copyright (c) 2000-2025 The QuantLib Authors |
| 11 | + * https://www.quantlib.org/ |
| 12 | + */ |
| 13 | + |
| 14 | +#include "pyquantlib/pyquantlib.h" |
| 15 | +#include <ql/math/interpolations/sabrinterpolation.hpp> |
| 16 | +#include <ql/math/optimization/levenbergmarquardt.hpp> |
| 17 | +#include <pybind11/pybind11.h> |
| 18 | +#include <pybind11/stl.h> |
| 19 | + |
| 20 | +namespace py = pybind11; |
| 21 | +using namespace QuantLib; |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | + // Extended data holder that also stores forward on the heap. |
| 26 | + // SABRInterpolation stores const Real& forward, so forward needs |
| 27 | + // a stable address that survives the move into shared_ptr's deleter. |
| 28 | + struct SABRDataHolder { |
| 29 | + std::vector<Real> x, y; |
| 30 | + ext::shared_ptr<Real> forward; |
| 31 | + |
| 32 | + template <typename T> |
| 33 | + void operator()(T* p) const { delete p; } |
| 34 | + }; |
| 35 | + |
| 36 | +} // namespace |
| 37 | + |
| 38 | +void ql_math::sabrinterpolation(py::module_& m) { |
| 39 | + py::class_<SABRInterpolation, Interpolation, |
| 40 | + ext::shared_ptr<SABRInterpolation>>( |
| 41 | + m, "SABRInterpolation", |
| 42 | + "SABR smile interpolation between discrete volatility points.") |
| 43 | + .def(py::init([](std::vector<Real> strikes, |
| 44 | + std::vector<Real> vols, |
| 45 | + Real t, |
| 46 | + Real forward, |
| 47 | + Real alpha, Real beta, Real nu, Real rho, |
| 48 | + bool alphaIsFixed, bool betaIsFixed, |
| 49 | + bool nuIsFixed, bool rhoIsFixed, |
| 50 | + bool vegaWeighted, |
| 51 | + const ext::shared_ptr<EndCriteria>& endCriteria, |
| 52 | + const ext::shared_ptr<OptimizationMethod>& optMethod, |
| 53 | + Real errorAccept, |
| 54 | + bool useMaxError, |
| 55 | + Size maxGuesses, |
| 56 | + Real shift) { |
| 57 | + |
| 58 | + QL_REQUIRE(strikes.size() == vols.size(), |
| 59 | + "strikes and vols must have the same size"); |
| 60 | + QL_REQUIRE(strikes.size() >= 2, |
| 61 | + "at least 2 points required"); |
| 62 | + |
| 63 | + SABRDataHolder holder{ |
| 64 | + std::move(strikes), std::move(vols), |
| 65 | + ext::make_shared<Real>(forward)}; |
| 66 | + |
| 67 | + auto* ptr = new SABRInterpolation( |
| 68 | + holder.x.begin(), holder.x.end(), holder.y.begin(), |
| 69 | + t, *holder.forward, |
| 70 | + alpha, beta, nu, rho, |
| 71 | + alphaIsFixed, betaIsFixed, nuIsFixed, rhoIsFixed, |
| 72 | + vegaWeighted, endCriteria, optMethod, |
| 73 | + errorAccept, useMaxError, maxGuesses, |
| 74 | + shift); |
| 75 | + |
| 76 | + return ext::shared_ptr<SABRInterpolation>(ptr, std::move(holder)); |
| 77 | + }), |
| 78 | + py::arg("strikes"), py::arg("vols"), |
| 79 | + py::arg("expiry"), py::arg("forward"), |
| 80 | + py::arg("alpha"), py::arg("beta"), py::arg("nu"), py::arg("rho"), |
| 81 | + py::arg("alphaIsFixed"), py::arg("betaIsFixed"), |
| 82 | + py::arg("nuIsFixed"), py::arg("rhoIsFixed"), |
| 83 | + py::arg("vegaWeighted") = true, |
| 84 | + py::arg("endCriteria") = ext::shared_ptr<EndCriteria>(), |
| 85 | + py::arg("optMethod") = ext::shared_ptr<OptimizationMethod>(), |
| 86 | + py::arg("errorAccept") = 0.0020, |
| 87 | + py::arg("useMaxError") = false, |
| 88 | + py::arg("maxGuesses") = Size(50), |
| 89 | + py::arg("shift") = 0.0, |
| 90 | + "Constructs SABR interpolation from strikes and volatilities.") |
| 91 | + .def("expiry", &SABRInterpolation::expiry, "Returns the expiry.") |
| 92 | + .def("forward", &SABRInterpolation::forward, "Returns the forward.") |
| 93 | + .def("alpha", &SABRInterpolation::alpha, "Returns calibrated alpha.") |
| 94 | + .def("beta", &SABRInterpolation::beta, "Returns calibrated beta.") |
| 95 | + .def("nu", &SABRInterpolation::nu, "Returns calibrated nu.") |
| 96 | + .def("rho", &SABRInterpolation::rho, "Returns calibrated rho.") |
| 97 | + .def("rmsError", &SABRInterpolation::rmsError, "Returns RMS calibration error.") |
| 98 | + .def("maxError", &SABRInterpolation::maxError, "Returns max calibration error.") |
| 99 | + .def("endCriteria", &SABRInterpolation::endCriteria, "Returns the end criteria type.") |
| 100 | + .def("interpolationWeights", &SABRInterpolation::interpolationWeights, |
| 101 | + py::return_value_policy::reference_internal, |
| 102 | + "Returns the interpolation weights."); |
| 103 | +} |
0 commit comments