QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mchullwhiteengine.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) 2006 Banca Profilo S.p.A.
5 Copyright (C) 2006 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file mchullwhiteengine.hpp
22 \brief Monte Carlo Hull-White engine for cap/floors
23*/
24
25#ifndef quantlib_mc_hull_white_cap_floor_engine_hpp
26#define quantlib_mc_hull_white_cap_floor_engine_hpp
27
32#include <utility>
33
34namespace QuantLib {
35
36 namespace detail {
37
38 class HullWhiteCapFloorPricer : public PathPricer<Path> {
39 public:
41 ext::shared_ptr<HullWhite>,
42 Time forwardMeasureTime);
43 Real operator()(const Path& path) const override;
44
45 private:
47 ext::shared_ptr<HullWhite> model_;
50 std::vector<Time> startTimes_, endTimes_, fixingTimes_;
51 };
52
53 }
54
55
56 //! Monte Carlo Hull-White engine for cap/floors
57 /*! \ingroup capfloorengines */
58 template <class RNG = PseudoRandom, class S = Statistics>
60 : public CapFloor::engine,
61 public McSimulation<SingleVariate,RNG,S> {
62 private:
64 ext::shared_ptr<HullWhite> model_;
69 public:
73
74 MCHullWhiteCapFloorEngine(ext::shared_ptr<HullWhite> model,
75 bool brownianBridge,
76 bool antitheticVariate,
77 Size requiredSamples,
78 Real requiredTolerance,
79 Size maxSamples,
80 BigNatural seed)
81 : McSimulation<SingleVariate, RNG, S>(antitheticVariate, false), model_(std::move(model)),
82 requiredSamples_(requiredSamples), maxSamples_(maxSamples),
83 requiredTolerance_(requiredTolerance), brownianBridge_(brownianBridge), seed_(seed) {
85 }
86
87 void calculate() const override {
91 results_.value = this->mcModel_->sampleAccumulator().mean();
92 if (RNG::allowsErrorEstimate)
94 this->mcModel_->sampleAccumulator().errorEstimate();
95 }
96
97 protected:
98 ext::shared_ptr<path_pricer_type> pathPricer() const {
99 Date referenceDate = model_->termStructure()->referenceDate();
100 DayCounter dayCounter = model_->termStructure()->dayCounter();
101 Time forwardMeasureTime =
102 dayCounter.yearFraction(referenceDate,
103 arguments_.endDates.back());
104 return ext::shared_ptr<path_pricer_type>(
106 forwardMeasureTime));
107 }
108
110
111 Date referenceDate = model_->termStructure()->referenceDate();
112 DayCounter dayCounter = model_->termStructure()->dayCounter();
113
114 // only add future fixing times...
115 std::vector<Time> times;
116 for (Size i=0; i<arguments_.fixingDates.size(); i++) {
117 if (arguments_.fixingDates[i] > referenceDate)
118 times.push_back(
119 dayCounter.yearFraction(referenceDate,
121 }
122 // ...and maturity.
123 times.push_back(
124 dayCounter.yearFraction(referenceDate,
125 arguments_.endDates.back()));
126 return TimeGrid(times.begin(), times.end());
127 }
128
129 ext::shared_ptr<path_generator_type> pathGenerator() const {
130
131 Handle<YieldTermStructure> curve = model_->termStructure();
132 Date referenceDate = curve->referenceDate();
133 DayCounter dayCounter = curve->dayCounter();
134
135 Time forwardMeasureTime =
136 dayCounter.yearFraction(referenceDate,
137 arguments_.endDates.back());
138 Array parameters = model_->params();
139 Real a = parameters[0], sigma = parameters[1];
140 ext::shared_ptr<HullWhiteForwardProcess> process(
141 new HullWhiteForwardProcess(curve, a, sigma));
142 process->setForwardMeasureTime(forwardMeasureTime);
143
144 TimeGrid grid = this->timeGrid();
145 typename RNG::rsg_type generator =
146 RNG::make_sequence_generator(grid.size()-1,seed_);
147 return ext::shared_ptr<path_generator_type>(
148 new path_generator_type(process, grid, generator,
150 }
151 };
152
153
154
155 //! Monte Carlo Hull-White cap-floor engine factory
156 template <class RNG = PseudoRandom, class S = Statistics>
158 public:
159 MakeMCHullWhiteCapFloorEngine(ext::shared_ptr<HullWhite>);
160 // named parameters
167 // conversion to pricing engine
168 operator ext::shared_ptr<PricingEngine>() const;
169 private:
170 ext::shared_ptr<HullWhite> model_;
171 bool antithetic_ = false;
174 bool brownianBridge_ = false;
176 };
177
178
179 // inline definitions
180
181 template <class RNG, class S>
183 ext::shared_ptr<HullWhite> model)
184 : model_(std::move(model)), samples_(Null<Size>()), maxSamples_(Null<Size>()),
185 tolerance_(Null<Real>()) {}
186
187 template <class RNG, class S>
190 QL_REQUIRE(tolerance_ == Null<Real>(),
191 "tolerance already set");
192 samples_ = samples;
193 return *this;
194 }
195
196 template <class RNG, class S>
199 Real tolerance) {
200 QL_REQUIRE(samples_ == Null<Size>(),
201 "number of samples already set");
202 QL_REQUIRE(RNG::allowsErrorEstimate,
203 "chosen random generator policy "
204 "does not allow an error estimate");
205 tolerance_ = tolerance;
206 return *this;
207 }
208
209 template <class RNG, class S>
212 maxSamples_ = samples;
213 return *this;
214 }
215
216 template <class RNG, class S>
219 seed_ = seed;
220 return *this;
221 }
222
223 template <class RNG, class S>
226 brownianBridge_ = b;
227 return *this;
228 }
229
230 template <class RNG, class S>
233 antithetic_ = b;
234 return *this;
235 }
236
237 template <class RNG, class S>
239 operator ext::shared_ptr<PricingEngine>() const {
240 return ext::shared_ptr<PricingEngine>(new
242 brownianBridge_, antithetic_,
243 samples_, tolerance_,
244 maxSamples_, seed_));
245 }
246
247}
248
249
250#endif
251
cap and floor class
1-D array used in linear algebra.
Definition: array.hpp:52
Arguments for cap/floor calculation
Definition: capfloor.hpp:138
std::vector< Date > endDates
Definition: capfloor.hpp:144
std::vector< Date > fixingDates
Definition: capfloor.hpp:143
base class for cap/floor engines
Definition: capfloor.hpp:158
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Shared handle to an observable.
Definition: handle.hpp:41
Forward Hull-White stochastic process
Monte Carlo Hull-White engine for cap/floors.
MCHullWhiteCapFloorEngine(ext::shared_ptr< HullWhite > model, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< HullWhite > model_
ext::shared_ptr< path_generator_type > pathGenerator() const
simulation::path_generator_type path_generator_type
McSimulation< SingleVariate, RNG, S > simulation
ext::shared_ptr< path_pricer_type > pathPricer() const
simulation::path_pricer_type path_pricer_type
Monte Carlo Hull-White cap-floor engine factory.
MakeMCHullWhiteCapFloorEngine & withAbsoluteTolerance(Real tolerance)
MakeMCHullWhiteCapFloorEngine(ext::shared_ptr< HullWhite >)
MakeMCHullWhiteCapFloorEngine & withSeed(BigNatural seed)
MakeMCHullWhiteCapFloorEngine & withMaxSamples(Size samples)
MakeMCHullWhiteCapFloorEngine & withBrownianBridge(bool b=true)
MakeMCHullWhiteCapFloorEngine & withSamples(Size samples)
MakeMCHullWhiteCapFloorEngine & withAntitheticVariate(bool b=true)
base class for Monte Carlo engines
MonteCarloModel< MC, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< MonteCarloModel< MC, RNG, S > > mcModel_
void calculate(Real requiredTolerance, Size requiredSamples, Size maxSamples) const
basic calculate method provided to inherited pricing engines
MonteCarloModel< MC, RNG, S >::path_pricer_type path_pricer_type
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
single-factor random walk
Definition: path.hpp:40
base class for path pricers
Definition: pathpricer.hpp:40
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
Real operator()(const Path &path) const override
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
ext::function< Real(Real)> b
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
Real sigma
Hull & White (HW) model.
Hull-White stochastic processes.
framework for Monte Carlo engines
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
default Monte Carlo traits for single-variate models
Definition: mctraits.hpp:39