QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
integralcdsengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
5 Copyright (C) 2008, 2009 StatPro Italia srl
6 Copyright (C) 2009 Jose Aparicio
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/cashflows/fixedratecoupon.hpp>
23#include <ql/instruments/claim.hpp>
24#include <ql/pricingengines/credit/integralcdsengine.hpp>
25#include <ql/termstructures/yieldtermstructure.hpp>
26#include <ql/optional.hpp>
27#include <utility>
28
29namespace QuantLib {
30
33 Real recoveryRate,
34 Handle<YieldTermStructure> discountCurve,
35 const ext::optional<bool>& includeSettlementDateFlows)
36 : integrationStep_(step), probability_(std::move(probability)), recoveryRate_(recoveryRate),
37 discountCurve_(std::move(discountCurve)),
38 includeSettlementDateFlows_(includeSettlementDateFlows) {
41 }
42
44 QL_REQUIRE(integrationStep_ != Period(),
45 "null period set");
46 QL_REQUIRE(!discountCurve_.empty(),
47 "no discount term structure set");
48 QL_REQUIRE(!probability_.empty(),
49 "no probability term structure set");
50
52 Date settlementDate = discountCurve_->referenceDate();
53
54 // Upfront amount.
55 Real upfPVO1 = 0.0;
56 results_.upfrontNPV = 0.0;
57 if (!arguments_.upfrontPayment->hasOccurred(
58 settlementDate, includeSettlementDateFlows_)) {
59 upfPVO1 = discountCurve_->discount(arguments_.upfrontPayment->date());
60 results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment->amount();
61 }
62
63 // Accrual rebate.
65 // NOLINTNEXTLINE(readability-implicit-bool-conversion)
67 !arguments_.accrualRebate->hasOccurred(settlementDate, includeSettlementDateFlows_)) {
69 discountCurve_->discount(arguments_.accrualRebate->date()) *
70 arguments_.accrualRebate->amount();
71 }
72
75 for (Size i=0; i<arguments_.leg.size(); ++i) {
76 if (arguments_.leg[i]->hasOccurred(settlementDate,
78 continue;
79
80 ext::shared_ptr<FixedRateCoupon> coupon =
81 ext::dynamic_pointer_cast<FixedRateCoupon>(arguments_.leg[i]);
82
83 // In order to avoid a few switches, we calculate the NPV
84 // of both legs as a positive quantity. We'll give them
85 // the right sign at the end.
86
87 Date paymentDate = coupon->date(),
88 startDate = (i == 0 ? arguments_.protectionStart :
89 coupon->accrualStartDate()),
90 endDate = coupon->accrualEndDate();
91 Date effectiveStartDate =
92 (startDate <= today && today <= endDate) ? today : startDate;
93 Real couponAmount = coupon->amount();
94
95 Probability S = probability_->survivalProbability(paymentDate);
96
97 // On one side, we add the fixed rate payments in case of
98 // survival.
100 S * couponAmount * discountCurve_->discount(paymentDate);
101
102 // On the other side, we add the payment (and possibly the
103 // accrual) in case of default.
104
106 Date d0 = effectiveStartDate;
107 Date d1 = std::min(d0 + step, endDate);
108 Probability P0 = probability_->defaultProbability(d0);
109 DiscountFactor endDiscount = discountCurve_->discount(paymentDate);
110 do {
113 discountCurve_->discount(d1) :
114 endDiscount;
115
116 Probability P1 = probability_->defaultProbability(d1);
117 Probability dP = P1 - P0;
118
119 // accrual...
123 coupon->accruedAmount(d1) * B * dP;
124 else
126 couponAmount * B * dP;
127 }
128
129 // ...and claim.
130 Real claim = arguments_.claim->amount(d1,
133 results_.defaultLegNPV += claim * B * dP;
134
135 // setup for next time around the loop
136 P0 = P1;
137 d0 = d1;
138 d1 = std::min(d0 + step, endDate);
139 } while (d0 < endDate);
140 }
141
142 Real upfrontSign = 1.0;
143 switch (arguments_.side) {
145 results_.defaultLegNPV *= -1.0;
147 break;
149 results_.couponLegNPV *= -1.0;
150 results_.upfrontNPV *= -1.0;
151 upfrontSign = -1.0;
152 break;
153 default:
154 QL_FAIL("unknown protection side");
155 }
156
161
162 if (results_.couponLegNPV != 0.0) {
166 } else {
168 }
169
170 if (upfPVO1 > 0.0) {
174 / (upfPVO1 * arguments_.notional);
175 } else {
177 }
178
179 static const Rate basisPoint = 1.0e-4;
180
181 if (arguments_.spread != 0.0) {
184 } else {
186 }
187
188 // NOLINTNEXTLINE(readability-implicit-bool-conversion)
189 if (arguments_.upfront && *arguments_.upfront != 0.0) {
192 } else {
194 }
195 }
196
197}
ext::shared_ptr< SimpleCashFlow > accrualRebate
ext::shared_ptr< SimpleCashFlow > upfrontPayment
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
Handle< YieldTermStructure > discountCurve_
Handle< DefaultProbabilityTermStructure > probability_
ext::optional< bool > includeSettlementDateFlows_
void calculate() const override
IntegralCdsEngine(const Period &integrationStep, Handle< DefaultProbabilityTermStructure >, Real recoveryRate, Handle< YieldTermStructure > discountCurve, const ext::optional< bool > &includeSettlementDateFlows=ext::nullopt)
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
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
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Probability
probability
Definition: types.hpp:82
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.