1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2002, 2003 Ferdinando Ametrano
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file pathgenerator.hpp
\brief Generates random paths using a sequence generator
*/
#ifndef quantlib_montecarlo_path_generator_hpp
#define quantlib_montecarlo_path_generator_hpp
#include <ql/methods/montecarlo/brownianbridge.hpp>
#include <ql/stochasticprocess.hpp>
namespace QuantLib {
class StochasticProcess;
class StochasticProcess1D;
//! Generates random paths using a sequence generator
/*! Generates random paths with drift(S,t) and variance(S,t)
using a gaussian sequence generator
\ingroup mcarlo
\test the generated paths are checked against cached results
*/
template <class GSG>
class PathGenerator {
public:
typedef Sample<Path> sample_type;
// constructors
PathGenerator(const ext::shared_ptr<StochasticProcess>&,
Time length,
Size timeSteps,
const GSG& generator,
bool brownianBridge);
PathGenerator(const ext::shared_ptr<StochasticProcess>&,
const TimeGrid& timeGrid,
const GSG& generator,
bool brownianBridge);
//! \name inspectors
//@{
const sample_type& next() const;
const sample_type& antithetic() const;
Size size() const { return dimension_; }
const TimeGrid& timeGrid() const { return timeGrid_; }
//@}
private:
const sample_type& next(bool antithetic) const;
bool brownianBridge_;
GSG generator_;
Size dimension_;
TimeGrid timeGrid_;
ext::shared_ptr<StochasticProcess1D> process_;
mutable sample_type next_;
mutable std::vector<Real> temp_;
BrownianBridge bb_;
};
// template definitions
template <class GSG>
PathGenerator<GSG>::PathGenerator(
const ext::shared_ptr<StochasticProcess>& process,
Time length,
Size timeSteps,
const GSG& generator,
bool brownianBridge)
: brownianBridge_(brownianBridge), generator_(generator),
dimension_(generator_.dimension()), timeGrid_(length, timeSteps),
process_(ext::dynamic_pointer_cast<StochasticProcess1D>(process)),
next_(Path(timeGrid_),1.0), temp_(dimension_), bb_(timeGrid_) {
QL_REQUIRE(dimension_==timeSteps,
"sequence generator dimensionality (" << dimension_
<< ") != timeSteps (" << timeSteps << ")");
}
template <class GSG>
PathGenerator<GSG>::PathGenerator(
const ext::shared_ptr<StochasticProcess>& process,
const TimeGrid& timeGrid,
const GSG& generator,
bool brownianBridge)
: brownianBridge_(brownianBridge), generator_(generator),
dimension_(generator_.dimension()), timeGrid_(timeGrid),
process_(ext::dynamic_pointer_cast<StochasticProcess1D>(process)),
next_(Path(timeGrid_),1.0), temp_(dimension_), bb_(timeGrid_) {
QL_REQUIRE(dimension_==timeGrid_.size()-1,
"sequence generator dimensionality (" << dimension_
<< ") != timeSteps (" << timeGrid_.size()-1 << ")");
}
template <class GSG>
const typename PathGenerator<GSG>::sample_type&
PathGenerator<GSG>::next() const {
return next(false);
}
template <class GSG>
const typename PathGenerator<GSG>::sample_type&
PathGenerator<GSG>::antithetic() const {
return next(true);
}
template <class GSG>
const typename PathGenerator<GSG>::sample_type&
PathGenerator<GSG>::next(bool antithetic) const {
typedef typename GSG::sample_type sequence_type;
const sequence_type& sequence_ =
antithetic ? generator_.lastSequence()
: generator_.nextSequence();
if (brownianBridge_) {
bb_.transform(sequence_.value.begin(),
sequence_.value.end(),
temp_.begin());
} else {
std::copy(sequence_.value.begin(),
sequence_.value.end(),
temp_.begin());
}
next_.weight = sequence_.weight;
Path& path = next_.value;
path.front() = process_->x0();
for (Size i=1; i<path.length(); i++) {
Time t = timeGrid_[i-1];
Time dt = timeGrid_.dt(i-1);
path[i] = process_->evolve(t, path[i-1], dt,
antithetic ? -temp_[i-1] :
temp_[i-1]);
}
return next_;
}
}
#endif
|