Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bondyieldshiftedcurvetermstructure.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 Quaternion Risk Management Ltd
3 Copyright (C) 2023 Oleg Kulkov
4 All rights reserved.
5
6 This file is part of ORE, a free-software/open-source library
7 for transparent pricing and risk analysis - http://opensourcerisk.org
8
9 ORE is free software: you can redistribute it and/or modify it
10 under the terms of the Modified BSD License. You should have received a
11 copy of the license along with this program.
12 The license is also available online at <http://opensourcerisk.org>
13
14 This program is distributed on the basis that it will form a useful
15 contribution to risk analytics and model standardisation, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file bondyieldshiftedcurvetermstructure.hpp
21 \brief term structure provided yield curve shifted by bond spread
22*/
23
24#include <ql/termstructures/yieldtermstructure.hpp>
25
26#include <boost/accumulators/accumulators.hpp>
27#include <boost/accumulators/statistics/mean.hpp>
28#include <boost/accumulators/statistics/stats.hpp>
29
30namespace QuantExt {
31using namespace QuantLib;
32using namespace boost::accumulators;
33
34/*! The given date will be the implied reference date.
35
36 \note This term structure will be linked to the original
37 curve and the bond spread, i.e., any changes in the latter will be
38 reflected in this structure as well.
39*/
40class BondYieldShiftedCurveTermStructure : public QuantLib::YieldTermStructure {
41public:
42 BondYieldShiftedCurveTermStructure(const QuantLib::Handle<YieldTermStructure>& originalCurve,
43 const Real& bondSpread,
44 const Real& duration
45 )
46 : YieldTermStructure(originalCurve->dayCounter()), originalCurve_(originalCurve), bondSpread_(bondSpread), duration_(duration) {
47 registerWith(originalCurve);
48 }
49 BondYieldShiftedCurveTermStructure(const QuantLib::Handle<YieldTermStructure>& originalCurve,
50 const std::vector<Real>& bondYields,
51 const std::vector<Real>& bondDurations
52 )
53 : YieldTermStructure(originalCurve->dayCounter()), originalCurve_(originalCurve) {
54 registerWith(originalCurve);
55
56 QL_REQUIRE(bondYields.size() == bondDurations.size(),
57 "BondYieldShiftedCurveTermStructure: inconsistent lengths of yield and duration vectors ("
58 << bondYields.size() << " vs. " << bondDurations.size() << ")");
59
60 QL_REQUIRE(bondYields.size() > 0, "at least one bondYield for shifting of the reference curve required.");
61
62 accumulator_set<Real, stats<tag::mean>> spreadMean, durationMean;
63
64 for (Size i = 0; i < bondYields.size(); ++i) {
65 //estimate spread at duration
66 Real thisCrvRate = -std::log(originalCurve_->discount(static_cast<Real>(bondDurations[i])))/bondDurations[i];
67 Real thisSpread = static_cast<Real>(bondYields[i]) - thisCrvRate;
68
69 spreadMean(thisSpread);
70 durationMean(bondDurations[i]);
71 }
72
73 bondSpread_ = mean(spreadMean);
74 duration_ = mean(durationMean);
75 }
76
77 //! \name BondYieldShiftedCurveTermStructure interface
78 //@{
79 DayCounter dayCounter() const override;
80 Calendar calendar() const override;
81 Natural settlementDays() const override;
82 const Date& referenceDate() const override;
83 Date maxDate() const override;
84 Real bondSpread() const;
85 Real duration() const;
86
87protected:
88 DiscountFactor discountImpl(Time) const override;
89 //@}
90private:
91 Handle<YieldTermStructure> originalCurve_;
94};
95
96// inline definitions
97
98inline DayCounter BondYieldShiftedCurveTermStructure::dayCounter() const { return originalCurve_->dayCounter(); }
99
100inline Calendar BondYieldShiftedCurveTermStructure::calendar() const { return originalCurve_->calendar(); }
101
102inline Natural BondYieldShiftedCurveTermStructure::settlementDays() const { return originalCurve_->settlementDays(); }
103
104inline Date BondYieldShiftedCurveTermStructure::maxDate() const { return originalCurve_->maxDate(); }
105
107
109
111 return originalCurve_->referenceDate();
112}
113
114inline DiscountFactor BondYieldShiftedCurveTermStructure::discountImpl(Time t) const {
115
116 if ((duration_ != Null<Real>()) && (bondSpread_ != Null<Real>())) {
117
118 Real df = originalCurve_->discount(t) * std::exp(-t * bondSpread_);
119
120 return df;
121
122 } else {
123 return originalCurve_->discount(t);
124 }
125
126 }
127
128} // namespace QuantExt
BondYieldShiftedCurveTermStructure(const QuantLib::Handle< YieldTermStructure > &originalCurve, const std::vector< Real > &bondYields, const std::vector< Real > &bondDurations)
BondYieldShiftedCurveTermStructure(const QuantLib::Handle< YieldTermStructure > &originalCurve, const Real &bondSpread, const Real &duration)