Skip to content

Commit

Permalink
added pricing engines for spread- and basket options (#2110)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored Nov 12, 2024
2 parents 585bb09 + 4529267 commit 266c3d3
Show file tree
Hide file tree
Showing 47 changed files with 4,284 additions and 96 deletions.
21 changes: 21 additions & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@
<ClInclude Include="ql\math\matrixutilities\factorreduction.hpp" />
<ClInclude Include="ql\math\matrixutilities\getcovariance.hpp" />
<ClInclude Include="ql\math\matrixutilities\gmres.hpp" />
<ClInclude Include="ql\math\matrixutilities\householder.hpp" />
<ClInclude Include="ql\math\matrixutilities\pseudosqrt.hpp" />
<ClInclude Include="ql\math\matrixutilities\qrdecomposition.hpp" />
<ClInclude Include="ql\math\matrixutilities\sparseilupreconditioner.hpp" />
Expand Down Expand Up @@ -1137,6 +1138,7 @@
<ClInclude Include="ql\math\solvers1d\brent.hpp" />
<ClInclude Include="ql\math\solvers1d\falseposition.hpp" />
<ClInclude Include="ql\math\solvers1d\finitedifferencenewtonsafe.hpp" />
<ClInclude Include="ql\math\solvers1d\halley.hpp" />
<ClInclude Include="ql\math\solvers1d\newton.hpp" />
<ClInclude Include="ql\math\solvers1d\newtonsafe.hpp" />
<ClInclude Include="ql\math\solvers1d\ridder.hpp" />
Expand Down Expand Up @@ -1198,6 +1200,7 @@
<ClInclude Include="ql\methods\finitedifferences\operators\fdmlinearopiterator.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmlinearoplayout.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmlocalvolfwdop.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmwienerop.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmornsteinuhlenbeckop.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmsabrop.hpp" />
<ClInclude Include="ql\methods\finitedifferences\operators\fdmsquarerootfwdop.hpp" />
Expand Down Expand Up @@ -1495,11 +1498,19 @@
<ClInclude Include="ql\pricingengines\barrier\fdhestonrebateengine.hpp" />
<ClInclude Include="ql\pricingengines\barrier\mcbarrierengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\all.hpp" />
<ClInclude Include="ql\pricingengines\basket\bjerksundstenslandspreadengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\choibasketengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\denglizhoubasketengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\fd2dblackscholesvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\fdndimblackscholesvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\kirkengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\mcamericanbasketengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\mceuropeanbasketengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\operatorsplittingspreadengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\singlefactorbsmbasketengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\spreadblackscholesvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\stulzengine.hpp" />
<ClInclude Include="ql\pricingengines\basket\vectorbsmprocessextractor.hpp" />
<ClInclude Include="ql\pricingengines\blackcalculator.hpp" />
<ClInclude Include="ql\pricingengines\blackformula.hpp" />
<ClInclude Include="ql\pricingengines\blackscholescalculator.hpp" />
Expand Down Expand Up @@ -2271,6 +2282,7 @@
<ClCompile Include="ql\math\matrixutilities\factorreduction.cpp" />
<ClCompile Include="ql\math\matrixutilities\getcovariance.cpp" />
<ClCompile Include="ql\math\matrixutilities\gmres.cpp" />
<ClCompile Include="ql\math\matrixutilities\householder.cpp" />
<ClCompile Include="ql\math\matrixutilities\pseudosqrt.cpp" />
<ClCompile Include="ql\math\matrixutilities\qrdecomposition.cpp" />
<ClCompile Include="ql\math\matrixutilities\sparseilupreconditioner.cpp" />
Expand Down Expand Up @@ -2344,6 +2356,7 @@
<ClCompile Include="ql\methods\finitedifferences\operators\fdmhullwhiteop.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmlinearoplayout.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmlocalvolfwdop.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmwienerop.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmornsteinuhlenbeckop.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmsabrop.cpp" />
<ClCompile Include="ql\methods\finitedifferences\operators\fdmsquarerootfwdop.cpp" />
Expand Down Expand Up @@ -2555,11 +2568,19 @@
<ClCompile Include="ql\pricingengines\barrier\fdhestondoublebarrierengine.cpp" />
<ClCompile Include="ql\pricingengines\barrier\fdhestonrebateengine.cpp" />
<ClCompile Include="ql\pricingengines\barrier\mcbarrierengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\bjerksundstenslandspreadengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\choibasketengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\denglizhoubasketengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\fd2dblackscholesvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\fdndimblackscholesvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\kirkengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\mcamericanbasketengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\mceuropeanbasketengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\operatorsplittingspreadengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\singlefactorbsmbasketengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\spreadblackscholesvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\stulzengine.cpp" />
<ClCompile Include="ql\pricingengines\basket\vectorbsmprocessextractor.cpp" />
<ClCompile Include="ql\pricingengines\blackcalculator.cpp" />
<ClCompile Include="ql\pricingengines\blackformula.cpp" />
<ClCompile Include="ql\pricingengines\blackscholescalculator.cpp" />
Expand Down
67 changes: 65 additions & 2 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Natvis Include="QuantLib.natvis" />
Expand Down Expand Up @@ -4413,6 +4413,39 @@
<ClInclude Include="ql\indexes\ibor\swestr.hpp">
<Filter>indexes\ibor</Filter>
</ClInclude>
<ClInclude Include="ql\math\matrixutilities\householder.hpp">
<Filter>math\matrixutilities</Filter>
</ClInclude>
<ClInclude Include="ql\math\solvers1d\halley.hpp">
<Filter>math\solvers1D</Filter>
</ClInclude>
<ClInclude Include="ql\methods\finitedifferences\operators\fdmwienerop.hpp">
<Filter>methods\finitedifferences\operators</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\bjerksundstenslandspreadengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\choibasketengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\denglizhoubasketengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\fdndimblackscholesvanillaengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\operatorsplittingspreadengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\singlefactorbsmbasketengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\spreadblackscholesvanillaengine.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\basket\vectorbsmprocessextractor.hpp">
<Filter>pricingengines\basket</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ql\methods\montecarlo\brownianbridge.cpp">
Expand Down Expand Up @@ -7161,5 +7194,35 @@
<ClCompile Include="ql\time\daycounters\yearfractiontodate.cpp">
<Filter>time\daycounters</Filter>
</ClCompile>
<ClCompile Include="ql\math\matrixutilities\householder.cpp">
<Filter>math\matrixutilities</Filter>
</ClCompile>
<ClCompile Include="ql\methods\finitedifferences\operators\fdmwienerop.cpp">
<Filter>methods\finitedifferences\operators</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\bjerksundstenslandspreadengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\choibasketengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\denglizhoubasketengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\fdndimblackscholesvanillaengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\operatorsplittingspreadengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\singlefactorbsmbasketengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\spreadblackscholesvanillaengine.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\basket\vectorbsmprocessextractor.cpp">
<Filter>pricingengines\basket</Filter>
</ClCompile>
</ItemGroup>
</Project>
</Project>
21 changes: 21 additions & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ set(QL_SOURCES
math/matrixutilities/factorreduction.cpp
math/matrixutilities/getcovariance.cpp
math/matrixutilities/gmres.cpp
math/matrixutilities/householder.cpp
math/matrixutilities/pseudosqrt.cpp
math/matrixutilities/qrdecomposition.cpp
math/matrixutilities/sparseilupreconditioner.cpp
Expand Down Expand Up @@ -461,6 +462,7 @@ set(QL_SOURCES
methods/finitedifferences/operators/fdmornsteinuhlenbeckop.cpp
methods/finitedifferences/operators/fdmsabrop.cpp
methods/finitedifferences/operators/fdmsquarerootfwdop.cpp
methods/finitedifferences/operators/fdmwienerop.cpp
methods/finitedifferences/operators/firstderivativeop.cpp
methods/finitedifferences/operators/ninepointlinearop.cpp
methods/finitedifferences/operators/nthorderderivativeop.cpp
Expand Down Expand Up @@ -672,10 +674,18 @@ set(QL_SOURCES
pricingengines/barrier/fdhestondoublebarrierengine.cpp
pricingengines/barrier/fdhestonrebateengine.cpp
pricingengines/barrier/mcbarrierengine.cpp
pricingengines/basket/bjerksundstenslandspreadengine.cpp
pricingengines/basket/choibasketengine.cpp
pricingengines/basket/vectorbsmprocessextractor.cpp
pricingengines/basket/denglizhoubasketengine.cpp
pricingengines/basket/fd2dblackscholesvanillaengine.cpp
pricingengines/basket/fdndimblackscholesvanillaengine.cpp
pricingengines/basket/kirkengine.cpp
pricingengines/basket/mcamericanbasketengine.cpp
pricingengines/basket/mceuropeanbasketengine.cpp
pricingengines/basket/operatorsplittingspreadengine.cpp
pricingengines/basket/singlefactorbsmbasketengine.cpp
pricingengines/basket/spreadblackscholesvanillaengine.cpp
pricingengines/basket/stulzengine.cpp
pricingengines/blackcalculator.cpp
pricingengines/blackformula.cpp
Expand Down Expand Up @@ -1468,6 +1478,7 @@ set(QL_HEADERS
math/matrixutilities/expm.hpp
math/matrixutilities/getcovariance.hpp
math/matrixutilities/gmres.hpp
math/matrixutilities/householder.hpp
math/matrixutilities/pseudosqrt.hpp
math/matrixutilities/qrdecomposition.hpp
math/matrixutilities/sparseilupreconditioner.hpp
Expand Down Expand Up @@ -1535,6 +1546,7 @@ set(QL_HEADERS
math/solvers1d/brent.hpp
math/solvers1d/falseposition.hpp
math/solvers1d/finitedifferencenewtonsafe.hpp
math/solvers1d/halley.hpp
math/solvers1d/newton.hpp
math/solvers1d/newtonsafe.hpp
math/solvers1d/ridder.hpp
Expand Down Expand Up @@ -1595,6 +1607,7 @@ set(QL_HEADERS
methods/finitedifferences/operators/fdmornsteinuhlenbeckop.hpp
methods/finitedifferences/operators/fdmsabrop.hpp
methods/finitedifferences/operators/fdmsquarerootfwdop.hpp
methods/finitedifferences/operators/fdmwienerop.hpp
methods/finitedifferences/operators/firstderivativeop.hpp
methods/finitedifferences/operators/modtriplebandlinearop.hpp
methods/finitedifferences/operators/ninepointlinearop.hpp
Expand Down Expand Up @@ -1865,10 +1878,18 @@ set(QL_HEADERS
pricingengines/barrier/fdhestondoublebarrierengine.hpp
pricingengines/barrier/fdhestonrebateengine.hpp
pricingengines/barrier/mcbarrierengine.hpp
pricingengines/basket/bjerksundstenslandspreadengine.hpp
pricingengines/basket/choibasketengine.hpp
pricingengines/basket/vectorbsmprocessextractor.hpp
pricingengines/basket/denglizhoubasketengine.hpp
pricingengines/basket/fd2dblackscholesvanillaengine.hpp
pricingengines/basket/fdndimblackscholesvanillaengine.hpp
pricingengines/basket/kirkengine.hpp
pricingengines/basket/mcamericanbasketengine.hpp
pricingengines/basket/mceuropeanbasketengine.hpp
pricingengines/basket/operatorsplittingspreadengine.hpp
pricingengines/basket/singlefactorbsmbasketengine.hpp
pricingengines/basket/spreadblackscholesvanillaengine.hpp
pricingengines/basket/stulzengine.hpp
pricingengines/blackcalculator.hpp
pricingengines/blackformula.hpp
Expand Down
6 changes: 4 additions & 2 deletions ql/instruments/basketoption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ namespace QuantLib {

class AverageBasketPayoff : public BasketPayoff {
public:
AverageBasketPayoff(const ext::shared_ptr<Payoff>& p, Array a)
: BasketPayoff(p), weights_(std::move(a)) {}
AverageBasketPayoff(const ext::shared_ptr<Payoff>& p, Array weights)
: BasketPayoff(p), weights_(std::move(weights)) {}
AverageBasketPayoff(const ext::shared_ptr<Payoff> &p,
Size n)
: BasketPayoff(p), weights_(n, 1.0/static_cast<Real>(n)) {}
Expand All @@ -80,6 +80,8 @@ namespace QuantLib {
a.begin(), Real(0.0));
}

const Array& weights() const { return weights_; }

private:
Array weights_;
};
Expand Down
46 changes: 46 additions & 0 deletions ql/math/integrals/gaussianquadratures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <ql/math/matrixutilities/tqreigendecomposition.hpp>
#include <ql/math/matrixutilities/symmetricschurdecomposition.hpp>

#include <map>

namespace QuantLib {

GaussianQuadrature::GaussianQuadrature(
Expand Down Expand Up @@ -59,6 +61,50 @@ namespace QuantLib {
}


MultiDimGaussianIntegration::MultiDimGaussianIntegration(
const std::vector<Size>& ns,
const std::function<ext::shared_ptr<GaussianQuadrature>(Size)>& genQuad)
: weights_(std::accumulate(ns.begin(), ns.end(), Size(1), std::multiplies<>()), 1.0),
x_(weights_.size(), Array(ns.size())) {

const Size m = ns.size();
const Size n = x_.size();

std::vector<Size> spacing(m);
spacing[0] = 1;
std::partial_sum(ns.begin(), ns.end()-1, spacing.begin()+1, std::multiplies<>());

std::map<Size, Array> n2weights, n2x;
for (auto order: ns) {
if (n2x.find(order) == n2x.end()) {
const ext::shared_ptr<GaussianQuadrature> quad = genQuad(order);
n2x[order] = quad->x();
n2weights[order] = quad->weights();
}
}

for (Size i=0; i < n; ++i) {
for (Size j=0; j < m; ++j) {
const Size order = ns[j];
const Size nx = (i / spacing[j]) % ns[j];
weights_[i] *= n2weights[order][nx];
x_[i][j] = n2x[order][nx];
}
}
}

Real MultiDimGaussianIntegration::operator()(
const std::function<Real(Array)>& f) const {
Real s = 0.0;
const Size n = x_.size();
for (Size i=0; i < n; ++i)
s += weights_[i]*f(x_[i]);

return s;
}



namespace detail {
template <class Integration>
GaussianQuadratureIntegrator<Integration>::GaussianQuadratureIntegrator(
Expand Down
16 changes: 16 additions & 0 deletions ql/math/integrals/gaussianquadratures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ namespace QuantLib {
Array x_, w_;
};

class MultiDimGaussianIntegration {
public:
MultiDimGaussianIntegration(
const std::vector<Size>& ns,
const std::function<ext::shared_ptr<GaussianQuadrature>(Size)>& genQuad);

Real operator()(const std::function<Real(Array)>& f) const;

const Array& weights() const { return weights_; }
const std::vector<Array>& x() const { return x_; }

private:
Array weights_;
std::vector<Array> x_;
};


//! generalized Gauss-Laguerre integration
/*! This class performs a 1-dimensional Gauss-Laguerre integration.
Expand Down
6 changes: 3 additions & 3 deletions ql/math/interpolations/chebyshevinterpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@


namespace QuantLib {
class LagrangeInterpolation;

/*! References:
S.A. Sarra: Chebyshev Interpolation: An Interactive Tour,
https://www.maa.org/sites/default/files/images/upload_library/4/vol6/Sarra/Chebyshev.html
Expand All @@ -47,6 +45,9 @@ namespace QuantLib {
Size n, const std::function<Real(Real)>& f,
PointsType pointsType = SecondKind);

explicit ChebyshevInterpolation(const ChebyshevInterpolation&) = delete;
ChebyshevInterpolation& operator=(const ChebyshevInterpolation&) = delete;

void updateY(const Array& y);

Array nodes() const;
Expand All @@ -55,7 +56,6 @@ namespace QuantLib {
private:
const Array x_;
Array y_;
ext::shared_ptr<LagrangeInterpolation> lagrangeInterp_;
};
}

Expand Down
2 changes: 2 additions & 0 deletions ql/math/matrixutilities/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ this_include_HEADERS = \
factorreduction.hpp \
getcovariance.hpp \
gmres.hpp \
householder.hpp \
pseudosqrt.hpp \
qrdecomposition.hpp \
sparseilupreconditioner.hpp \
Expand All @@ -28,6 +29,7 @@ cpp_files = \
factorreduction.cpp \
getcovariance.cpp \
gmres.cpp \
householder.cpp \
pseudosqrt.cpp \
qrdecomposition.cpp \
sparseilupreconditioner.cpp \
Expand Down
1 change: 1 addition & 0 deletions ql/math/matrixutilities/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ql/math/matrixutilities/factorreduction.hpp>
#include <ql/math/matrixutilities/getcovariance.hpp>
#include <ql/math/matrixutilities/gmres.hpp>
#include <ql/math/matrixutilities/householder.hpp>
#include <ql/math/matrixutilities/pseudosqrt.hpp>
#include <ql/math/matrixutilities/qrdecomposition.hpp>
#include <ql/math/matrixutilities/sparseilupreconditioner.hpp>
Expand Down
Loading

0 comments on commit 266c3d3

Please sign in to comment.