QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
riskyassetswap.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008, 2009 Roland Lichters
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/event.hpp>
21#include <ql/experimental/credit/riskyassetswap.hpp>
22#include <ql/utilities/null_deleter.hpp>
23#include <utility>
24
25namespace QuantLib {
26
28 Real nominal,
29 Schedule fixedSchedule,
30 Schedule floatSchedule,
31 DayCounter fixedDayCounter,
32 DayCounter floatDayCounter,
33 Rate spread,
34 Rate recoveryRate,
37 Rate coupon)
38 : fixedPayer_(fixedPayer), nominal_(nominal), fixedSchedule_(std::move(fixedSchedule)),
39 floatSchedule_(std::move(floatSchedule)), fixedDayCounter_(std::move(fixedDayCounter)),
40 floatDayCounter_(std::move(floatDayCounter)), spread_(spread), recoveryRate_(recoveryRate),
41 yieldTS_(std::move(yieldTS)), defaultTS_(std::move(defaultTS)), coupon_(coupon) {
42
45 }
46
49 .hasOccurred(yieldTS_->referenceDate());
50 }
51
52
55 }
56
57
59 // order of calls is essential
63
65
68
71 + yieldTS_->discount (fixedSchedule_.dates().front())
72 - yieldTS_->discount (fixedSchedule_.dates().back())
74
75 NPV_ *= nominal_;
76
77 if (!fixedPayer_)
78 NPV_ *= -1;
79 }
80
81
83 Real annuity = 0;
84 for (Size i = 1; i < floatSchedule_.size(); i++) {
87 annuity += dcf * yieldTS_->discount (floatSchedule_[i]);
88 }
89 return annuity;
90 }
91
92
94 Real annuity = 0;
95 for (Size i = 1; i < floatSchedule_.size(); i++) {
98 annuity += dcf * yieldTS_->discount (floatSchedule_[i]);
99 }
100 return annuity;
101 }
102
103
105 return (yieldTS_->discount(fixedSchedule_.dates().front())
106 -yieldTS_->discount(fixedSchedule_.dates().back()))
108 }
109
110
113 // simple Euler integral to evaluate the recovery value
114 for (Size i = 1; i < fixedSchedule_.size(); i++) {
115 TimeUnit stepSize = Days;
116 Date d;
117 if (fixedSchedule_[i-1] >= defaultTS_->referenceDate())
118 d = fixedSchedule_[i-1];
119 else
120 d = defaultTS_->referenceDate();
121 Date d0 = d;
122 do {
123 Real disc = yieldTS_->discount (d);
124 Real dd = defaultTS_->defaultDensity (d, true);
125 Real dcf = defaultTS_->dayCounter().yearFraction (d0, d);
126
127 recoveryValue += disc * dd * dcf;
128
129 d0 = d;
130
131 d = NullCalendar().advance (d0, 1, stepSize, Unadjusted);
132 }
133 while (d < fixedSchedule_[i]);
134 }
136
137 return recoveryValue;
138 }
139
140
142 Real value = 0;
143 for (Size i = 1; i < fixedSchedule_.size(); i++) {
145 fixedSchedule_[i]);
146 value += dcf * yieldTS_->discount (fixedSchedule_[i])
147 * defaultTS_->survivalProbability (fixedSchedule_[i], true);
148 }
149 value *= coupon_;
150
151 value += yieldTS_->discount (fixedSchedule_.dates().back())
152 * defaultTS_->survivalProbability (fixedSchedule_.dates().back(),
153 true);
154
155 return value + recoveryValue_;
156 }
157
158
160 calculate();
161
162 Real value = 0;
163 for (Size i = 1; i < fixedSchedule_.size(); i++) {
165 fixedSchedule_[i]);
166 value += dcf * yieldTS_->discount (fixedSchedule_[i])
167 * defaultTS_->defaultProbability (fixedSchedule_[i], true);
168 }
169 value *= coupon_;
170
171 value += yieldTS_->discount (fixedSchedule_.dates().back())
172 * defaultTS_->defaultProbability (fixedSchedule_.dates().back(),
173 true);
174
175 Real initialDiscount = yieldTS_->discount(fixedSchedule_[0]);
176
177 return (1.0 - initialDiscount + value - recoveryValue_) / fixedAnnuity_;
178 }
179
180
182 const Period& tenor,
183 Natural settlementDays,
184 Calendar calendar,
185 const Period& fixedPeriod,
186 BusinessDayConvention fixedConvention,
187 DayCounter fixedDayCount,
188 const Period& floatPeriod,
189 BusinessDayConvention floatConvention,
190 DayCounter floatDayCount,
191 Real recoveryRate,
193 const Period& integrationStepSize)
194 : DefaultProbabilityHelper(spread), tenor_(tenor), settlementDays_(settlementDays),
195 calendar_(std::move(calendar)), fixedConvention_(fixedConvention), fixedPeriod_(fixedPeriod),
196 fixedDayCount_(std::move(fixedDayCount)), floatConvention_(floatConvention),
197 floatPeriod_(floatPeriod), floatDayCount_(std::move(floatDayCount)),
198 recoveryRate_(recoveryRate), yieldTS_(yieldTS), integrationStepSize_(integrationStepSize) {
199
201
202 registerWith(Settings::instance().evaluationDate());
203 registerWith(yieldTS);
204 }
205
207 QL_REQUIRE(!probability_.empty(),
208 "default term structure not set");
209 // we didn't register as observers - force calculation
210 asw_->recalculate();
211 return asw_->fairSpread();
212 }
213
217
218 probability_.linkTo(
219 ext::shared_ptr<DefaultProbabilityTermStructure>(ts, null_deleter()),
220 false);
221
223 }
224
226 if (evaluationDate_ != Settings::instance().evaluationDate())
228
230 }
231
234
237
238 Date maturity = earliestDate_ + tenor_;
239
241
242 Schedule fixedSchedule(earliestDate_, maturity,
246 Schedule floatSchedule(earliestDate_, maturity,
250
251 asw_ = ext::make_shared<RiskyAssetSwap>(true,
252 100.0,
253 fixedSchedule,
254 floatSchedule,
257 0.01,
259 yieldTS_,
261 }
262
263}
BusinessDayConvention fixedConvention_
RelinkableHandle< DefaultProbabilityTermStructure > probability_
RelinkableHandle< YieldTermStructure > yieldTS_
void setTermStructure(DefaultProbabilityTermStructure *) override
Real impliedQuote() const override
ext::shared_ptr< RiskyAssetSwap > asw_
BusinessDayConvention floatConvention_
AssetSwapHelper(const Handle< Quote > &spread, const Period &tenor, Natural settlementDays, Calendar calendar, const Period &fixedPeriod, BusinessDayConvention fixedConvention, DayCounter fixedDayCount, const Period &floatPeriod, BusinessDayConvention floatConvention, DayCounter floatDayCount, Real recoveryRate, const RelinkableHandle< YieldTermStructure > &yieldTS, const Period &integrationStepSize=Period())
Base helper class for bootstrapping.
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
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
Default probability term structure.
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
Definition: event.cpp:28
Shared handle to an observable.
Definition: handle.hpp:41
void calculate() const override
Definition: instrument.hpp:129
virtual void setupExpired() const
Definition: instrument.hpp:140
Calendar for reproducing theoretical calculations.
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Relinkable handle to an observable.
Definition: handle.hpp:112
void performCalculations() const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Handle< DefaultProbabilityTermStructure > defaultTS_
void setupExpired() const override
RiskyAssetSwap(bool fixedPayer, Real nominal, Schedule fixedSchedule, Schedule floatSchedule, DayCounter fixedDayCounter, DayCounter floatDayCounter, Rate spread, Rate recoveryRate_, Handle< YieldTermStructure > yieldTS, Handle< DefaultProbabilityTermStructure > defaultTS, Rate coupon=Null< Rate >())
Handle< YieldTermStructure > yieldTS_
Payment schedule.
Definition: schedule.hpp:40
const std::vector< Date > & dates() const
Definition: schedule.hpp:75
Size size() const
Definition: schedule.hpp:69
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
TimeUnit
Units used to describe time periods.
Definition: timeunit.hpp:37
BusinessDayConvention
Business Day conventions.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
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.