Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
discountingriskybondenginemultistate.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/cashflows/coupon.hpp>
22#include <ql/termstructures/yield/zerospreadedtermstructure.hpp>
23
24#include <boost/make_shared.hpp>
25
26using namespace QuantLib;
27
28namespace QuantExt {
29
31 const Handle<YieldTermStructure>& discountCurve,
32 const std::vector<Handle<DefaultProbabilityTermStructure>>& defaultCurves,
33 const std::vector<Handle<Quote>>& recoveryRates, const Size mainResultState, const Handle<Quote>& securitySpread,
34 Period timestepPeriod, boost::optional<bool> includeSettlementDateFlows)
35 : DiscountingRiskyBondEngine(discountCurve, Handle<DefaultProbabilityTermStructure>(), Handle<Quote>(),
36 securitySpread, timestepPeriod, includeSettlementDateFlows),
37 defaultCurves_(defaultCurves), recoveryRates_(recoveryRates), mainResultState_(mainResultState) {
38 QL_REQUIRE(!defaultCurves.empty(), "DiscountingRiskBondEngineMultiState: no default curves / recovery rates given");
39 for (auto const& h : defaultCurves_) {
40 registerWith(h);
41 }
42 for (auto const& r : recoveryRates_) {
43 registerWith(r);
44 }
45 QL_REQUIRE(mainResultState < defaultCurves.size(), "DiscountingRiskBondEngineMultiState: mainResultState ("
46 << mainResultState << ") out of range 0..."
47 << defaultCurves.size() - 1);
48}
49
53}
54
56
57 // calculate all states except the main states
58
59 std::vector<Real> values(defaultCurves_.size() + 1);
60 for (Size i = 0; i < defaultCurves_.size(); ++i) {
61 if (i == mainResultState_)
62 continue;
63 linkCurves(i);
65 values[i] = results_.value;
66 }
67
68 // calculate the main state last to keep the results from this calculation
69
72 values[mainResultState_] = results_.value;
73
74 // calculate the default state
75
76 values.back() = calculateDefaultValue();
77
78 // set additional result
79
80 results_.additionalResults["stateNpv"] = values;
81}
82
84 Date npvDate = discountCurve_->referenceDate();
85 for (auto& cf : arguments_.cashflows) {
86 if (cf->hasOccurred(npvDate, includeSettlementDateFlows_))
87 continue;
88 QuantLib::ext::shared_ptr<Coupon> coupon = QuantLib::ext::dynamic_pointer_cast<Coupon>(cf);
89 if (coupon) {
90 return coupon->nominal() * recoveryRates_.back()->value();
91 }
92 }
93 // FIXME handle bonds without coupons
94 QL_FAIL("could not calculated default value, no alive coupons found");
95}
96
97} // namespace QuantExt
const Instrument::results * results_
Definition: cdsoption.cpp:81
Handle< DefaultProbabilityTermStructure > defaultCurve_
const std::vector< Handle< DefaultProbabilityTermStructure > > defaultCurves_
DiscountingRiskyBondEngineMultiState(const Handle< YieldTermStructure > &discountCurve, const std::vector< Handle< DefaultProbabilityTermStructure > > &defaultCurves, const std::vector< Handle< Quote > > &recoveryRates, const Size mainResultState, const Handle< Quote > &securitySpread, Period timestepPeriod, const boost::optional< bool > includeSettlementDateFlows=boost::none)
const std::vector< Handle< DefaultProbabilityTermStructure > > & defaultCurves() const
Swap::arguments * arguments_