QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 //! Alternatives ways of determining the pillar date
42 enum Choice {
43 MaturityDate, //! instruments maturity date
44 LastRelevantDate, //! last date relevant for instrument pricing
45 CustomDate //! custom choice
46 };
47 };
48
49 std::ostream& operator<<(std::ostream& out, Pillar::Choice type);
50
51 //! Base helper class for bootstrapping
52 /*! This class provides an abstraction for the instruments used to
53 bootstrap a term structure.
54
55 It is advised that a bootstrap helper for an instrument
56 contains an instance of the actual instrument class to ensure
57 consistancy between the algorithms used during bootstrapping
58 and later instrument pricing. This is not yet fully enforced
59 in the available bootstrap helpers.
60 */
61 template <class TS>
62 class BootstrapHelper : public Observer, public Observable {
63 public:
64 explicit BootstrapHelper(const std::variant<Spread, Handle<Quote>>& quote);
65 ~BootstrapHelper() override = default;
66 //! \name BootstrapHelper interface
67 //@{
68 const Handle<Quote>& quote() const { return quote_; }
69 virtual Real impliedQuote() const = 0;
70 Real quoteError() const { return quote_->value() - impliedQuote(); }
71 //! sets the term structure to be used for pricing
72 /*! \warning Being a pointer and not a shared_ptr, the term
73 structure is not guaranteed to remain allocated
74 for the whole life of the rate helper. It is
75 responsibility of the programmer to ensure that
76 the pointer remains valid. It is advised that
77 this method is called only inside the term
78 structure being bootstrapped, setting the pointer
79 to <b>this</b>, i.e., the term structure itself.
80 */
81 virtual void setTermStructure(TS*);
82
83 //! earliest relevant date
84 /*! The earliest date at which data are needed by the
85 helper in order to provide a quote.
86 */
87 virtual Date earliestDate() const;
88
89 //! instrument's maturity date
90 virtual Date maturityDate() const;
91
92 //! latest relevant date
93 /*! The latest date at which data are needed by the helper
94 in order to provide a quote. It does not necessarily
95 equal the maturity of the underlying instrument.
96 */
97 virtual Date latestRelevantDate() const;
98
99 //! pillar date
100 virtual Date pillarDate() const;
101
102 //! latest date
103 /*! equal to pillarDate()
104 */
105 virtual Date latestDate() const;
106 //@}
107 //! \name Observer interface
108 //@{
109 void update() override;
110 //@}
111 //! \name Visitability
112 //@{
113 virtual void accept(AcyclicVisitor&);
114 //@}
115 protected:
120 };
121
122 //! Bootstrap helper with date schedule relative to global evaluation date
123 /*! Derived classes must takes care of rebuilding the date schedule when
124 the global evaluation date changes
125 */
126 template <class TS>
128 public:
130 const std::variant<Spread, Handle<Quote>>& quote,
131 bool updateDates = true);
132
133 //! \name Observer interface
134 //@{
135 void update() override {
136 if (updateDates_ && evaluationDate_ != Settings::instance().evaluationDate()) {
139 }
141 }
142 //@}
143 protected:
144 virtual void initializeDates() = 0;
147 };
148
149 // template definitions
150
151 template <class TS>
153 : quote_(handleFromVariant(quote)), termStructure_(nullptr) {
155 }
156
157 template <class TS>
159 QL_REQUIRE(t != nullptr, "null term structure given");
160 termStructure_ = t;
161 }
162
163 template <class TS>
165 return earliestDate_;
166 }
167
168 template <class TS>
170 if (maturityDate_ == Date())
171 return latestRelevantDate();
172 return maturityDate_;
173 }
174
175 template <class TS>
177 if (latestRelevantDate_ == Date())
178 return latestDate();
179 return latestRelevantDate_;
180 }
181
182 template <class TS>
184 if (pillarDate_==Date())
185 return latestDate();
186 return pillarDate_;
187 }
188
189 template <class TS>
191 if (latestDate_ == Date())
192 return pillarDate_;
193 return latestDate_;
194 }
195
196 template <class TS>
198 notifyObservers();
199 }
200
201 template <class TS>
203 auto* v1 = dynamic_cast<Visitor<BootstrapHelper<TS> >*>(&v);
204 if (v1 != nullptr)
205 v1->visit(*this);
206 else
207 QL_FAIL("not a bootstrap-helper visitor");
208 }
209
210
211 template <class TS>
213 const std::variant<Spread, Handle<Quote>>& quote, bool updateDates)
214 : BootstrapHelper<TS>(quote), updateDates_(updateDates) {
215 if (updateDates) {
216 this->registerWith(Settings::instance().evaluationDate());
218 }
219 }
220
221
222 inline std::ostream& operator<<(std::ostream& out,
224 switch (t) {
226 return out << "MaturityPillarDate";
228 return out << "LastRelevantPillarDate";
230 return out << "CustomPillarDate";
231 default:
232 QL_FAIL("unknown Pillar::Choice(" << Integer(t) << ")");
233 }
234 }
235
236 namespace detail {
237
239 public:
240 template <class Helper>
242 const ext::shared_ptr<Helper>& h1,
243 const ext::shared_ptr<Helper>& h2) const {
244 return (h1->pillarDate() < h2->pillarDate());
245 }
246 };
247
248 }
249
250}
251
252#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
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
BootstrapHelper(const std::variant< Spread, Handle< Quote > > &quote)
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:226
Bootstrap helper with date schedule relative to global evaluation date.
RelativeDateBootstrapHelper(const std::variant< Spread, Handle< Quote > > &quote, bool updateDates=true)
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
Real Spread
spreads on interest rates
Definition: types.hpp:74
Globally accessible relinkable pointer.
Definition: any.hpp:37
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
Handle< Quote > handleFromVariant(const std::variant< Real, Handle< Quote > > &value)
Definition: quote.cpp:27
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
Alternatives ways of determining the pillar date.
@ CustomDate
last date relevant for instrument pricing
@ LastRelevantDate
instruments maturity date
SimpleQuote & quote_
degenerate base class for the Acyclic Visitor pattern