QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
midpointcdsengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008, 2009 Jose Aparicio
5 Copyright (C) 2008 Roland Lichters
6 Copyright (C) 2008, 2009 StatPro Italia srl
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/midpointcdsengine.hpp>
25#include <ql/termstructures/yieldtermstructure.hpp>
26#include <ql/optional.hpp>
27#include <utility>
28
29namespace QuantLib {
30
32 Real recoveryRate,
33 Handle<YieldTermStructure> discountCurve,
34 const ext::optional<bool>& includeSettlementDateFlows)
35 : probability_(std::move(probability)), recoveryRate_(recoveryRate),
36 discountCurve_(std::move(discountCurve)),
37 includeSettlementDateFlows_(includeSettlementDateFlows) {
40 }
41
43 QL_REQUIRE(!discountCurve_.empty(),
44 "no discount term structure set");
45 QL_REQUIRE(!probability_.empty(),
46 "no probability term structure set");
47
49 Date settlementDate = discountCurve_->referenceDate();
50
51 // Upfront amount.
52 Real upfPVO1 = 0.0;
53 results_.upfrontNPV = 0.0;
54 if (!arguments_.upfrontPayment->hasOccurred(
55 settlementDate, includeSettlementDateFlows_)) {
56 upfPVO1 = discountCurve_->discount(arguments_.upfrontPayment->date());
57 results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment->amount();
58 }
59
60 // Accrual rebate.
62 // NOLINTNEXTLINE(readability-implicit-bool-conversion)
64 !arguments_.accrualRebate->hasOccurred(settlementDate, includeSettlementDateFlows_)) {
66 discountCurve_->discount(arguments_.accrualRebate->date()) *
67 arguments_.accrualRebate->amount();
68 }
69
72 for (Size i=0; i<arguments_.leg.size(); ++i) {
73 if (arguments_.leg[i]->hasOccurred(settlementDate,
75 continue;
76
77 ext::shared_ptr<FixedRateCoupon> coupon =
78 ext::dynamic_pointer_cast<FixedRateCoupon>(arguments_.leg[i]);
79
80 // In order to avoid a few switches, we calculate the NPV
81 // of both legs as a positive quantity. We'll give them
82 // the right sign at the end.
83
84 Date paymentDate = coupon->date(),
85 startDate = coupon->accrualStartDate(),
86 endDate = coupon->accrualEndDate();
87 // this is the only point where it might not coincide
88 if (i==0)
89 startDate = arguments_.protectionStart;
90 Date effectiveStartDate =
91 (startDate <= today && today <= endDate) ? today : startDate;
92 Date defaultDate = // mid-point
93 effectiveStartDate + (endDate-effectiveStartDate)/2;
94
95 Probability S = probability_->survivalProbability(paymentDate);
96 Probability P = probability_->defaultProbability(
97 effectiveStartDate,
98 endDate);
99
100 // on one side, we add the fixed rate payments in case of
101 // survival...
103 S * coupon->amount() *
104 discountCurve_->discount(paymentDate);
105 // ...possibly including accrual in case of default.
109 P * coupon->accruedAmount(defaultDate) *
110 discountCurve_->discount(defaultDate);
111 } else {
112 // pays at the end
114 P * coupon->amount() *
115 discountCurve_->discount(paymentDate);
116 }
117 }
118
119 // on the other side, we add the payment in case of default.
120 Real claim = arguments_.claim->amount(defaultDate,
125 P * claim * discountCurve_->discount(defaultDate);
126 } else {
128 P * claim * discountCurve_->discount(paymentDate);
129 }
130 }
131
132 Real upfrontSign = 1.0;
133 switch (arguments_.side) {
135 results_.defaultLegNPV *= -1.0;
137 break;
139 results_.couponLegNPV *= -1.0;
140 results_.upfrontNPV *= -1.0;
141 upfrontSign = -1.0;
142 break;
143 default:
144 QL_FAIL("unknown protection side");
145 }
146
151
152 if (results_.couponLegNPV != 0.0) {
156 } else {
158 }
159
160 if (upfPVO1 > 0.0) {
164 / (upfPVO1 * arguments_.notional);
165 } else {
167 }
168
169 static const Rate basisPoint = 1.0e-4;
170
171 if (arguments_.spread != 0.0) {
174 } else {
176 }
177
178 // NOLINTNEXTLINE(readability-implicit-bool-conversion)
179 if (arguments_.upfront && *arguments_.upfront != 0.0) {
182 } else {
184 }
185 }
186
187}
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
MidPointCdsEngine(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 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.