QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcamericanengine.cpp
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 Klaus Spanderen
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file mcamericanengine.cpp
21 \brief Monte Carlo engine for vanilla american options
22*/
23
24#include <ql/errors.hpp>
27#include <utility>
28
29namespace QuantLib {
30
32 Size polynomialOrder,
33 LsmBasisSystem::PolynomialType polynomialType)
34 : payoff_(std::move(payoff)),
35 v_(LsmBasisSystem::pathBasisSystem(polynomialOrder, polynomialType)) {
36
37 QL_REQUIRE( polynomialType == LsmBasisSystem::Monomial
38 || polynomialType == LsmBasisSystem::Laguerre
39 || polynomialType == LsmBasisSystem::Hermite
40 || polynomialType == LsmBasisSystem::Hyperbolic
41 || polynomialType == LsmBasisSystem::Chebyshev2nd,
42 "insufficient polynomial type");
43
44 // the payoff gives an additional value
45 v_.emplace_back([&](Real state){ return this->payoff(state); });
46
47 const ext::shared_ptr<StrikedTypePayoff> strikePayoff
48 = ext::dynamic_pointer_cast<StrikedTypePayoff>(payoff_);
49
50 if (strikePayoff != nullptr) {
51 scalingValue_/=strikePayoff->strike();
52 }
53 }
54
56 return (*payoff_)(state/scalingValue_);
57 }
58
60 return payoff(state(path, t));
61 }
62
64 // scale values of the underlying
65 // to increase numerical stability
66 return path[t]*scalingValue_;
67 }
68
69 std::vector<ext::function<Real(Real)> >
71 return v_;
72 }
73
74}
std::vector< ext::function< Real(Real)> > basisSystem() const override
Real state(const Path &path, Size t) const override
Real payoff(Real state) const
Real operator()(const Path &path, Size t) const override
std::vector< ext::function< Real(Real)> > v_
AmericanPathPricer(ext::shared_ptr< Payoff > payoff, Size polynomialOrder, LsmBasisSystem::PolynomialType polynomialType)
const ext::shared_ptr< Payoff > payoff_
single-factor random walk
Definition: path.hpp:40
const DefaultType & t
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
const ext::shared_ptr< Payoff > payoff_
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
American Monte Carlo engine.
Definition: any.hpp:35
STL namespace.
Payoffs for various options.