• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 18905857194

29 Oct 2025 11:09AM UTC coverage: 74.32% (+0.4%) from 73.914%
18905857194

Pull #2344

github

web-flow
Merge d353587bf into d823f4ecb
Pull Request #2344: add multicurve bootstrap

99 of 103 new or added lines in 8 files covered. (96.12%)

216 existing lines in 13 files now uncovered.

57072 of 76792 relevant lines covered (74.32%)

8781066.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/ql/termstructures/globalbootstrap.cpp
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
 <http://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
MultiCurveBootstrap::MultiCurveBootstrap(Real accuracy) {
1✔
27
    optimizer_ = ext::make_shared<LevenbergMarquardt>(accuracy, accuracy, accuracy);
1✔
28
    endCriteria_ = ext::make_shared<EndCriteria>(1000, 10, accuracy, accuracy, accuracy);
1✔
29
}
1✔
30

NEW
31
MultiCurveBootstrap::MultiCurveBootstrap(ext::shared_ptr<OptimizationMethod> optimizer,
×
NEW
32
                                         ext::shared_ptr<EndCriteria> endCriteria)
×
NEW
33
: optimizer_(std::move(optimizer)), endCriteria_(std::move(endCriteria)) {}
×
34

35
void MultiCurveBootstrap::add(const MultiCurveBootstrapContributor* c) {
2✔
36
    contributors_.push_back(c);
2✔
37
    c->setParentBootstrapper(shared_from_this());
2✔
38
}
2✔
39

40
void MultiCurveBootstrap::runMultiCurveBootstrap() {
1✔
41

42
    std::vector<Size> guessSizes;
43
    std::vector<Real> globalGuess;
44

45
    for (auto const& c : contributors_) {
3✔
46
        Array guess = c->setupCostFunction();
2✔
47
        globalGuess.insert(globalGuess.end(), guess.begin(), guess.end());
2✔
48
        guessSizes.push_back(guess.size());
2✔
49
    }
50

51
    auto fn = [this, &guessSizes](const Array& x) {
1,016✔
52
        // call the contributors' cost functions' set part
53

54
        std::size_t offset = 0;
55
        for (std::size_t c = 0; c < contributors_.size(); ++c) {
381✔
56
            Array tmp(guessSizes[c]);
254✔
57
            std::copy(std::next(x.begin(), offset), std::next(x.begin(), offset + guessSizes[c]),
254✔
58
                      tmp.begin());
59
            offset += guessSizes[c];
254✔
60
            contributors_[c]->setCostFunctionArgument(tmp);
254✔
61
        }
62

63
        // collect the contributoes result
64

65
        std::vector<Array> results;
66
        for (std::size_t c = 0; c < contributors_.size(); ++c) {
381✔
67
            results.push_back(contributors_[c]->evaluateCostFunction());
508✔
68
        }
69

70
        // concatenate the contributors' values and return the concatenation as the result
71

72
        std::size_t resultSize =
73
            std::accumulate(results.begin(), results.end(), 0,
74
                            [](std::size_t len, const Array& a) { return len + a.size(); });
254✔
75

76
        Array result(resultSize);
127✔
77

78
        offset = 0;
79
        for (auto const& r : results) {
381✔
80
            std::copy(r.begin(), r.end(), std::next(result.begin(), offset));
254✔
81
            offset += r.size();
254✔
82
        }
83

84
        return result;
127✔
85
    };
127✔
86

87
    SimpleCostFunction<decltype(fn)> costFunction(fn);
88
    NoConstraint noConstraint;
1✔
89
    Problem problem(costFunction, noConstraint, Array(globalGuess.begin(), globalGuess.end()));
1✔
90
    EndCriteria::Type endType = optimizer_->minimize(problem, *endCriteria_);
1✔
91

92
    QL_REQUIRE(
1✔
93
        EndCriteria::succeeded(endType),
94
        "global bootstrap failed to minimize to required accuracy (during multi curve bootstrap): "
95
            << endType);
96

97
    // set all contributors to valid
98

99
    for (auto const& c : contributors_)
3✔
100
        c->setToValid();
2✔
101
}
1✔
102

103
} // namespace QuantLib
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc