QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
nthtodefault.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 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/experimental/credit/nthtodefault.hpp>
21#include <ql/experimental/credit/lossdistribution.hpp>
22#include <ql/instruments/claim.hpp>
23#include <ql/cashflows/fixedratecoupon.hpp>
24#include <ql/termstructures/yieldtermstructure.hpp>
25#include <ql/event.hpp>
26#include <ql/experimental/credit/basket.hpp>
27
28namespace QuantLib {
29
30
32 const ext::shared_ptr<Basket>& basket,
33 Size n,
35 const Schedule& premiumSchedule,
36 Rate upfrontRate,
37 Rate premiumRate,
38 const DayCounter& dayCounter,
39 Real nominal,
40 bool settlePremiumAccrual
41 )
42 : basket_(basket), n_(n),
43 side_(side), nominal_(nominal),
44 premiumSchedule_(premiumSchedule), premiumRate_(premiumRate),
45 upfrontRate_(upfrontRate),
46 dayCounter_(dayCounter), settlePremiumAccrual_(settlePremiumAccrual)
47 {
48 QL_REQUIRE(n_ <= basket_->size(),
49 "NTD order provided is larger than the basket size.");
50
51 // Basket inception must lie before contract protection start.
52 QL_REQUIRE(basket->refDate() <= premiumSchedule.startDate(),
53 //using the start date of the schedule might be wrong, think of the CDS rule
54 "Basket did not exist before contract start.");
55
56 premiumLeg_ = FixedRateLeg(premiumSchedule)
58 .withCouponRates(premiumRate, dayCounter)
60
62 }
63
64
65// SOME OF THESE ARE INLINES---------------------------------
66 Size NthToDefault::basketSize() const { return basket_->size(); }
67
69 return detail::simple_event(premiumLeg_.back()->date()).hasOccurred();
70 }
71
73 calculate();
74 QL_REQUIRE(fairPremium_ != Null<Rate>(),
75 "fair premium not available");
76 return fairPremium_;
77 }
78
80 calculate();
81 QL_REQUIRE(premiumValue_ != Null<Rate>(),
82 "premium leg not available");
83 QL_REQUIRE(upfrontPremiumValue_ != Null<Rate>(),
84 "upfront value not available");
86 }
87
89 calculate();
90 QL_REQUIRE(protectionValue_ != Null<Rate>(),
91 "protection leg not available");
92 return protectionValue_;
93 }
94
96 calculate();
97 QL_REQUIRE(errorEstimate_ != Null<Rate>(),
98 "error estimate not available");
99 return errorEstimate_;
100
101 }
102
105
106 premiumValue_ = 0.0;
107 protectionValue_ = 0.0;
109 fairPremium_ = 0.0;
110 errorEstimate_ = 0.0;
111 }
112
114 auto* arguments = dynamic_cast<NthToDefault::arguments*>(args);
115 QL_REQUIRE(arguments != nullptr, "wrong argument type");
124 }
125
128
129 const auto* results = dynamic_cast<const NthToDefault::results*>(r);
130 QL_REQUIRE(results != nullptr, "wrong result type");
131
137 }
138
146 additionalResults.clear();
147 }
148
150 QL_REQUIRE(basket && !basket->names().empty(), "no basket given");
151 QL_REQUIRE(side != Protection::Side(-1), "side not set");
152 QL_REQUIRE(premiumRate != Null<Real>(), "no premium rate given");
153 QL_REQUIRE(upfrontRate != Null<Real>(), "no upfront rate given");
154 QL_REQUIRE(notional != Null<Real>(), "no notional given");
155 QL_REQUIRE(ntdOrder != Null<Size>(), "no NTD order given");
156 }
157
158}
159
day counter class
Definition: daycounter.hpp:44
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
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)
std::map< std::string, ext::any > additionalResults
Definition: instrument.hpp:123
void calculate() const override
Definition: instrument.hpp:129
virtual void fetchResults(const PricingEngine::results *) const
Definition: instrument.hpp:155
virtual void setupExpired() const
Definition: instrument.hpp:140
ext::shared_ptr< Basket > basket
void validate() const override
ext::shared_ptr< Basket > basket_
void setupArguments(PricingEngine::arguments *) const override
const ext::shared_ptr< Basket > & basket() const
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Real errorEstimate() const
Protection::Side side_
Real protectionLegNPV() const
Real premiumLegNPV() const
Rate fairPremium() const
void setupExpired() const override
void fetchResults(const PricingEngine::results *) const override
DayCounter dayCounter() const
NthToDefault(const ext::shared_ptr< Basket > &basket, Size n, Protection::Side side, const Schedule &premiumSchedule, Rate upfrontRate, Rate premiumRate, const DayCounter &dayCounter, Real nominal, bool settlePremiumAccrual)
This product is 'digital'; the basket might be tranched but this is.
Protection::Side side() const
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
Payment schedule.
Definition: schedule.hpp:40
const Date & startDate() const
Definition: schedule.hpp:180
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35