QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
caphelper.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 Copyright (C) 2007 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
21#include <ql/cashflows/cashflowvectors.hpp>
22#include <ql/models/shortrate/calibrationhelpers/caphelper.hpp>
23#include <ql/pricingengines/capfloor/bacheliercapfloorengine.hpp>
24#include <ql/pricingengines/capfloor/blackcapfloorengine.hpp>
25#include <ql/pricingengines/capfloor/discretizedcapfloor.hpp>
26#include <ql/pricingengines/swap/discountingswapengine.hpp>
27#include <ql/quotes/simplequote.hpp>
28#include <ql/time/schedule.hpp>
29#include <utility>
30
31namespace QuantLib {
32
34 const Handle<Quote>& volatility,
35 ext::shared_ptr<IborIndex> index,
36 Frequency fixedLegFrequency,
37 DayCounter fixedLegDayCounter,
38 bool includeFirstSwaplet,
39 Handle<YieldTermStructure> termStructure,
41 const VolatilityType type,
42 const Real shift)
43 : BlackCalibrationHelper(volatility, errorType, type, shift), length_(length),
44 index_(std::move(index)), termStructure_(std::move(termStructure)),
45 fixedLegFrequency_(fixedLegFrequency), fixedLegDayCounter_(std::move(fixedLegDayCounter)),
46 includeFirstSwaplet_(includeFirstSwaplet) {
49 }
50
51 void CapHelper::addTimesTo(std::list<Time>& times) const {
52 calculate();
54 cap_->setupArguments(&args);
55 std::vector<Time> capTimes =
57 termStructure_->referenceDate(),
58 termStructure_->dayCounter()).mandatoryTimes();
59 times.insert(times.end(),
60 capTimes.begin(), capTimes.end());
61 }
62
64 calculate();
65 cap_->setPricingEngine(engine_);
66 return cap_->NPV();
67 }
68
70 calculate();
71 Handle<Quote> vol(ext::shared_ptr<Quote>(new SimpleQuote(sigma)));
72 ext::shared_ptr<PricingEngine> engine;
73 switch(volatilityType_) {
75 engine = ext::make_shared<BlackCapFloorEngine>(
77 break;
78 case Normal:
79 engine = ext::make_shared<BachelierCapFloorEngine>(
81 break;
82 default:
83 QL_FAIL("unknown volatility type: " << volatilityType_);
84 }
85 cap_->setPricingEngine(engine);
86 Real value = cap_->NPV();
87 cap_->setPricingEngine(engine_);
88 return value;
89 }
90
92
93 Period indexTenor = index_->tenor();
94 Rate fixedRate = 0.04; // dummy value
95 Date startDate, maturity;
97 startDate = termStructure_->referenceDate();
98 maturity = termStructure_->referenceDate() + length_;
99 } else {
100 startDate = termStructure_->referenceDate() + indexTenor;
101 maturity = termStructure_->referenceDate() + length_;
102 }
103 ext::shared_ptr<IborIndex> dummyIndex(new
104 IborIndex("dummy",
105 indexTenor,
106 index_->fixingDays(),
107 index_->currency(),
108 index_->fixingCalendar(),
109 index_->businessDayConvention(),
110 index_->endOfMonth(),
111 termStructure_->dayCounter(),
113
114 std::vector<Real> nominals(1,1.0);
115
116 Schedule floatSchedule(startDate, maturity,
117 index_->tenor(), index_->fixingCalendar(),
118 index_->businessDayConvention(),
119 index_->businessDayConvention(),
121 Leg floatingLeg = IborLeg(floatSchedule, index_)
122 .withNotionals(nominals)
123 .withPaymentAdjustment(index_->businessDayConvention())
124 .withFixingDays(0);
125
126 Schedule fixedSchedule(startDate, maturity, Period(fixedLegFrequency_),
127 index_->fixingCalendar(),
130 Leg fixedLeg = FixedRateLeg(fixedSchedule)
131 .withNotionals(nominals)
133 .withPaymentAdjustment(index_->businessDayConvention());
134
135 Swap swap(floatingLeg, fixedLeg);
136 swap.setPricingEngine(ext::shared_ptr<PricingEngine>(
138 Rate fairRate = fixedRate - swap.NPV()/(swap.legBPS(1)/1.0e-4);
139 cap_ = ext::make_shared<Cap>(floatingLeg,
140 std::vector<Rate>(1, fairRate));
141
143
144 }
145
146
147}
Actual/365 (Fixed) day count convention.
liquid Black76 market instrument used during calibration
void performCalculations() const override
ext::shared_ptr< PricingEngine > engine_
Arguments for cap/floor calculation
Definition: capfloor.hpp:138
void performCalculations() const override
Definition: caphelper.cpp:91
CapHelper(const Period &length, const Handle< Quote > &volatility, ext::shared_ptr< IborIndex > index, Frequency fixedLegFrequency, DayCounter fixedLegDayCounter, bool includeFirstSwaplet, Handle< YieldTermStructure > termStructure, BlackCalibrationHelper::CalibrationErrorType errorType=BlackCalibrationHelper::RelativePriceError, VolatilityType type=ShiftedLognormal, Real shift=0.0)
Definition: caphelper.cpp:33
const ext::shared_ptr< IborIndex > index_
Definition: caphelper.hpp:57
const Period length_
Definition: caphelper.hpp:56
Real blackPrice(Volatility volatility) const override
Black or Bachelier price given a volatility.
Definition: caphelper.cpp:69
Real modelValue() const override
returns the price of the instrument according to the model
Definition: caphelper.cpp:63
void addTimesTo(std::list< Time > &times) const override
Definition: caphelper.cpp:51
const Frequency fixedLegFrequency_
Definition: caphelper.hpp:59
ext::shared_ptr< Cap > cap_
Definition: caphelper.hpp:55
const Handle< YieldTermStructure > termStructure_
Definition: caphelper.hpp:58
const DayCounter fixedLegDayCounter_
Definition: caphelper.hpp:60
const bool includeFirstSwaplet_
Definition: caphelper.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
std::vector< Time > mandatoryTimes() const override
helper class building a sequence of fixed rate coupons
FixedRateLeg & withNotionals(Real)
FixedRateLeg & withPaymentAdjustment(BusinessDayConvention)
FixedRateLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
Shared handle to an observable.
Definition: handle.hpp:41
base class for Inter-Bank-Offered-Rate indexes (e.g. Libor, etc.)
Definition: iborindex.hpp:35
helper class building a sequence of capped/floored ibor-rate coupons
Definition: iborcoupon.hpp:133
IborLeg & withPaymentAdjustment(BusinessDayConvention)
Definition: iborcoupon.cpp:173
IborLeg & withNotionals(Real notional)
Definition: iborcoupon.cpp:158
IborLeg & withFixingDays(Natural fixingDays)
Definition: iborcoupon.cpp:188
virtual void calculate() const
Definition: lazyobject.hpp:253
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Payment schedule.
Definition: schedule.hpp:40
market element returning a stored value
Definition: simplequote.hpp:33
Interest rate swap.
Definition: swap.hpp:41
Frequency
Frequency of events.
Definition: frequency.hpp:37
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.