Coverage Report

Created: 2025-12-08 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/basket/mcamericanbasketengine.cpp
Line
Count
Source
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
 <https://www.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
21
#include <ql/methods/montecarlo/lsmbasissystem.hpp>
22
#include <ql/pricingengines/basket/mcamericanbasketengine.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    AmericanBasketPathPricer::AmericanBasketPathPricer(
28
        Size assetNumber,
29
        ext::shared_ptr<Payoff> payoff,
30
        Size polynomialOrder,
31
        LsmBasisSystem::PolynomialType polynomialType)
32
0
    : assetNumber_(assetNumber), payoff_(std::move(payoff)),
33
0
      v_(LsmBasisSystem::multiPathBasisSystem(assetNumber_, polynomialOrder, polynomialType)) {
34
0
        QL_REQUIRE(   polynomialType == LsmBasisSystem::Monomial
35
0
                   || polynomialType == LsmBasisSystem::Laguerre
36
0
                   || polynomialType == LsmBasisSystem::Hermite
37
0
                   || polynomialType == LsmBasisSystem::Hyperbolic
38
0
                   || polynomialType == LsmBasisSystem::Chebyshev2nd,
39
0
                   "insufficient polynomial type");
40
41
0
        const ext::shared_ptr<BasketPayoff> basketPayoff
42
0
            = ext::dynamic_pointer_cast<BasketPayoff>(payoff_);
43
0
        QL_REQUIRE(basketPayoff, "payoff not a basket payoff");
44
45
0
        const ext::shared_ptr<StrikedTypePayoff> strikePayoff
46
0
            = ext::dynamic_pointer_cast<StrikedTypePayoff>(basketPayoff->basePayoff());
47
48
0
        if (strikePayoff != nullptr) {
49
0
            scalingValue_/=strikePayoff->strike();
50
0
        }
51
52
0
        v_.emplace_back([&](const Array& state) { return this->payoff(state); });
53
0
    }
54
55
    Array AmericanBasketPathPricer::state(const MultiPath& path,
56
0
                                          Size t) const {
57
0
        QL_REQUIRE(path.assetNumber() == assetNumber_, "invalid multipath");
58
59
0
        Array tmp(assetNumber_);
60
0
        for (Size i=0; i<assetNumber_; ++i) {
61
0
            tmp[i] = path[i][t]*scalingValue_;
62
0
        }
63
64
0
        return tmp;
65
0
    }
66
67
0
    Real AmericanBasketPathPricer::payoff(const Array& state) const {
68
0
        const ext::shared_ptr<BasketPayoff> basketPayoff
69
0
            = ext::dynamic_pointer_cast<BasketPayoff>(payoff_);
70
0
        QL_REQUIRE(basketPayoff, "payoff not a basket payoff");
71
72
0
        Real value = basketPayoff->accumulate(state);
73
0
        return (*payoff_)(value/scalingValue_);
74
0
    }
75
76
    Real AmericanBasketPathPricer::operator()(const MultiPath& path,
77
0
                                              Size t) const {
78
0
        return this->payoff(this->state(path, t));
79
0
    }
80
81
    std::vector<std::function<Real(Array)> >
82
0
    AmericanBasketPathPricer::basisSystem() const {
83
0
        return v_;
84
0
    }
85
86
}