Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/callablebonds/treecallablebondengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Allen Kuo
5
 Copyright (C) 2021 Ralf Konrad Eckel
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/experimental/callablebonds/discretizedcallablefixedratebond.hpp>
22
#include <ql/experimental/callablebonds/treecallablebondengine.hpp>
23
#include <ql/models/shortrate/onefactormodel.hpp>
24
#include <utility>
25
26
namespace QuantLib {
27
28
    TreeCallableFixedRateBondEngine::TreeCallableFixedRateBondEngine(
29
        const ext::shared_ptr<ShortRateModel>& model,
30
        const Size timeSteps,
31
        Handle<YieldTermStructure> termStructure)
32
0
    : LatticeShortRateModelEngine<CallableBond::arguments, CallableBond::results>(model, timeSteps),
33
0
      termStructure_(std::move(termStructure)) {
34
0
        registerWith(termStructure_);
35
0
    }
36
37
    TreeCallableFixedRateBondEngine::TreeCallableFixedRateBondEngine(
38
        const ext::shared_ptr<ShortRateModel>& model,
39
        const TimeGrid& timeGrid,
40
        Handle<YieldTermStructure> termStructure)
41
0
    : LatticeShortRateModelEngine<CallableBond::arguments, CallableBond::results>(model, timeGrid),
42
0
      termStructure_(std::move(termStructure)) {
43
0
        registerWith(termStructure_);
44
0
    }
45
46
0
    void TreeCallableFixedRateBondEngine::calculate() const {
47
0
        calculateWithSpread(arguments_.spread);
48
0
    }
49
50
0
    void TreeCallableFixedRateBondEngine::calculateWithSpread(Spread s) const {
51
0
        QL_REQUIRE(!model_.empty(), "no model specified");
52
53
0
        ext::shared_ptr<TermStructureConsistentModel> tsmodel =
54
0
            ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
55
0
        Handle<YieldTermStructure> discountCurve =
56
0
            tsmodel != nullptr ? tsmodel->termStructure() : termStructure_;
57
58
0
        DiscretizedCallableFixedRateBond callableBond(arguments_, discountCurve);
59
0
        ext::shared_ptr<Lattice> lattice;
60
61
0
        if (lattice_ != nullptr) {
62
0
            lattice = lattice_;
63
0
        } else {
64
0
            std::vector<Time> times = callableBond.mandatoryTimes();
65
0
            TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
66
0
            lattice = model_->tree(timeGrid);
67
0
        }
68
69
0
        if (s != 0.0) {
70
0
            auto* sr = dynamic_cast<OneFactorModel::ShortRateTree*>(&(*lattice));
71
0
            QL_REQUIRE(sr,
72
0
                       "Spread is not supported for trees other than OneFactorModel");
73
0
            sr->setSpread(s);
74
0
        }
75
76
0
        auto referenceDate = discountCurve->referenceDate();
77
0
        auto dayCounter = discountCurve->dayCounter();
78
0
        Time redemptionTime = dayCounter.yearFraction(referenceDate, arguments_.redemptionDate);
79
80
0
        callableBond.initialize(lattice, redemptionTime);
81
0
        callableBond.rollback(0.0);
82
83
0
        results_.value = callableBond.presentValue();
84
85
0
        DiscountFactor d = discountCurve->discount(arguments_.settlementDate);
86
0
        results_.settlementValue = results_.value / d;
87
0
    }
88
89
}
90