Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
modelimpliedyieldtermstructure.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 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 modelimpliedyieldtermstructure.hpp
20 \brief yield term structure implied by an IR model
21 \ingroup models
22*/
23
24#pragma once
25
27
28#include <ql/termstructures/yieldtermstructure.hpp>
29#include <ql/math/comparison.hpp>
30
31namespace QuantExt {
32using namespace QuantLib;
33
34//! IR Implied Yield Term Structure
35/*! The termstructure has the reference date of the model's
36 termstructure at construction, but you can vary this
37 as well as the state.
38 The purely time based variant is mainly there for
39 perfomance reasons, note that it does not provide the
40 full term structure interface and does not send
41 notifications on reference time updates.
42
43 \ingroup models
44 */
45
46class ModelImpliedYieldTermStructure : public YieldTermStructure {
47public:
48 ModelImpliedYieldTermStructure(const QuantLib::ext::shared_ptr<IrModel>& model, const DayCounter& dc = DayCounter(),
49 const bool purelyTimeBased = false);
50
51 Date maxDate() const override;
52 Time maxTime() const override;
53
54 const Date& referenceDate() const override;
55
56 virtual void referenceDate(const Date& d);
57 virtual void referenceTime(const Time t);
58 void state(const Array& s);
59 void move(const Date& d, const Array& s);
60 void move(const Time t, const Array& s);
61
62 virtual void update() override;
63
64protected:
65 Real discountImpl(Time t) const override;
66
67 const QuantLib::ext::shared_ptr<IrModel> model_;
68 const bool purelyTimeBased_;
71 Array state_;
72};
73
74//! Model Implied Yts Fwd Corrected
75/*! the target curve should have a reference date consistent with
76 the model's term structure
77
78 \ingroup models
79*/
81public:
82 ModelImpliedYtsFwdFwdCorrected(const QuantLib::ext::shared_ptr<IrModel>& model,
83 const Handle<YieldTermStructure> targetCurve, const DayCounter& dc = DayCounter(),
84 const bool purelyTimeBased = false);
85
86 void referenceDate(const Date& d) override;
87 void referenceTime(const Time t) override;
88
89protected:
90 Real discountImpl(Time t) const override;
91
92private:
93 const Handle<YieldTermStructure> targetCurve_;
94};
95
96//! Lgm Implied Yts Spot Corrected
97/*! the target curve should have a reference date consistent with
98 the model's term structure
99
100 \ingroup models
101*/
103public:
104 ModelImpliedYtsSpotCorrected(const QuantLib::ext::shared_ptr<IrModel>& model, const Handle<YieldTermStructure> targetCurve,
105 const DayCounter& dc, const bool purelyTimeBased);
106
107protected:
108 Real discountImpl(Time t) const override;
109
110private:
111 const Handle<YieldTermStructure> targetCurve_;
112};
113
114// inline
115
117 // we don't care - let the underlying classes throw
118 // exceptions if applicable
119 return Date::maxDate();
120}
121
123 // see maxDate
124 return QL_MAX_REAL;
125}
126
128 QL_REQUIRE(!purelyTimeBased_, "reference date not available for purely "
129 "time based term structure");
130 return referenceDate_;
131}
132
134 QL_REQUIRE(!purelyTimeBased_, "reference date not available for purely "
135 "time based term structure");
136 referenceDate_ = d;
137 update();
138}
139
141 QL_REQUIRE(!purelyTimeBased_, "reference date not available for purely "
142 "time based term structure");
143 referenceDate_ = d;
144 update();
145}
146
148 QL_REQUIRE(purelyTimeBased_, "reference time can only be set for purely "
149 "time based term structure");
150 relativeTime_ = t;
151 notifyObservers();
152}
153
155 QL_REQUIRE(purelyTimeBased_, "reference time can only be set for purely "
156 "time based term structure");
157 relativeTime_ = t;
158 notifyObservers();
159}
160
161inline void ModelImpliedYieldTermStructure::state(const Array& s) {
162 state_ = s;
163 notifyObservers();
164}
165
166inline void ModelImpliedYieldTermStructure::move(const Date& d, const Array& s) {
167 state_ = s;
168 referenceDate(d);
169}
170
171inline void ModelImpliedYieldTermStructure::move(const Time t, const Array& s) {
172 state_ = s;
173 referenceTime(t);
174 notifyObservers();
175}
176
178 if (!purelyTimeBased_) {
179 relativeTime_ = dayCounter().yearFraction(model_->termStructure()->referenceDate(), referenceDate_);
180 }
181 notifyObservers();
182}
183
185 QL_REQUIRE(t >= 0.0, "negative time (" << t << ") given");
186 return model_->discountBond(relativeTime_, relativeTime_ + t, state_);
187}
188
190 QL_REQUIRE(t >= 0.0, "negative time (" << t << ") given");
191 // if relativeTime_ is close to zero, we return the discount factor directly from the target curve
192 if (QuantLib::close_enough(relativeTime_, 0.0)) {
193 return targetCurve_->discount(t);
194 } else {
195 return model_->discountBond(relativeTime_, relativeTime_ + t, state_, targetCurve_);
196 }
197}
198
200 QL_REQUIRE(t >= 0.0, "negative time (" << t << ") given");
202 model_->termStructure()->discount(relativeTime_) / model_->termStructure()->discount(relativeTime_ + t);
203}
204
205} // namespace QuantExt
const QuantLib::ext::shared_ptr< IrModel > model_
ir model base class