QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mchullwhiteengine.cpp
Go to the documentation of this file.
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 <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
22#include <utility>
23
24namespace QuantLib {
25
26 namespace detail {
27
29 ext::shared_ptr<HullWhite> model,
30 Time forwardMeasureTime)
31 : args_(args), model_(std::move(model)), forwardMeasureTime_(forwardMeasureTime) {
33 model_->termStructure()->discount(forwardMeasureTime_);
34
35 Date referenceDate = model_->termStructure()->referenceDate();
36 DayCounter dayCounter = model_->termStructure()->dayCounter();
37
38 startTimes_.resize(args.startDates.size());
39 for (Size i=0; i<startTimes_.size(); ++i)
40 startTimes_[i] = dayCounter.yearFraction(referenceDate,
41 args.startDates[i]);
42
43 endTimes_.resize(args.endDates.size());
44 for (Size i=0; i<endTimes_.size(); ++i)
45 endTimes_[i] = dayCounter.yearFraction(referenceDate,
46 args.endDates[i]);
47
48 fixingTimes_.resize(args.fixingDates.size());
49 for (Size i=0; i<fixingTimes_.size(); ++i)
50 fixingTimes_[i] = dayCounter.yearFraction(referenceDate,
51 args.fixingDates[i]);
52 }
53
55
56 bool isCap = (args_.type == CapFloor::Cap);
57 Real npv = 0.0;
59
60 Size pastFixings = 0;
61 for (Size i = 0; i<fixingTimes_.size(); i++) {
62 Time tau = args_.accrualTimes[i];
63 Time start = startTimes_[i],
64 end = endTimes_[i],
65 fixing = fixingTimes_[i];
66 if (end <= 0.0) {
67 // the fixing is in the past...
68 pastFixings++;
69 // ...and the caplet is expired; nothing more to do.
70 } else {
71 Rate ri_1, ri_2, currentLibor;
72 if (fixing <= 0.0) {
73 // current caplet. The fixing is in the past...
74 pastFixings++;
75 // ...so it is determined.
76 currentLibor = args_.forwards[i];
77 // However, the short rate at caplet expiry is not.
78 ri_2 = path[i-pastFixings+2];
79 } else {
80 // future caplet. Everything is to be forecast.
81 // The number of past fixings is used as an offset
82 // to index into the path.
83 ri_1 = path[i-pastFixings+1];
84 ri_2 = path[i-pastFixings+2];
85
87 model_->discountBond(fixing, start, ri_1);
89 model_->discountBond(fixing, end, ri_1);
90 currentLibor = (d1/d2-1)/tau;
91 }
92
93 Real accrualFactor =
94 1.0/model_->discountBond(end, Tb, ri_2);
95
96 Rate strike = isCap?
97 args_.capRates[i] :
99 Real payoff = isCap?
100 std::max(currentLibor - strike, 0.0) :
101 std::max(strike - currentLibor, 0.0);
102
103 npv += payoff * tau * args_.gearings[i] *
104 args_.nominals[i] * accrualFactor;
105 }
106 }
107 npv *= endDiscount_;
108 return npv;
109 }
110
111 }
112
113}
114
Arguments for cap/floor calculation
Definition: capfloor.hpp:138
std::vector< Time > accrualTimes
Definition: capfloor.hpp:145
std::vector< Rate > forwards
Definition: capfloor.hpp:148
std::vector< Date > startDates
Definition: capfloor.hpp:142
std::vector< Real > gearings
Definition: capfloor.hpp:149
std::vector< Rate > floorRates
Definition: capfloor.hpp:147
std::vector< Real > nominals
Definition: capfloor.hpp:151
std::vector< Date > endDates
Definition: capfloor.hpp:144
std::vector< Rate > capRates
Definition: capfloor.hpp:146
std::vector< Date > fixingDates
Definition: capfloor.hpp:143
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
single-factor random walk
Definition: path.hpp:40
HullWhiteCapFloorPricer(const CapFloor::arguments &, ext::shared_ptr< HullWhite >, Time forwardMeasureTime)
Real operator()(const Path &path) const override
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Monte Carlo Hull-White engine for cap/floors.
Definition: any.hpp:35
STL namespace.