/src/quantlib/ql/pricingengines/vanilla/mcamericanengine.cpp
Line | Count | Source (jump to first uncovered line) |
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> |
25 | | #include <ql/instruments/payoffs.hpp> |
26 | | #include <ql/pricingengines/vanilla/mcamericanengine.hpp> |
27 | | #include <utility> |
28 | | |
29 | | namespace QuantLib { |
30 | | |
31 | | AmericanPathPricer::AmericanPathPricer(ext::shared_ptr<Payoff> payoff, |
32 | | Size polynomialOrder, |
33 | | LsmBasisSystem::PolynomialType polynomialType) |
34 | 0 | : payoff_(std::move(payoff)), |
35 | 0 | v_(LsmBasisSystem::pathBasisSystem(polynomialOrder, polynomialType)) { |
36 | |
|
37 | 0 | QL_REQUIRE( polynomialType == LsmBasisSystem::Monomial |
38 | 0 | || polynomialType == LsmBasisSystem::Laguerre |
39 | 0 | || polynomialType == LsmBasisSystem::Hermite |
40 | 0 | || polynomialType == LsmBasisSystem::Hyperbolic |
41 | 0 | || polynomialType == LsmBasisSystem::Chebyshev2nd, |
42 | 0 | "insufficient polynomial type"); |
43 | | |
44 | | // the payoff gives an additional value |
45 | 0 | v_.emplace_back([&](Real state){ return this->payoff(state); }); |
46 | |
|
47 | 0 | const ext::shared_ptr<StrikedTypePayoff> strikePayoff |
48 | 0 | = ext::dynamic_pointer_cast<StrikedTypePayoff>(payoff_); |
49 | |
|
50 | 0 | if (strikePayoff != nullptr) { |
51 | 0 | scalingValue_/=strikePayoff->strike(); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 0 | Real AmericanPathPricer::payoff(Real state) const { |
56 | 0 | return (*payoff_)(state/scalingValue_); |
57 | 0 | } |
58 | | |
59 | 0 | Real AmericanPathPricer::operator()(const Path& path, Size t) const { |
60 | 0 | return payoff(state(path, t)); |
61 | 0 | } |
62 | | |
63 | 0 | Real AmericanPathPricer::state(const Path& path, Size t) const { |
64 | | // scale values of the underlying |
65 | | // to increase numerical stability |
66 | 0 | return path[t]*scalingValue_; |
67 | 0 | } |
68 | | |
69 | | std::vector<std::function<Real(Real)> > |
70 | 0 | AmericanPathPricer::basisSystem() const { |
71 | 0 | return v_; |
72 | 0 | } |
73 | | |
74 | | } |