QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bacheliercapfloorengine.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) 2014, 2015 Michael von den Driesch
5 Copyright (C) 2019 Wojciech Ĺšlusarski
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
27#include <utility>
28
29namespace QuantLib {
30
33 const DayCounter& dc)
34 : discountCurve_(std::move(discountCurve)),
35 vol_(ext::shared_ptr<OptionletVolatilityStructure>(
38 }
39
41 const Handle<Quote>& v,
42 const DayCounter& dc)
43 : discountCurve_(std::move(discountCurve)),
44 vol_(ext::shared_ptr<OptionletVolatilityStructure>(
48 }
49
52 : discountCurve_(std::move(discountCurve)), vol_(std::move(volatility)) {
53 QL_REQUIRE(vol_->volatilityType() == Normal,
54 "BachelierCapFloorEngine should only be used for vol "
55 "surfaces stripped with normal model. Options were stripped "
56 "with model "
57 << vol_->volatilityType());
60 }
61
63 Real value = 0.0;
64 Real vega = 0.0;
65 Size optionlets = arguments_.startDates.size();
66 std::vector<Real> values(optionlets, 0.0);
67 std::vector<Real> deltas(optionlets, 0.0);
68 std::vector<Real> vegas(optionlets, 0.0);
69 std::vector<Real> stdDevs(optionlets, 0.0);
70 std::vector<DiscountFactor> discountFactors(optionlets, 0.0);
72 Date today = vol_->referenceDate();
73 Date settlement = discountCurve_->referenceDate();
74
75 for (Size i=0; i<optionlets; ++i) {
76 Date paymentDate = arguments_.endDates[i];
77 // handling of settlementDate, npvDate and includeSettlementFlows
78 // should be implemented.
79 // For the time being just discard expired caplets
80 if (paymentDate > settlement) {
81 DiscountFactor d = discountCurve_->discount(paymentDate);
82 discountFactors[i] = d;
83 Real accrualFactor = arguments_.nominals[i] *
86 Real discountedAccrual = d * accrualFactor;
87
88 Rate forward = arguments_.forwards[i];
89
90 Date fixingDate = arguments_.fixingDates[i];
91 Time sqrtTime = 0.0;
92 if (fixingDate > today)
93 sqrtTime = std::sqrt(vol_->timeFromReference(fixingDate));
94
95 if (type == CapFloor::Cap || type == CapFloor::Collar) {
96 Rate strike = arguments_.capRates[i];
97 if (sqrtTime>0.0) {
98 stdDevs[i] = std::sqrt(vol_->blackVariance(fixingDate,
99 strike));
101 forward, stdDevs[i], discountedAccrual) * sqrtTime;
103 strike, forward, stdDevs[i]);
104 }
105 // include caplets with past fixing date
107 strike, forward, stdDevs[i], discountedAccrual);
108 }
109 if (type == CapFloor::Floor || type == CapFloor::Collar) {
110 Rate strike = arguments_.floorRates[i];
111 Real floorletVega = 0.0;
112 Real floorletDelta = 0.0;
113 if (sqrtTime>0.0) {
114 stdDevs[i] = std::sqrt(vol_->blackVariance(fixingDate,
115 strike));
116 floorletVega = bachelierBlackFormulaStdDevDerivative(strike,
117 forward, stdDevs[i], discountedAccrual) * sqrtTime;
119 Option::Put, strike, forward,
120 stdDevs[i]);
121 }
123 strike, forward, stdDevs[i], discountedAccrual);
124 if (type == CapFloor::Floor) {
125 values[i] = floorlet;
126 vegas[i] = floorletVega;
127 deltas[i] = floorletDelta;
128 } else {
129 // a collar is long a cap and short a floor
130 values[i] -= floorlet;
131 vegas[i] -= floorletVega;
132 deltas[i] -= floorletDelta;
133 }
134 }
135 value += values[i];
136 vega += vegas[i];
137 }
138 }
139 results_.value = value;
140 results_.additionalResults["vega"] = vega;
141
142 results_.additionalResults["optionletsPrice"] = values;
143 results_.additionalResults["optionletsVega"] = vegas;
144 results_.additionalResults["optionletsDelta"] = deltas;
145 results_.additionalResults["optionletsDiscountFactor"] = discountFactors;
146 results_.additionalResults["optionletsAtmForward"] = arguments_.forwards;
147 if (type != CapFloor::Collar)
148 results_.additionalResults["optionletsStdDev"] = stdDevs;
149 }
150
151}
Bachelier-Black-formula cap/floor engine.
Black formula.
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
ext::shared_ptr< SimpleQuote > vol_
Definition: cdsoption.cpp:62
Handle< YieldTermStructure > discountCurve_
BachelierCapFloorEngine(Handle< YieldTermStructure > discountCurve, Volatility vol, const DayCounter &dc=Actual365Fixed())
Handle< OptionletVolatilityStructure > vol_
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
Constant caplet volatility, no time-strike dependence.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
std::map< std::string, ext::any > additionalResults
Definition: instrument.hpp:123
Calendar for reproducing theoretical calculations.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Optionlet (caplet/floorlet) volatility structure.
Constant caplet/floorlet volatility.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
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 Volatility
volatility
Definition: types.hpp:78
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real bachelierBlackFormulaAssetItmProbability(Option::Type optionType, Real strike, Real forward, Real stdDev)
Real bachelierBlackFormulaStdDevDerivative(Rate strike, Rate forward, Real stdDev, Real discount)
Real bachelierBlackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount)
STL namespace.
Calendar for reproducing theoretical calculations.
ext::shared_ptr< BlackVolTermStructure > v
StrippedOptionlet Adapter.
Interest-rate term structure.