QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcamericanbasketengine.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) 2004 Neil Firth
5 Copyright (C) 2006 Klaus Spanderen
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
23#include <utility>
24
25namespace QuantLib {
26
28 Size assetNumber,
29 ext::shared_ptr<Payoff> payoff,
30 Size polynomialOrder,
31 LsmBasisSystem::PolynomialType polynomialType)
32 : assetNumber_(assetNumber), payoff_(std::move(payoff)),
33 v_(LsmBasisSystem::multiPathBasisSystem(assetNumber_, polynomialOrder, polynomialType)) {
34 QL_REQUIRE( polynomialType == LsmBasisSystem::Monomial
35 || polynomialType == LsmBasisSystem::Laguerre
36 || polynomialType == LsmBasisSystem::Hermite
37 || polynomialType == LsmBasisSystem::Hyperbolic
38 || polynomialType == LsmBasisSystem::Chebyshev2nd,
39 "insufficient polynomial type");
40
41 const ext::shared_ptr<BasketPayoff> basketPayoff
42 = ext::dynamic_pointer_cast<BasketPayoff>(payoff_);
43 QL_REQUIRE(basketPayoff, "payoff not a basket payoff");
44
45 const ext::shared_ptr<StrikedTypePayoff> strikePayoff
46 = ext::dynamic_pointer_cast<StrikedTypePayoff>(basketPayoff->basePayoff());
47
48 if (strikePayoff != nullptr) {
49 scalingValue_/=strikePayoff->strike();
50 }
51
52 v_.emplace_back([&](const Array& state) { return this->payoff(state); });
53 }
54
56 Size t) const {
57 QL_REQUIRE(path.assetNumber() == assetNumber_, "invalid multipath");
58
60 for (Size i=0; i<assetNumber_; ++i) {
61 tmp[i] = path[i][t]*scalingValue_;
62 }
63
64 return tmp;
65 }
66
68 const ext::shared_ptr<BasketPayoff> basketPayoff
69 = ext::dynamic_pointer_cast<BasketPayoff>(payoff_);
70 QL_REQUIRE(basketPayoff, "payoff not a basket payoff");
71
72 Real value = basketPayoff->accumulate(state);
73 return (*payoff_)(value/scalingValue_);
74 }
75
77 Size t) const {
78 return this->payoff(this->state(path, t));
79 }
80
81 std::vector<ext::function<Real(Array)> >
83 return v_;
84 }
85
86}
Real payoff(const Array &state) const
Real operator()(const MultiPath &path, Size t) const override
Array state(const MultiPath &path, Size t) const override
std::vector< ext::function< Real(Array)> > basisSystem() const override
std::vector< ext::function< Real(Array)> > v_
AmericanBasketPathPricer(Size assetNumber, ext::shared_ptr< Payoff > payoff, Size polynomialOrder=2, LsmBasisSystem::PolynomialType polynomialType=LsmBasisSystem::Monomial)
const ext::shared_ptr< Payoff > payoff_
1-D array used in linear algebra.
Definition: array.hpp:52
Correlated multiple asset paths.
Definition: multipath.hpp:39
Size assetNumber() const
Definition: multipath.hpp:47
const DefaultType & t
#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
utility classes for Longstaff-Schwartz early-exercise Monte Carlo
Least-square Monte Carlo engines.
Definition: any.hpp:35
STL namespace.