Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
staticallycorrectedyieldtermstructure.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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 staticallycorrectedyieldtermstructure.hpp
20 \brief Statically corrected yield term structure
21 \ingroup termstructures
22*/
23
24#ifndef quantext_statically_corrected_yts_hpp
25#define quantext_statically_corrected_yts_hpp
26
28
29#include <ql/termstructures/yieldtermstructure.hpp>
30
31#include <boost/unordered_map.hpp>
32
33namespace QuantExt {
34using namespace QuantLib;
35
36//! Statically Corrected Yield Term Structure
37/*! This termstructure takes a floating reference date term structure
38 and two fixed reference date term structures, applying a static
39 correction to the floating ts implied by the two fixed ones.
40 Usually the floating term structure will coincide with
41 the first fixed at construction time. Also, the two fixed
42 termstructures should have the same reference date and all three
43 termstructures should have the same day counter.
44
45 \ingroup termstructures
46 */
47class StaticallyCorrectedYieldTermStructure : public YieldTermStructure {
48public:
49 StaticallyCorrectedYieldTermStructure(const Handle<YieldTermStructure>& floatingTermStructure,
50 const Handle<YieldTermStructure>& fixedSourceTermStructure,
51 const Handle<YieldTermStructure>& fixedTargetTermStructure,
52 const YieldCurveRollDown& rollDown = ForwardForward)
53 : YieldTermStructure(floatingTermStructure->dayCounter()), x_(floatingTermStructure),
54 source_(fixedSourceTermStructure), target_(fixedTargetTermStructure), rollDown_(rollDown) {
55 registerWith(floatingTermStructure);
56 registerWith(fixedSourceTermStructure);
57 registerWith(fixedTargetTermStructure);
58 }
59
60 Date maxDate() const override { return x_->maxDate(); }
61 void update() override {}
62 const Date& referenceDate() const override { return x_->referenceDate(); }
63
64 Calendar calendar() const override { return x_->calendar(); }
65 Natural settlementDays() const override { return x_->settlementDays(); }
66
67 void flushCache() { cache_c_.clear(); }
68
69protected:
70 Real discountImpl(Time t) const override;
71
72private:
73 // FIXME: remove cache
74 // cache for source and target forwards
75 struct cache_key {
76 double t0, t;
77 bool operator==(const cache_key& o) const { return (t0 == o.t0) && (t == o.t); }
78 };
79 struct cache_hasher {
80 std::size_t operator()(cache_key const& x) const {
81 std::size_t seed = 0;
82 boost::hash_combine(seed, x.t0);
83 boost::hash_combine(seed, x.t);
84 return seed;
85 }
86 };
87 mutable boost::unordered_map<cache_key, Real, cache_hasher> cache_c_;
88 // end cache
89 const Handle<YieldTermStructure> x_, source_, target_;
91};
92
93// inline
94
96 Real c = 1.0;
98 Real t0 = source_->timeFromReference(referenceDate());
99 // roll down = ForwardForward
100 // cache lookup
101 cache_key k = { t0, t };
102 boost::unordered_map<cache_key, Real>::const_iterator i = cache_c_.find(k);
103 if (i == cache_c_.end()) {
104 c = source_->discount(t0) / source_->discount(t0 + t) * target_->discount(t0 + t) / target_->discount(t0);
105 cache_c_.insert(std::make_pair(k, c));
106 } else {
107 c = i->second;
108 }
109 } else {
110 // roll down = ConstantDiscount
111 // cache lookup
112 cache_key k = { 0.0, t };
113 boost::unordered_map<cache_key, Real>::const_iterator i = cache_c_.find(k);
114 if (i == cache_c_.end()) {
115 c = target_->discount(t) / source_->discount(t);
116 cache_c_.insert(std::make_pair(k, c));
117 } else {
118 c = i->second;
119 }
120 }
121 return x_->discount(t) * c;
122}
123
124} // namespace QuantExt
125
126#endif
boost::unordered_map< cache_key, Real, cache_hasher > cache_c_
StaticallyCorrectedYieldTermStructure(const Handle< YieldTermStructure > &floatingTermStructure, const Handle< YieldTermStructure > &fixedSourceTermStructure, const Handle< YieldTermStructure > &fixedTargetTermStructure, const YieldCurveRollDown &rollDown=ForwardForward)
dynamics type definitions
YieldCurveRollDown
Yield Curve Roll Down.