1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008, 2009 Jose Aparicio
Copyright (C) 2008 Roland Lichters
Copyright (C) 2008, 2009 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/pricingengines/credit/midpointcdsengine.hpp>
#include <ql/instruments/claim.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
namespace QuantLib {
MidPointCdsEngine::MidPointCdsEngine(
const Handle<DefaultProbabilityTermStructure>& probability,
Real recoveryRate,
const Handle<YieldTermStructure>& discountCurve,
boost::optional<bool> includeSettlementDateFlows)
: probability_(probability), recoveryRate_(recoveryRate),
discountCurve_(discountCurve),
includeSettlementDateFlows_(includeSettlementDateFlows) {
registerWith(probability_);
registerWith(discountCurve_);
}
void MidPointCdsEngine::calculate() const {
QL_REQUIRE(!discountCurve_.empty(),
"no discount term structure set");
QL_REQUIRE(!probability_.empty(),
"no probability term structure set");
Date today = Settings::instance().evaluationDate();
Date settlementDate = discountCurve_->referenceDate();
// Upfront Flow NPV. Either we are on-the-run (no flow)
// or we are forward start
Real upfPVO1 = 0.0;
if (!arguments_.upfrontPayment->hasOccurred(
settlementDate,
includeSettlementDateFlows_)) {
// date determining the probability survival so we have to pay
// the upfront (did not knock out)
Date effectiveUpfrontDate =
arguments_.protectionStart > probability_->referenceDate() ?
arguments_.protectionStart : probability_->referenceDate();
upfPVO1 =
probability_->survivalProbability(effectiveUpfrontDate) *
discountCurve_->discount(arguments_.upfrontPayment->date());
}
results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment->amount();
results_.couponLegNPV = 0.0;
results_.defaultLegNPV = 0.0;
for (Size i=0; i<arguments_.leg.size(); ++i) {
if (arguments_.leg[i]->hasOccurred(settlementDate,
includeSettlementDateFlows_))
continue;
boost::shared_ptr<FixedRateCoupon> coupon =
boost::dynamic_pointer_cast<FixedRateCoupon>(arguments_.leg[i]);
// In order to avoid a few switches, we calculate the NPV
// of both legs as a positive quantity. We'll give them
// the right sign at the end.
Date paymentDate = coupon->date(),
startDate = coupon->accrualStartDate(),
endDate = coupon->accrualEndDate();
// this is the only point where it might not coincide
if (i==0)
startDate = arguments_.protectionStart;
Date effectiveStartDate =
(startDate <= today && today <= endDate) ? today : startDate;
Date defaultDate = // mid-point
effectiveStartDate + (endDate-effectiveStartDate)/2;
Probability S = probability_->survivalProbability(paymentDate);
Probability P = probability_->defaultProbability(
effectiveStartDate,
endDate);
// on one side, we add the fixed rate payments in case of
// survival...
results_.couponLegNPV +=
S * coupon->amount() *
discountCurve_->discount(paymentDate);
// ...possibly including accrual in case of default.
if (arguments_.settlesAccrual) {
if (arguments_.paysAtDefaultTime) {
results_.couponLegNPV +=
P * coupon->accruedAmount(defaultDate) *
discountCurve_->discount(defaultDate);
} else {
// pays at the end
results_.couponLegNPV +=
P * coupon->amount() *
discountCurve_->discount(paymentDate);
}
}
// on the other side, we add the payment in case of default.
Real claim = arguments_.claim->amount(defaultDate,
arguments_.notional,
recoveryRate_);
if (arguments_.paysAtDefaultTime) {
results_.defaultLegNPV +=
P * claim * discountCurve_->discount(defaultDate);
} else {
results_.defaultLegNPV +=
P * claim * discountCurve_->discount(paymentDate);
}
}
Real upfrontSign = 1.0;
switch (arguments_.side) {
case Protection::Seller:
results_.defaultLegNPV *= -1.0;
break;
case Protection::Buyer:
results_.couponLegNPV *= -1.0;
results_.upfrontNPV *= -1.0;
upfrontSign = -1.0;
break;
default:
QL_FAIL("unknown protection side");
}
results_.value =
results_.defaultLegNPV+results_.couponLegNPV+results_.upfrontNPV;
results_.errorEstimate = Null<Real>();
if (results_.couponLegNPV != 0.0) {
results_.fairSpread =
-results_.defaultLegNPV*arguments_.spread/results_.couponLegNPV;
} else {
results_.fairSpread = Null<Rate>();
}
Real upfrontSensitivity = upfPVO1 * arguments_.notional;
if (upfrontSensitivity != 0.0) {
results_.fairUpfront =
-upfrontSign*(results_.defaultLegNPV + results_.couponLegNPV)
/ upfrontSensitivity;
} else {
results_.fairUpfront = Null<Rate>();
}
static const Rate basisPoint = 1.0e-4;
if (arguments_.spread != 0.0) {
results_.couponLegBPS =
results_.couponLegNPV*basisPoint/arguments_.spread;
} else {
results_.couponLegBPS = Null<Rate>();
}
if (arguments_.upfront && *arguments_.upfront != 0.0) {
results_.upfrontBPS =
results_.upfrontNPV*basisPoint/(*arguments_.upfront);
} else {
results_.upfrontBPS = Null<Rate>();
}
}
}
|