QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcamericanengine.cpp
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
24#include <ql/errors.hpp>
25#include <ql/instruments/payoffs.hpp>
26#include <ql/pricingengines/vanilla/mcamericanengine.hpp>
27#include <utility>
28
29namespace QuantLib {
30
31 AmericanPathPricer::AmericanPathPricer(ext::shared_ptr<Payoff> payoff,
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
63 Real AmericanPathPricer::state(const Path& path, Size t) const {
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
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.