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 /*! \file riskybond.hpp 21 \brief Defaultable bonds 22 */ 23 24 #ifndef quantlib_riskybond_hpp 25 #define quantlib_riskybond_hpp 26 27 #include <ql/instrument.hpp> 28 #include <ql/experimental/credit/issuer.hpp> 29 #include <ql/default.hpp> 30 #include <ql/time/schedule.hpp> 31 #include <ql/time/daycounter.hpp> 32 #include <ql/cashflow.hpp> 33 #include <ql/indexes/iborindex.hpp> 34 #include <ql/patterns/lazyobject.hpp> 35 #include <ql/termstructures/yieldtermstructure.hpp> 36 #include <ql/experimental/credit/pool.hpp> 37 #include <ql/termstructures/defaulttermstructure.hpp> 38 #include <ql/currency.hpp> 39 40 namespace QuantLib { 41 42 /*! Base class for default risky bonds 43 \ingroup credit 44 */ 45 class RiskyBond : public Instrument { 46 public: 47 /*! The value is contingent to survival, i.e., the knockout 48 probability is considered. To compute the npv given that 49 the issuer has survived, divide the npv by 50 \f[(1-P_{def}(T_{npv}))\f] 51 */ 52 RiskyBond(const std::string& name, 53 const Currency& ccy, 54 Real recoveryRate, 55 const Handle<DefaultProbabilityTermStructure>& defaultTS, 56 const Handle<YieldTermStructure>& yieldTS, 57 Natural settlementDays = 0, 58 const Calendar& calendar = Calendar()); ~RiskyBond()59 virtual ~RiskyBond() {} 60 virtual std::vector<ext::shared_ptr<CashFlow> > cashflows() const = 0; 61 std::vector<ext::shared_ptr<CashFlow> > expectedCashflows(); 62 virtual Real notional(Date date = Date::minDate()) const = 0; 63 virtual Date effectiveDate() const = 0; 64 virtual Date maturityDate() const = 0; 65 virtual std::vector<ext::shared_ptr<CashFlow> > interestFlows() const = 0; 66 virtual std::vector<ext::shared_ptr<CashFlow> > notionalFlows() const = 0; 67 Real riskfreeNPV() const; 68 Real totalFutureFlows() const; 69 std::string name() const; 70 Currency ccy() const; 71 Handle<YieldTermStructure> yieldTS() const; 72 Handle<DefaultProbabilityTermStructure> defaultTS() const; 73 Real recoveryRate() const; 74 //! \name Instrument interface 75 //@{ 76 bool isExpired() const; 77 //@} 78 protected: 79 void setupExpired() const; 80 void performCalculations() const; 81 private: 82 std::string name_; 83 Currency ccy_; 84 Real recoveryRate_; 85 Handle<DefaultProbabilityTermStructure> defaultTS_; 86 Handle<YieldTermStructure> yieldTS_; 87 protected: 88 // engines data 89 Natural settlementDays_; 90 Calendar calendar_; 91 }; 92 name() const93 inline std::string RiskyBond::name() const { 94 return name_; 95 } 96 ccy() const97 inline Currency RiskyBond::ccy() const { 98 return ccy_; 99 } 100 yieldTS() const101 inline Handle<YieldTermStructure> RiskyBond::yieldTS() const { 102 return yieldTS_; 103 } 104 105 inline Handle<DefaultProbabilityTermStructure> defaultTS() const106 RiskyBond::defaultTS() const { 107 return defaultTS_; 108 } 109 recoveryRate() const110 inline Real RiskyBond::recoveryRate() const { 111 return recoveryRate_; 112 } 113 114 /*! Default risky fixed bond 115 \ingroup credit 116 */ 117 class RiskyFixedBond : public RiskyBond { 118 public: 119 RiskyFixedBond(const std::string& name, 120 const Currency& ccy, 121 Real recoveryRate, 122 const Handle<DefaultProbabilityTermStructure>& defaultTS, 123 const Schedule& schedule, 124 Real rate, 125 const DayCounter& dayCounter, 126 BusinessDayConvention paymentConvention, 127 const std::vector<Real>& notionals, 128 const Handle<YieldTermStructure>& yieldTS, 129 Natural settlementDays = 0); 130 std::vector<ext::shared_ptr<CashFlow> > cashflows() const; 131 Real notional(Date date = Date::minDate()) const; 132 Date effectiveDate() const; 133 Date maturityDate() const; 134 std::vector<ext::shared_ptr<CashFlow> > interestFlows() const; 135 std::vector<ext::shared_ptr<CashFlow> > notionalFlows() const; 136 private: 137 Schedule schedule_; 138 Real rate_; 139 DayCounter dayCounter_; 140 // BusinessDayConvention paymentConvention_; 141 std::vector<Real> notionals_; 142 std::vector<ext::shared_ptr<CashFlow> > leg_; 143 std::vector<ext::shared_ptr<CashFlow> > interestLeg_; 144 std::vector<ext::shared_ptr<CashFlow> > redemptionLeg_; 145 }; 146 147 148 /*! Default risky floating bonds 149 \ingroup credit 150 */ 151 class RiskyFloatingBond : public RiskyBond { 152 public: 153 RiskyFloatingBond(const std::string& name, 154 const Currency& ccy, 155 Real recoveryRate, 156 const Handle<DefaultProbabilityTermStructure>& defaultTS, 157 const Schedule& schedule, 158 const ext::shared_ptr<IborIndex>& index, 159 Integer fixingDays, 160 Real spread, 161 const std::vector<Real>& notionals, 162 const Handle<YieldTermStructure>& yieldTS, 163 Natural settlementDays = 0); 164 std::vector<ext::shared_ptr<CashFlow> > cashflows() const; 165 Real notional(Date date = Date::minDate()) const; 166 Date effectiveDate() const; 167 Date maturityDate() const; 168 std::vector<ext::shared_ptr<CashFlow> > interestFlows() const; 169 std::vector<ext::shared_ptr<CashFlow> > notionalFlows() const; 170 private: 171 Schedule schedule_; 172 ext::shared_ptr<IborIndex> index_; 173 DayCounter dayCounter_; 174 Integer fixingDays_; 175 Real spread_; 176 // BusinessDayConvention paymentConvention_; 177 std::vector<Real> notionals_; 178 std::vector<ext::shared_ptr<CashFlow> > leg_; 179 std::vector<ext::shared_ptr<CashFlow> > interestLeg_; 180 std::vector<ext::shared_ptr<CashFlow> > redemptionLeg_; 181 }; 182 183 } 184 185 186 #endif 187