Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/pricingengines/capfloor/mchullwhiteengine.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) 2006 Banca Profilo S.p.A.
5
 Copyright (C) 2006 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/capfloor/mchullwhiteengine.hpp>
22
#include <utility>
23
24
namespace QuantLib::detail {
25
26
        HullWhiteCapFloorPricer::HullWhiteCapFloorPricer(const CapFloor::arguments& args,
27
                                                         ext::shared_ptr<HullWhite> model,
28
                                                         Time forwardMeasureTime)
29
0
        : args_(args), model_(std::move(model)), forwardMeasureTime_(forwardMeasureTime) {
30
0
            endDiscount_ =
31
0
                model_->termStructure()->discount(forwardMeasureTime_);
32
33
0
            Date referenceDate = model_->termStructure()->referenceDate();
34
0
            DayCounter dayCounter = model_->termStructure()->dayCounter();
35
36
0
            startTimes_.resize(args.startDates.size());
37
0
            for (Size i=0; i<startTimes_.size(); ++i)
38
0
                startTimes_[i] = dayCounter.yearFraction(referenceDate,
39
0
                                                         args.startDates[i]);
40
41
0
            endTimes_.resize(args.endDates.size());
42
0
            for (Size i=0; i<endTimes_.size(); ++i)
43
0
                endTimes_[i] = dayCounter.yearFraction(referenceDate,
44
0
                                                       args.endDates[i]);
45
46
0
            fixingTimes_.resize(args.fixingDates.size());
47
0
            for (Size i=0; i<fixingTimes_.size(); ++i)
48
0
                fixingTimes_[i] = dayCounter.yearFraction(referenceDate,
49
0
                                                          args.fixingDates[i]);
50
0
        }
51
52
0
        Real HullWhiteCapFloorPricer::operator()(const Path& path) const {
53
54
0
            bool isCap = (args_.type == CapFloor::Cap);
55
0
            Real npv = 0.0;
56
0
            Time Tb = forwardMeasureTime_;
57
58
0
            Size pastFixings = 0;
59
0
            for (Size i = 0; i<fixingTimes_.size(); i++) {
60
0
                Time tau = args_.accrualTimes[i];
61
0
                Time start = startTimes_[i],
62
0
                     end = endTimes_[i],
63
0
                     fixing = fixingTimes_[i];
64
0
                if (end <= 0.0) {
65
                    // the fixing is in the past...
66
0
                    pastFixings++;
67
                    // ...and the caplet is expired; nothing more to do.
68
0
                } else {
69
0
                    Rate ri_1, ri_2, currentLibor;
70
0
                    if (fixing <= 0.0) {
71
                        // current caplet. The fixing is in the past...
72
0
                        pastFixings++;
73
                        // ...so it is determined.
74
0
                        currentLibor = args_.forwards[i];
75
                        // However, the short rate at caplet expiry is not.
76
0
                        ri_2 = path[i-pastFixings+2];
77
0
                    } else {
78
                        // future caplet. Everything is to be forecast.
79
                        // The number of past fixings is used as an offset
80
                        // to index into the path.
81
0
                        ri_1 = path[i-pastFixings+1];
82
0
                        ri_2 = path[i-pastFixings+2];
83
84
0
                        DiscountFactor d1 =
85
0
                            model_->discountBond(fixing, start, ri_1);
86
0
                        DiscountFactor d2 =
87
0
                            model_->discountBond(fixing, end, ri_1);
88
0
                        currentLibor = (d1/d2-1)/tau;
89
0
                    }
90
91
0
                    Real accrualFactor =
92
0
                        1.0/model_->discountBond(end, Tb, ri_2);
93
94
0
                    Rate strike = isCap?
95
0
                        args_.capRates[i] :
96
0
                        args_.floorRates[i];
97
0
                    Real payoff = isCap?
98
0
                        std::max(currentLibor - strike, 0.0) :
99
0
                    std::max(strike - currentLibor, 0.0);
100
101
0
                    npv += payoff * tau * args_.gearings[i] *
102
0
                           args_.nominals[i] * accrualFactor;
103
0
                }
104
0
            }
105
0
            npv *= endDiscount_;
106
0
            return  npv;
107
0
        }
108
109
    }
110