Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bondbasket.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
19/*! \file qle/instruments/bondbasket.hpp
20 \brief Basket of defaultable bonds
21*/
22
23#pragma once
24
25#include <ql/experimental/credit/pool.hpp>
27#include <ql/instruments/bond.hpp>
28
29namespace QuantLib {
30
31/*! \relates Currency */
32bool operator<(const Currency&, const Currency&);
33bool operator>(const Currency&, const Currency&);
34
35inline bool operator<(const Currency& c1, const Currency& c2) { return c1.name() < c2.name(); }
36
37inline bool operator>(const Currency& c1, const Currency& c2) { return c1.name() > c2.name(); }
38
39} // namespace QuantLib
40
41namespace QuantExt {
42using namespace QuantLib;
43
44class Cash {
45public:
46 Cash(Real flow = 0.0, Real discountedFlow = 0.0) : flow_(flow), discountedFlow_(discountedFlow) {}
47 Real flow_;
49};
50
51// Sum the undiscounted values of two cash objects
52Real sum(const Cash& c, const Cash& d);
53
54// Sum the discounted values of two cash objects
55Real sumDiscounted(const Cash& c, const Cash& d);
56
57//! Bond Basket
58/*!
59 This class holds a basket of defaultable bonds along with the pool of
60 relevant names. There may be more bonds than names involved, e.g.
61 several different bonds with same issuer.
62
63 The class provides tools for evaluating basket cash flows of different
64 kinds (interest, principal) for scenarios of default times stored for
65 all names involved in the Pool structure.
66
67 For further information refer to the detailed QuantExt documentation.
68
69 \ingroup instruments
70 */
72public:
73 // BondBasket() {}
75 //! map of QuantLib bonds
76 const std::map<std::string, QuantLib::ext::shared_ptr<QuantLib::Bond>>& qlBonds,
77 //! recoveries per bonds
78 const std::map<std::string, double>& recoveries,
79 //! multipliers per bonds
80 const std::map<std::string, double>& multipliers,
81 //! discount curves per bonds
82 const std::map<std::string, QuantLib::Handle<QuantLib::YieldTermStructure>>& yieldTermStructures,
83 //! currencies per bonds
84 const std::map<std::string, Currency>& currencies,
85 //! Pool storing default time for all names involved in the basekt above
86 const QuantLib::ext::shared_ptr<QuantLib::Pool> pool,
87 //! Base currency
88 Currency baseCcy,
89 //! Forex structure to compute spot and forward FX rates
90 const std::map <std::string, QuantLib::ext::shared_ptr<QuantExt::FxIndex>>& fxIndexMap,
91 //! end of reinvestment period
92 const QuantLib::Date & reinvestmentEndDate,
93 //! scalar for reinvestment period per bonds
94 const std::map<std::string, std::vector<double>>& reinvestmentScalar,
95 //! flow types of cashflows per bonds
96 const std::map<std::string, std::vector<std::string>>& flowType);
97 //! Inspectors
98 //@{
99 /*! Vector of risky bonds */
100 const std::map<std::string, QuantLib::ext::shared_ptr<QuantLib::Bond>>& bonds() const { return qlBonds_; }
101 /*! Pool of names with associated default times */
102 const QuantLib::ext::shared_ptr<QuantLib::Pool>& pool() const;
103 /*! Forex structure */
104 const std::map <std::string, QuantLib::ext::shared_ptr<QuantExt::FxIndex>>& fxIndexMap() const { return fxIndexMap_;}
105 /*! Unique currencies involved */
106 const std::set<QuantLib::Currency> unique_currencies() const { return unique_currencies_; }
107 /*! Recovery rate for given name */
108 const double recoveryRate (const std::string& name) const;
109 /*! Multiplier for given name */
110 const double multiplier (const std::string& name) const ;
111
112 /*! FX conversion */
113 Real convert(Real amount, Currency ccy, Date date = Date());
114 //@}
115 /*!
116 Set the date grid for mapping cash flows.
117 Store for each bond cash flow date the associated date grid bucket.
118 */
119 void setGrid(std::vector<Date> dates);
120 std::map<Currency, std::vector<Cash>> scenarioCashflow(std::vector<Date> dates);
121 std::map<Currency, std::vector<Cash>> scenarioInterestflow(std::vector<Date> dates);
122 std::map<Currency, std::vector<Cash>> scenarioPrincipalflow(std::vector<Date> dates);
123 std::map<Currency, std::vector<Real>> scenarioRemainingNotional(std::vector<Date> dates);
124 std::map<Currency, std::vector<Cash>> scenarioLossflow(std::vector<Date> dates);
125 std::map<Currency, std::vector<Cash>> scenarioFeeflow(const std::vector<QuantLib::Date>& dates);
126
127 void fillFlowMaps();
128
129private:
130
131 const double getScalar(const std::string& name, const QuantLib::Date& currentDate) const;
132 const QuantLib::Handle<QuantLib::YieldTermStructure> yts(const std::string& name) const;
133 const Currency currency(const std::string& name) const;
134 const std::vector<double> reinvestmentScalar(const std::string& name) const;
135 const std::string flowType(const std::string& name, int idx) const;
136 const QuantLib::ext::shared_ptr<QuantExt::FxIndex> fxIndex(const std::string& name) const;
137
138 //members filled by input arguments
139 const std::map<std::string, QuantLib::ext::shared_ptr<QuantLib::Bond>> qlBonds_;
140 const std::map<std::string, double> recoveries_;
141 const std::map<std::string, double> multipliers_;
142 const std::map<std::string, QuantLib::Handle<QuantLib::YieldTermStructure>> yieldTermStructures_;
143 const std::map<std::string, QuantLib::Currency> currencies_;
144 const QuantLib::ext::shared_ptr<QuantLib::Pool> pool_;
145 const Currency baseCcy_;
146 const std::map <std::string, QuantLib::ext::shared_ptr<QuantExt::FxIndex>> fxIndexMap_;
148 const std::map<std::string, std::vector<double>> reinvestmentScalar_;
149 const std::map<std::string, std::vector<std::string>> flowType_;
150
151 std::set<QuantLib::Currency> unique_currencies_;
152 std::vector<Date> grid_;
153
154 std::map<std::string, std::vector<int>> cashflow2grid_;
155 std::map<std::string, std::vector<int>> interestflow2grid_;
156 std::map<std::string, std::vector<int>> notionalflow2grid_;
157 std::map<std::string, std::vector<int>> feeflow2grid_;
158
159 std::map<std::string, std::vector<ext::shared_ptr<QuantLib::CashFlow>>> cashflows_; // full leg_
160 std::map<std::string, std::vector<ext::shared_ptr<QuantLib::CashFlow>>> interestFlows_; // interestLeg_
161 std::map<std::string, std::vector<ext::shared_ptr<QuantLib::CashFlow>>> notionalFlows_; // redemptionLeg_
162 std::map<std::string, std::vector<ext::shared_ptr<QuantLib::CashFlow>>> feeFlows_; // feeLeg_
163
164};
165
166} // namespace QuantExt
const QuantLib::ext::shared_ptr< QuantLib::Pool > pool_
Definition: bondbasket.hpp:144
const QuantLib::ext::shared_ptr< QuantExt::FxIndex > fxIndex(const std::string &name) const
Definition: bondbasket.cpp:213
std::map< std::string, std::vector< int > > interestflow2grid_
Definition: bondbasket.hpp:155
const Date reinvestmentEndDate_
Definition: bondbasket.hpp:147
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > feeFlows_
Definition: bondbasket.hpp:162
const Currency baseCcy_
Definition: bondbasket.hpp:145
std::map< Currency, std::vector< Cash > > scenarioLossflow(std::vector< Date > dates)
Definition: bondbasket.cpp:420
std::vector< Date > grid_
Definition: bondbasket.hpp:152
const std::map< std::string, std::vector< double > > reinvestmentScalar_
Definition: bondbasket.hpp:148
std::map< Currency, std::vector< Cash > > scenarioCashflow(std::vector< Date > dates)
Definition: bondbasket.cpp:268
const double multiplier(const std::string &name) const
Definition: bondbasket.cpp:178
std::map< Currency, std::vector< Cash > > scenarioInterestflow(std::vector< Date > dates)
Definition: bondbasket.cpp:314
const Currency currency(const std::string &name) const
Definition: bondbasket.cpp:192
const std::map< std::string, double > recoveries_
Definition: bondbasket.hpp:140
Real convert(Real amount, Currency ccy, Date date=Date())
Definition: bondbasket.cpp:115
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > notionalFlows_
Definition: bondbasket.hpp:161
void setGrid(std::vector< Date > dates)
Definition: bondbasket.cpp:220
std::map< std::string, std::vector< int > > cashflow2grid_
Definition: bondbasket.hpp:154
const double recoveryRate(const std::string &name) const
Definition: bondbasket.cpp:171
std::map< Currency, std::vector< Real > > scenarioRemainingNotional(std::vector< Date > dates)
Definition: bondbasket.cpp:451
const std::vector< double > reinvestmentScalar(const std::string &name) const
Definition: bondbasket.cpp:199
const double getScalar(const std::string &name, const QuantLib::Date &currentDate) const
Definition: bondbasket.cpp:28
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > interestFlows_
Definition: bondbasket.hpp:160
const std::map< std::string, QuantLib::ext::shared_ptr< QuantExt::FxIndex > > fxIndexMap_
Definition: bondbasket.hpp:146
const std::map< std::string, QuantLib::ext::shared_ptr< QuantLib::Bond > > qlBonds_
Definition: bondbasket.hpp:139
const QuantLib::Handle< QuantLib::YieldTermStructure > yts(const std::string &name) const
Definition: bondbasket.cpp:185
std::set< QuantLib::Currency > unique_currencies_
Definition: bondbasket.hpp:151
std::map< Currency, std::vector< Cash > > scenarioPrincipalflow(std::vector< Date > dates)
Definition: bondbasket.cpp:345
const std::map< std::string, QuantLib::ext::shared_ptr< QuantExt::FxIndex > > & fxIndexMap() const
Definition: bondbasket.hpp:104
const std::map< std::string, QuantLib::Currency > currencies_
Definition: bondbasket.hpp:143
std::map< Currency, std::vector< Cash > > scenarioFeeflow(const std::vector< QuantLib::Date > &dates)
Definition: bondbasket.cpp:390
std::map< std::string, std::vector< int > > notionalflow2grid_
Definition: bondbasket.hpp:156
const std::map< std::string, std::vector< std::string > > flowType_
Definition: bondbasket.hpp:149
std::map< std::string, std::vector< int > > feeflow2grid_
Definition: bondbasket.hpp:157
const std::string flowType(const std::string &name, int idx) const
Definition: bondbasket.cpp:206
const QuantLib::ext::shared_ptr< QuantLib::Pool > & pool() const
Definition: bondbasket.cpp:167
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > cashflows_
Definition: bondbasket.hpp:159
const std::map< std::string, QuantLib::Handle< QuantLib::YieldTermStructure > > yieldTermStructures_
Definition: bondbasket.hpp:142
const std::set< QuantLib::Currency > unique_currencies() const
Definition: bondbasket.hpp:106
const std::map< std::string, QuantLib::ext::shared_ptr< QuantLib::Bond > > & bonds() const
Inspectors.
Definition: bondbasket.hpp:100
const std::map< std::string, double > multipliers_
Definition: bondbasket.hpp:141
Cash(Real flow=0.0, Real discountedFlow=0.0)
Definition: bondbasket.hpp:46
Real discountedFlow_
Definition: bondbasket.hpp:48
FX index class.
Real sumDiscounted(const Cash &c, const Cash &d)
Definition: bondbasket.cpp:111
Real sum(const Cash &c, const Cash &d)
Definition: bondbasket.cpp:107
bool operator>(const Currency &, const Currency &)
Definition: bondbasket.hpp:37
bool operator<(const Currency &c1, const Currency &c2)
Definition: bondbasket.hpp:35