/src/quantlib/ql/termstructures/globalbootstrap.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2019 SoftSolutions! S.r.l. |
5 | | Copyright (C) 2025 Peter Caspers |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <https://www.quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | #include <ql/math/optimization/levenbergmarquardt.hpp> |
22 | | #include <ql/termstructures/globalbootstrap.hpp> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | 0 | MultiCurveBootstrap::MultiCurveBootstrap(Real accuracy) { |
27 | 0 | optimizer_ = ext::make_shared<LevenbergMarquardt>(accuracy, accuracy, accuracy); |
28 | 0 | endCriteria_ = ext::make_shared<EndCriteria>(1000, 10, accuracy, accuracy, accuracy); |
29 | 0 | } |
30 | | |
31 | | MultiCurveBootstrap::MultiCurveBootstrap(ext::shared_ptr<OptimizationMethod> optimizer, |
32 | | ext::shared_ptr<EndCriteria> endCriteria) |
33 | 0 | : optimizer_(std::move(optimizer)), endCriteria_(std::move(endCriteria)) { |
34 | 0 | constexpr auto accuracy = 1E-10; |
35 | 0 | if (optimizer_ == nullptr) |
36 | 0 | optimizer_ = ext::make_shared<LevenbergMarquardt>(accuracy, accuracy, accuracy); |
37 | 0 | if (endCriteria_ == nullptr) |
38 | 0 | endCriteria_ = ext::make_shared<EndCriteria>(1000, 10, accuracy, accuracy, accuracy); |
39 | 0 | } |
40 | | |
41 | 0 | void MultiCurveBootstrap::add(const MultiCurveBootstrapContributor* c) { |
42 | 0 | contributors_.push_back(c); |
43 | 0 | c->setParentBootstrapper(shared_from_this()); |
44 | 0 | } |
45 | | |
46 | 0 | void MultiCurveBootstrap::addObserver(Observer* o) { |
47 | 0 | observers_.push_back(o); |
48 | 0 | } |
49 | | |
50 | 0 | void MultiCurveBootstrap::runMultiCurveBootstrap() { |
51 | |
|
52 | 0 | std::vector<Size> guessSizes; |
53 | 0 | std::vector<Real> globalGuess; |
54 | |
|
55 | 0 | for (auto const& c : contributors_) { |
56 | 0 | Array guess = c->setupCostFunction(); |
57 | 0 | globalGuess.insert(globalGuess.end(), guess.begin(), guess.end()); |
58 | 0 | guessSizes.push_back(guess.size()); |
59 | 0 | } |
60 | |
|
61 | 0 | auto fn = [this, &guessSizes](const Array& x) { |
62 | | // call the contributors' cost functions' set part |
63 | |
|
64 | 0 | std::size_t offset = 0; |
65 | 0 | for (std::size_t c = 0; c < contributors_.size(); ++c) { |
66 | 0 | Array tmp(guessSizes[c]); |
67 | 0 | std::copy(std::next(x.begin(), offset), std::next(x.begin(), offset + guessSizes[c]), |
68 | 0 | tmp.begin()); |
69 | 0 | offset += guessSizes[c]; |
70 | 0 | contributors_[c]->setCostFunctionArgument(tmp); |
71 | 0 | } |
72 | | |
73 | | // update observers |
74 | 0 | for(auto *o: observers_) |
75 | 0 | o->update(); |
76 | | |
77 | | // collect the contributors' result |
78 | |
|
79 | 0 | std::vector<Array> results; |
80 | 0 | results.reserve(contributors_.size()); |
81 | 0 | for (auto& contributor : contributors_) { |
82 | 0 | results.push_back(contributor->evaluateCostFunction()); |
83 | 0 | } |
84 | | |
85 | | // concatenate the contributors' values and return the concatenation as the result |
86 | |
|
87 | 0 | std::size_t resultSize = |
88 | 0 | std::accumulate(results.begin(), results.end(), (std::size_t)0, |
89 | 0 | [](std::size_t len, const Array& a) { return len + a.size(); }); |
90 | |
|
91 | 0 | Array result(resultSize); |
92 | |
|
93 | 0 | offset = 0; |
94 | 0 | for (auto const& r : results) { |
95 | 0 | std::copy(r.begin(), r.end(), std::next(result.begin(), offset)); |
96 | 0 | offset += r.size(); |
97 | 0 | } |
98 | |
|
99 | 0 | return result; |
100 | 0 | }; |
101 | |
|
102 | 0 | SimpleCostFunction<decltype(fn)> costFunction(fn); |
103 | 0 | NoConstraint noConstraint; |
104 | 0 | Problem problem(costFunction, noConstraint, Array(globalGuess.begin(), globalGuess.end())); |
105 | 0 | EndCriteria::Type endType = optimizer_->minimize(problem, *endCriteria_); |
106 | |
|
107 | 0 | QL_REQUIRE( |
108 | 0 | EndCriteria::succeeded(endType), |
109 | 0 | "global bootstrap failed to minimize to required accuracy (during multi curve bootstrap): " |
110 | 0 | << endType); |
111 | | |
112 | | // set all contributors to valid |
113 | | |
114 | 0 | for (auto const& c : contributors_) |
115 | 0 | c->setToValid(); |
116 | 0 | } |
117 | | |
118 | | } // namespace QuantLib |