QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
bootstraphelper.hpp
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
26#ifndef quantlib_bootstrap_helper_hpp
27#define quantlib_bootstrap_helper_hpp
28
29#include <ql/handle.hpp>
30#include <ql/patterns/observable.hpp>
31#include <ql/patterns/visitor.hpp>
32#include <ql/quote.hpp>
33#include <ql/quotes/simplequote.hpp>
34#include <ql/settings.hpp>
35#include <ql/time/date.hpp>
36#include <utility>
37
38namespace QuantLib {
39
40 struct Pillar {
42
43 enum Choice {
47 };
48 };
49
50 std::ostream& operator<<(std::ostream& out, Pillar::Choice type);
51
53
62 template <class TS>
63 class BootstrapHelper : public Observer, public Observable {
64 public:
67 ~BootstrapHelper() override = default;
69
70 const Handle<Quote>& quote() const { return quote_; }
71 virtual Real impliedQuote() const = 0;
72 Real quoteError() const { return quote_->value() - impliedQuote(); }
74
83 virtual void setTermStructure(TS*);
84
86
89 virtual Date earliestDate() const;
90
92 virtual Date maturityDate() const;
93
95
99 virtual Date latestRelevantDate() const;
100
102 virtual Date pillarDate() const;
103
105
107 virtual Date latestDate() const;
109
111 void update() override;
113
115 virtual void accept(AcyclicVisitor&);
117 protected:
122 };
123
125
128 template <class TS>
130 public:
134
135 void update() override {
136 if (evaluationDate_ != Settings::instance().evaluationDate()) {
139 }
141 }
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_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(quote)))),
159 termStructure_(nullptr) {}
160
161 template <class TS>
163 QL_REQUIRE(t != nullptr, "null term structure given");
164 termStructure_ = t;
165 }
166
167 template <class TS>
169 return earliestDate_;
170 }
171
172 template <class TS>
174 if (maturityDate_ == Date())
175 return latestRelevantDate();
176 return maturityDate_;
177 }
178
179 template <class TS>
181 if (latestRelevantDate_ == Date())
182 return latestDate();
183 return latestRelevantDate_;
184 }
185
186 template <class TS>
188 if (pillarDate_==Date())
189 return latestDate();
190 return pillarDate_;
191 }
192
193 template <class TS>
195 if (latestDate_ == Date())
196 return pillarDate_;
197 return latestDate_;
198 }
199
200 template <class TS>
202 notifyObservers();
203 }
204
205 template <class TS>
207 auto* v1 = dynamic_cast<Visitor<BootstrapHelper<TS> >*>(&v);
208 if (v1 != nullptr)
209 v1->visit(*this);
210 else
211 QL_FAIL("not a bootstrap-helper visitor");
212 }
213
214 template <class TS>
216 const Handle<Quote>& quote)
217 : BootstrapHelper<TS>(quote) {
218 this->registerWith(Settings::instance().evaluationDate());
220 }
221
222 template <class TS>
224 : BootstrapHelper<TS>(quote) {
225 this->registerWith(Settings::instance().evaluationDate());
227 }
228
229 inline std::ostream& operator<<(std::ostream& out,
230 Pillar::Choice t) {
231 switch (t) {
233 return out << "MaturityPillarDate";
235 return out << "LastRelevantPillarDate";
237 return out << "CustomPillarDate";
238 default:
239 QL_FAIL("unknown Pillar::Choice(" << Integer(t) << ")");
240 }
241 }
242
243 namespace detail {
244
246 public:
247 template <class Helper>
249 const ext::shared_ptr<Helper>& h1,
250 const ext::shared_ptr<Helper>& h2) const {
251 return (h1->pillarDate() < h2->pillarDate());
252 }
253 };
254
255 }
256
257}
258
259#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
purely virtual base class for market observables
Definition: quote.hpp:37
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
market element returning a stored value
Definition: simplequote.hpp:33
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
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
STL namespace.
Choice
Enumeration for pillar determination alternatives.
@ CustomDate
last date relevant for instrument pricing
@ LastRelevantDate
instruments maturity date