File: singlefactorbsmbasketengine.cpp

package info (click to toggle)
quantlib 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,480 kB
  • sloc: cpp: 400,885; makefile: 6,547; python: 214; sh: 150; lisp: 86
file content (190 lines) | stat: -rw-r--r-- 7,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2024 Klaus Spanderen

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file singlefactorbsmbasketengine.cpp
*/

#include <ql/exercise.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/math/solvers1d/newton.hpp>
#include <ql/math/solvers1d/ridder.hpp>
#include <ql/math/solvers1d/halley.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/pricingengines/basket/vectorbsmprocessextractor.hpp>
#include <ql/pricingengines/basket/singlefactorbsmbasketengine.hpp>

namespace QuantLib {

    namespace detail {
        SumExponentialsRootSolver::SumExponentialsRootSolver(
            Array a, Array sig, Real K)
          : a_(std::move(a)), sig_(std::move(sig)), K_(K) {
            QL_REQUIRE(a_.size() == sig_.size(),
                    "Arrays must have the same size");
        }

        Real SumExponentialsRootSolver::operator()(Real x) const {
            ++fCtr_;

            Real s = 0.0;
            for (Size i=0; i < a_.size(); ++i)
                s += a_[i]*std::exp(sig_[i]*x);
            return s - K_;
        }

        Real SumExponentialsRootSolver::derivative(Real x) const {
            ++fPrimeCtr_;

            Real s = 0.0;
            for (Size i=0; i < a_.size(); ++i)
                s += a_[i]*sig_[i]*std::exp(sig_[i]*x);
            return s;
        }

        Real SumExponentialsRootSolver::secondDerivative(Real x) const {
            ++fDoublePrimeCtr_;

            Real s = 0.0;
            for (Size i=0; i < a_.size(); ++i)
                s += a_[i]*squared(sig_[i])*std::exp(sig_[i]*x);
            return s;
        }

        Size SumExponentialsRootSolver::getFCtr() const {
            return fCtr_;
        }

        Size SumExponentialsRootSolver::getDerivativeCtr() const {
            return fPrimeCtr_;
        }

        Size SumExponentialsRootSolver::getSecondDerivativeCtr() const {
            return fDoublePrimeCtr_;
        }

        Real SumExponentialsRootSolver::getRoot(Real xTol, Strategy strategy) const {
            const Array attr = a_*sig_;
            QL_REQUIRE(
                std::all_of(
                    attr.begin(), attr.end(),
                    [](Real x) -> bool { return x >= 0.0; }
                ),
                "a*sig should not be negative"
            );

            const bool logProb =
                std::all_of(
                    a_.begin(), a_.end(),
                    [](Real x) -> bool { return x > 0;}
            );

            QL_REQUIRE(K_ > 0 || !logProb,
                "non-positive strikes only allowed for spread options");

            // linear approximation
            const Real denom = std::accumulate(attr.begin(), attr.end(), Real(0.0));
            const Real xInit = (std::abs(denom) > 1000*QL_EPSILON)
                ? std::min(10.0, std::max(-10.0,
                      (K_ - std::accumulate(a_.begin(), a_.end(), Real(0.0)))/denom)
                  )
                : Real(0.0);

            switch(strategy) {
              case Brent:
                return QuantLib::Brent().solve(*this, xTol, xInit, 1.0);
              case Newton:
                return QuantLib::Newton().solve(*this, xTol, xInit, 1.0);
              case Ridder:
                return QuantLib::Ridder().solve(*this, xTol, xInit, 1.0);
              case Halley:
                return QuantLib::Halley().solve(*this, xTol, xInit, 1.0);
              default:
                QL_FAIL("unknown strategy type");
            }
        }
    }

    SingleFactorBsmBasketEngine::SingleFactorBsmBasketEngine(
        std::vector<ext::shared_ptr<GeneralizedBlackScholesProcess> > p,
        Real xTol)
      : xTol_(xTol),
        n_(p.size()), processes_(std::move(p)) {

        std::for_each(processes_.begin(), processes_.end(),
            [this](const auto& p) { registerWith(p); });
    }

    void SingleFactorBsmBasketEngine::calculate() const {
        const ext::shared_ptr<AverageBasketPayoff> avgPayoff =
            ext::dynamic_pointer_cast<AverageBasketPayoff>(arguments_.payoff);
        QL_REQUIRE(avgPayoff, "average basket payoff expected");
        const ext::shared_ptr<PlainVanillaPayoff> payoff =
             ext::dynamic_pointer_cast<PlainVanillaPayoff>(avgPayoff->basePayoff());
        QL_REQUIRE(payoff, "non-plain vanilla payoff given");
        const Real strike = payoff->strike();

        const Array weights = avgPayoff->weights();
        QL_REQUIRE(n_ == weights.size(),
             "wrong number of weights arguments in payoff");

        const ext::shared_ptr<EuropeanExercise> exercise =
            ext::dynamic_pointer_cast<EuropeanExercise>(arguments_.exercise);
        QL_REQUIRE(exercise, "not an European exercise");
        const Date maturityDate = exercise->lastDate();

        const detail::VectorBsmProcessExtractor pExtractor(processes_);
        const Array s = pExtractor.getSpot();
        const Array dq = pExtractor.getDividendYieldDf(maturityDate);
        const DiscountFactor dr0 = pExtractor.getInterestRateDf(maturityDate);

        const Array stdDev = pExtractor.getBlackStdDev(maturityDate);
        const Array v = stdDev*stdDev;

        const Array fwdBasket = weights * s * dq /dr0;

        // first check if all vols are zero -> intrinsic case
        if (std::all_of(
                stdDev.begin(), stdDev.end(),
                [](Real x) -> bool { return close_enough(x, 0.0); }
            )) {
            results_.value = dr0*payoff->operator()(
                std::accumulate(fwdBasket.begin(), fwdBasket.end(), Real(0.0)));
        }
        else {
            const Real d = -detail::SumExponentialsRootSolver(
                fwdBasket*Exp(-0.5*v), stdDev, strike)
                    .getRoot(xTol_, detail::SumExponentialsRootSolver::Brent);

            const CumulativeNormalDistribution N;
            const Real cp = (payoff->optionType() == Option::Call) ? 1.0 : -1.0;

            results_.value = cp * dr0 *
                std::inner_product(
                    fwdBasket.begin(), fwdBasket.end(), stdDev.begin(),
                    Real(-strike*N(cp*d)),
                    std::plus<>(),
                    [d, cp, &N](Real x, Real y) -> Real { return x*N(cp*(d+y)); }
                );

            results_.additionalResults["d"] = d;
        }
    }
}