QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analyticcapfloorengine.cpp
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 <http://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/pricingengines/capfloor/analyticcapfloorengine.hpp>
21#include <ql/optional.hpp>
22#include <utility>
23
24namespace QuantLib {
25
26 AnalyticCapFloorEngine::AnalyticCapFloorEngine(const ext::shared_ptr<AffineModel>& model,
27 Handle<YieldTermStructure> termStructure)
29 termStructure_(std::move(termStructure)) {
31 }
32
33
35 QL_REQUIRE(!model_.empty(), "null model");
36
37 Date referenceDate;
38 DayCounter dayCounter;
39
40 ext::shared_ptr<TermStructureConsistentModel> tsmodel =
41 ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
42 if (tsmodel != nullptr) {
43 referenceDate = tsmodel->termStructure()->referenceDate();
44 dayCounter = tsmodel->termStructure()->dayCounter();
45 } else {
46 referenceDate = termStructure_->referenceDate();
47 dayCounter = termStructure_->dayCounter();
48 }
49
50 Real value = 0.0;
51 CapFloor::Type type = arguments_.type;
52 Size nPeriods = arguments_.endDates.size();
53
54 bool includeRefDatePayments =
56 if (referenceDate == Settings::instance().evaluationDate()) {
57 ext::optional<bool> includeTodaysPayments =
59 if (includeTodaysPayments) // NOLINT(readability-implicit-bool-conversion)
60 includeRefDatePayments = *includeTodaysPayments;
61 }
62
63 for (Size i=0; i<nPeriods; i++) {
64
65 Time fixingTime =
66 dayCounter.yearFraction(referenceDate,
67 arguments_.fixingDates[i]);
68 Time paymentTime =
69 dayCounter.yearFraction(referenceDate,
70 arguments_.endDates[i]);
71
72 bool not_expired =
73 includeRefDatePayments ? paymentTime >= 0.0 : paymentTime > 0.0;
74
75 if (not_expired) {
76 Time tenor = arguments_.accrualTimes[i];
77 Rate fixing = arguments_.forwards[i];
78 if (fixingTime <= 0.0) {
79 if (type == CapFloor::Cap || type == CapFloor::Collar) {
80 DiscountFactor discount = model_->discount(paymentTime);
81 Rate strike = arguments_.capRates[i];
82 value += discount * arguments_.nominals[i] * tenor
83 * arguments_.gearings[i]
84 * std::max(0.0, fixing - strike);
85 }
86 if (type == CapFloor::Floor || type == CapFloor::Collar) {
87 DiscountFactor discount = model_->discount(paymentTime);
88 Rate strike = arguments_.floorRates[i];
89 Real mult = (type == CapFloor::Floor) ? 1.0 : -1.0;
90 value += discount * arguments_.nominals[i] * tenor
91 * mult * arguments_.gearings[i]
92 * std::max(0.0, strike - fixing);
93 }
94 } else {
95 Time maturity =
96 dayCounter.yearFraction(referenceDate,
97 arguments_.startDates[i]);
98 if (type == CapFloor::Cap || type == CapFloor::Collar) {
99 Real temp = 1.0+arguments_.capRates[i]*tenor;
100 value += arguments_.nominals[i] *
101 arguments_.gearings[i] * temp *
102 model_->discountBondOption(Option::Put, 1.0/temp,
103 maturity, paymentTime);
104 }
105 if (type == CapFloor::Floor || type == CapFloor::Collar) {
106 Real temp = 1.0+arguments_.floorRates[i]*tenor;
107 Real mult = (type == CapFloor::Floor) ? 1.0 : -1.0;
108 value += arguments_.nominals[i] *
109 arguments_.gearings[i] * temp * mult *
110 model_->discountBondOption(Option::Call, 1.0/temp,
111 maturity, paymentTime);
112 }
113 }
114 }
115 }
116
117 results_.value = value;
118 }
119
120}
Affine model class.
Definition: model.hpp:45
Handle< YieldTermStructure > termStructure_
AnalyticCapFloorEngine(const ext::shared_ptr< AffineModel > &model, Handle< YieldTermStructure > termStructure=Handle< YieldTermStructure >())
Base class for cap-like instruments.
Definition: capfloor.hpp:55
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
Base class for some pricing engine on a particular model.
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
bool & includeReferenceDateEvents()
Definition: settings.hpp:155
ext::optional< bool > & includeTodaysCashFlows()
Definition: settings.hpp:163
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
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
Definition: any.hpp:35
STL namespace.