1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | |
3 | /* |
4 | Copyright (C) 2008 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis |
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 | #include <ql/experimental/exoticoptions/mchimalayaengine.hpp> |
21 | #include <ql/payoff.hpp> |
22 | #include <utility> |
23 | |
24 | namespace QuantLib { |
25 | |
26 | HimalayaMultiPathPricer::HimalayaMultiPathPricer(ext::shared_ptr<Payoff> payoff, |
27 | DiscountFactor discount) |
28 | : payoff_(std::move(payoff)), discount_(discount) {} |
29 | |
30 | Real HimalayaMultiPathPricer::operator()(const MultiPath& multiPath) |
31 | const { |
32 | Size numAssets = multiPath.assetNumber(); |
33 | Size numNodes = multiPath.pathSize(); |
34 | QL_REQUIRE(numAssets > 0, "no asset given" ); |
35 | |
36 | std::vector<bool> remainingAssets(numAssets, true); |
37 | Real averagePrice = 0.0; |
38 | Size fixings = numNodes-1; |
39 | for (Size i = 1; i < numNodes; i++) { |
40 | Real bestPrice = 0.0; |
41 | Real bestYield = QL_MIN_REAL; |
42 | // dummy assignement to avoid compiler warning |
43 | Size removeAsset = 0; |
44 | for (Size j = 0; j < numAssets; j++) { |
45 | if (remainingAssets[j]) { |
46 | Real price = multiPath[j][i]; |
47 | Real yield = price/multiPath[j].front(); |
48 | if (yield >= bestYield) { |
49 | bestPrice = price; |
50 | bestYield = yield; |
51 | removeAsset = j; |
52 | } |
53 | } |
54 | } |
55 | remainingAssets[removeAsset] = false; |
56 | averagePrice += bestPrice; |
57 | } |
58 | averagePrice /= std::min(a: fixings, b: numAssets); |
59 | |
60 | Real payoff = (*payoff_)(averagePrice); |
61 | return payoff * discount_; |
62 | } |
63 | |
64 | } |
65 | |
66 | |