Coverage Report

Created: 2026-02-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/swaption/treeswaptionengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5
 Copyright (C) 2005, 2007 StatPro Italia srl
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/pricingengines/swaption/discretizedswaption.hpp>
22
#include <ql/pricingengines/swaption/treeswaptionengine.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    TreeSwaptionEngine::TreeSwaptionEngine(const ext::shared_ptr<ShortRateModel>& model,
28
                                           Size timeSteps,
29
                                           Handle<YieldTermStructure> termStructure)
30
0
    : LatticeShortRateModelEngine<Swaption::arguments, Swaption::results>(model, timeSteps),
31
0
      termStructure_(std::move(termStructure)) {
32
0
        registerWith(termStructure_);
33
0
    }
34
35
    TreeSwaptionEngine::TreeSwaptionEngine(const ext::shared_ptr<ShortRateModel>& model,
36
                                           const TimeGrid& timeGrid,
37
                                           Handle<YieldTermStructure> termStructure)
38
0
    : LatticeShortRateModelEngine<Swaption::arguments, Swaption::results>(model, timeGrid),
39
0
      termStructure_(std::move(termStructure)) {
40
0
        registerWith(termStructure_);
41
0
    }
42
43
    TreeSwaptionEngine::TreeSwaptionEngine(const Handle<ShortRateModel>& model,
44
                                           Size timeSteps,
45
                                           Handle<YieldTermStructure> termStructure)
46
0
    : LatticeShortRateModelEngine<Swaption::arguments, Swaption::results>(model, timeSteps),
47
0
      termStructure_(std::move(termStructure)) {
48
0
        registerWith(termStructure_);
49
0
    }
50
51
0
    void TreeSwaptionEngine::calculate() const {
52
53
0
        QL_REQUIRE(arguments_.settlementMethod != Settlement::ParYieldCurve,
54
0
                   "cash settled (ParYieldCurve) swaptions not priced with "
55
0
                   "TreeSwaptionEngine");
56
0
        QL_REQUIRE(!model_.empty(), "no model specified");
57
58
0
        Date referenceDate;
59
0
        DayCounter dayCounter;
60
61
0
        ext::shared_ptr<TermStructureConsistentModel> tsmodel =
62
0
            ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
63
0
        if (tsmodel != nullptr) {
64
0
            referenceDate = tsmodel->termStructure()->referenceDate();
65
0
            dayCounter = tsmodel->termStructure()->dayCounter();
66
0
        } else {
67
0
            referenceDate = termStructure_->referenceDate();
68
0
            dayCounter = termStructure_->dayCounter();
69
0
        }
70
71
0
        DiscretizedSwaption swaption(arguments_, referenceDate, dayCounter);
72
0
        ext::shared_ptr<Lattice> lattice;
73
74
0
        if (lattice_ != nullptr) {
75
0
            lattice = lattice_;
76
0
        } else {
77
0
            std::vector<Time> times = swaption.mandatoryTimes();
78
0
            TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
79
0
            lattice = model_->tree(timeGrid);
80
0
        }
81
82
0
        std::vector<Time> stoppingTimes(arguments_.exercise->dates().size());
83
0
        for (Size i=0; i<stoppingTimes.size(); ++i)
84
0
            stoppingTimes[i] =
85
0
                dayCounter.yearFraction(referenceDate,
86
0
                                        arguments_.exercise->date(i));
87
88
0
        swaption.initialize(lattice, stoppingTimes.back());
89
90
0
        Time nextExercise =
91
0
            *std::find_if(stoppingTimes.begin(), stoppingTimes.end(),
92
0
                          [](Time t){ return t >= 0.0; });
93
0
        swaption.rollback(nextExercise);
94
95
0
        results_.value = swaption.presentValue();
96
0
    }
97
98
}