Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/experimental/exoticoptions/mchimalayaengine.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) 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
0
    : payoff_(std::move(payoff)), discount_(discount) {}
29
30
    Real HimalayaMultiPathPricer::operator()(const MultiPath& multiPath)
31
0
                                                                      const {
32
0
        Size numAssets = multiPath.assetNumber();
33
0
        Size numNodes = multiPath.pathSize();
34
0
        QL_REQUIRE(numAssets > 0, "no asset given");
35
36
0
        std::vector<bool> remainingAssets(numAssets, true);
37
0
        Real averagePrice = 0.0;
38
0
        Size fixings = numNodes-1;
39
0
        for (Size i = 1; i < numNodes; i++) {
40
0
            Real bestPrice = 0.0;
41
0
            Real bestYield = QL_MIN_REAL;
42
            // dummy assignement to avoid compiler warning
43
0
            Size removeAsset = 0;
44
0
            for (Size j = 0; j < numAssets; j++) {
45
0
                if (remainingAssets[j]) {
46
0
                    Real price = multiPath[j][i];
47
0
                    Real yield = price/multiPath[j].front();
48
0
                    if (yield >= bestYield) {
49
0
                        bestPrice = price;
50
0
                        bestYield = yield;
51
0
                        removeAsset = j;
52
0
                    }
53
0
                }
54
0
            }
55
0
            remainingAssets[removeAsset] = false;
56
0
            averagePrice += bestPrice;
57
0
        }
58
0
        averagePrice /= std::min(fixings, numAssets);
59
60
0
        Real payoff = (*payoff_)(averagePrice);
61
0
        return payoff * discount_;
62
0
    }
63
64
}
65