Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/pricingengines/capfloor/treecapfloorengine.cpp
Line
Count
Source (jump to first uncovered line)
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
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
#include <ql/models/shortrate/onefactormodel.hpp>
21
#include <ql/pricingengines/capfloor/discretizedcapfloor.hpp>
22
#include <ql/pricingengines/capfloor/treecapfloorengine.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    TreeCapFloorEngine::TreeCapFloorEngine(const ext::shared_ptr<ShortRateModel>& model,
28
                                           Size timeSteps,
29
                                           Handle<YieldTermStructure> termStructure)
30
0
    : LatticeShortRateModelEngine<CapFloor::arguments, CapFloor::results>(model, timeSteps),
31
0
      termStructure_(std::move(termStructure)) {
32
0
        registerWith(termStructure_);
33
0
    }
34
35
    TreeCapFloorEngine::TreeCapFloorEngine(const ext::shared_ptr<ShortRateModel>& model,
36
                                           const TimeGrid& timeGrid,
37
                                           Handle<YieldTermStructure> termStructure)
38
0
    : LatticeShortRateModelEngine<CapFloor::arguments, CapFloor::results>(model, timeGrid),
39
0
      termStructure_(std::move(termStructure)) {
40
0
        registerWith(termStructure_);
41
0
    }
42
43
0
    void TreeCapFloorEngine::calculate() const {
44
45
0
        QL_REQUIRE(!model_.empty(), "no model specified");
46
47
0
        Date referenceDate;
48
0
        DayCounter dayCounter;
49
50
0
        ext::shared_ptr<TermStructureConsistentModel> tsmodel =
51
0
            ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
52
0
        if (tsmodel != nullptr) {
53
0
            referenceDate = tsmodel->termStructure()->referenceDate();
54
0
            dayCounter = tsmodel->termStructure()->dayCounter();
55
0
        } else {
56
0
            referenceDate = termStructure_->referenceDate();
57
0
            dayCounter = termStructure_->dayCounter();
58
0
        }
59
60
0
        DiscretizedCapFloor capfloor(arguments_, referenceDate, dayCounter);
61
0
        ext::shared_ptr<Lattice> lattice;
62
63
0
        if (lattice_ != nullptr) {
64
0
            lattice = lattice_;
65
0
        } else {
66
0
            std::vector<Time> times = capfloor.mandatoryTimes();
67
0
            TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
68
0
            lattice = model_->tree(timeGrid);
69
0
        }
70
71
0
        Time firstTime = dayCounter.yearFraction(referenceDate,
72
0
                                                 arguments_.startDates.front());
73
0
        Time lastTime = dayCounter.yearFraction(referenceDate,
74
0
                                                arguments_.endDates.back());
75
0
        capfloor.initialize(lattice, lastTime);
76
0
        capfloor.rollback(firstTime);
77
78
0
        results_.value = capfloor.presentValue();
79
0
    }
80
81
}
82
83