QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bootstraphelper.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006, 2007, 2008 StatPro Italia srl
5 Copyright (C) 2007, 2009, 2015 Ferdinando Ametrano
6 Copyright (C) 2015 Paolo Mazzocchi
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file bootstraphelper.hpp
23 \brief base helper class used for bootstrapping
24*/
25
26#ifndef quantlib_bootstrap_helper_hpp
27#define quantlib_bootstrap_helper_hpp
28
29#include <ql/handle.hpp>
32#include <ql/quote.hpp>
34#include <ql/settings.hpp>
35#include <ql/time/date.hpp>
36#include <utility>
37
38namespace QuantLib {
39
40 struct Pillar {
41 //! Enumeration for pillar determination alternatives
42 /*! These alternatives specify the determination of the pillar date. */
43 enum Choice {
44 MaturityDate, //! instruments maturity date
45 LastRelevantDate, //! last date relevant for instrument pricing
46 CustomDate //! custom choice
47 };
48 };
49
50 std::ostream& operator<<(std::ostream& out, Pillar::Choice type);
51
52 //! Base helper class for bootstrapping
53 /*! This class provides an abstraction for the instruments used to
54 bootstrap a term structure.
55
56 It is advised that a bootstrap helper for an instrument
57 contains an instance of the actual instrument class to ensure
58 consistancy between the algorithms used during bootstrapping
59 and later instrument pricing. This is not yet fully enforced
60 in the available bootstrap helpers.
61 */
62 template <class TS>
63 class BootstrapHelper : public Observer, public Observable {
64 public:
67 ~BootstrapHelper() override = default;
68 //! \name BootstrapHelper interface
69 //@{
70 const Handle<Quote>& quote() const { return quote_; }
71 virtual Real impliedQuote() const = 0;
72 Real quoteError() const { return quote_->value() - impliedQuote(); }
73 //! sets the term structure to be used for pricing
74 /*! \warning Being a pointer and not a shared_ptr, the term
75 structure is not guaranteed to remain allocated
76 for the whole life of the rate helper. It is
77 responsibility of the programmer to ensure that
78 the pointer remains valid. It is advised that
79 this method is called only inside the term
80 structure being bootstrapped, setting the pointer
81 to <b>this</b>, i.e., the term structure itself.
82 */
83 virtual void setTermStructure(TS*);
84
85 //! earliest relevant date
86 /*! The earliest date at which data are needed by the
87 helper in order to provide a quote.
88 */
89 virtual Date earliestDate() const;
90
91 //! instrument's maturity date
92 virtual Date maturityDate() const;
93
94 //! latest relevant date
95 /*! The latest date at which data are needed by the helper
96 in order to provide a quote. It does not necessarily
97 equal the maturity of the underlying instrument.
98 */
99 virtual Date latestRelevantDate() const;
100
101 //! pillar date
102 virtual Date pillarDate() const;
103
104 //! latest date
105 /*! equal to pillarDate()
106 */
107 virtual Date latestDate() const;
108 //@}
109 //! \name Observer interface
110 //@{
111 void update() override;
112 //@}
113 //! \name Visitability
114 //@{
115 virtual void accept(AcyclicVisitor&);
116 //@}
117 protected:
122 };
123
124 //! Bootstrap helper with date schedule relative to global evaluation date
125 /*! Derived classes must takes care of rebuilding the date schedule when
126 the global evaluation date changes
127 */
128 template <class TS>
130 public:
133 //! \name Observer interface
134 //@{
135 void update() override {
136 if (evaluationDate_ != Settings::instance().evaluationDate()) {
139 }
141 }
142 //@}
143 protected:
144 virtual void initializeDates() = 0;
146 };
147
148 // template definitions
149
150 template <class TS>
152 : quote_(std::move(quote)), termStructure_(nullptr) {
154 }
155
156 template <class TS>
158 : quote_(makeQuoteHandle(quote)), termStructure_(nullptr) {}
159
160 template <class TS>
162 QL_REQUIRE(t != nullptr, "null term structure given");
163 termStructure_ = t;
164 }
165
166 template <class TS>
168 return earliestDate_;
169 }
170
171 template <class TS>
173 if (maturityDate_ == Date())
174 return latestRelevantDate();
175 return maturityDate_;
176 }
177
178 template <class TS>
180 if (latestRelevantDate_ == Date())
181 return latestDate();
182 return latestRelevantDate_;
183 }
184
185 template <class TS>
187 if (pillarDate_==Date())
188 return latestDate();
189 return pillarDate_;
190 }
191
192 template <class TS>
194 if (latestDate_ == Date())
195 return pillarDate_;
196 return latestDate_;
197 }
198
199 template <class TS>
201 notifyObservers();
202 }
203
204 template <class TS>
206 auto* v1 = dynamic_cast<Visitor<BootstrapHelper<TS> >*>(&v);
207 if (v1 != nullptr)
208 v1->visit(*this);
209 else
210 QL_FAIL("not a bootstrap-helper visitor");
211 }
212
213 template <class TS>
215 const Handle<Quote>& quote)
216 : BootstrapHelper<TS>(quote) {
217 this->registerWith(Settings::instance().evaluationDate());
219 }
220
221 template <class TS>
223 : BootstrapHelper<TS>(quote) {
224 this->registerWith(Settings::instance().evaluationDate());
226 }
227
228 inline std::ostream& operator<<(std::ostream& out,
230 switch (t) {
232 return out << "MaturityPillarDate";
234 return out << "LastRelevantPillarDate";
236 return out << "CustomPillarDate";
237 default:
238 QL_FAIL("unknown Pillar::Choice(" << Integer(t) << ")");
239 }
240 }
241
242 namespace detail {
243
245 public:
246 template <class Helper>
248 const ext::shared_ptr<Helper>& h1,
249 const ext::shared_ptr<Helper>& h2) const {
250 return (h1->pillarDate() < h2->pillarDate());
251 }
252 };
253
254 }
255
256}
257
258#endif
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
Base helper class for bootstrapping.
virtual Real impliedQuote() const =0
~BootstrapHelper() override=default
const Handle< Quote > & quote() const
virtual void accept(AcyclicVisitor &)
virtual Date earliestDate() const
earliest relevant date
virtual Date pillarDate() const
pillar date
virtual Date latestDate() const
latest date
BootstrapHelper(Handle< Quote > quote)
virtual Date maturityDate() const
instrument's maturity date
virtual Date latestRelevantDate() const
latest relevant date
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Bootstrap helper with date schedule relative to global evaluation date.
RelativeDateBootstrapHelper(const Handle< Quote > &quote)
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
bool operator()(const ext::shared_ptr< Helper > &h1, const ext::shared_ptr< Helper > &h2) const
date- and time-related classes, typedefs and enumerations
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Globally accessible relinkable pointer.
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
RelinkableHandle< Quote > makeQuoteHandle(Real value)
Definition: simplequote.hpp:56
STL namespace.
observer/observable pattern
ext::shared_ptr< BlackVolTermStructure > v
purely virtual base class for market observables
global repository for run-time library settings
simple quote class
Choice
Enumeration for pillar determination alternatives.
@ CustomDate
last date relevant for instrument pricing
@ LastRelevantDate
instruments maturity date
SimpleQuote & quote_
degenerate base class for the Acyclic Visitor pattern