QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mchimalayaengine.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) 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
21#include <ql/payoff.hpp>
22#include <utility>
23
24namespace QuantLib {
25
27 DiscountFactor discount)
28 : payoff_(std::move(payoff)), discount_(discount) {}
29
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(fixings, numAssets);
59
60 Real payoff = (*payoff_)(averagePrice);
61 return payoff * discount_;
62 }
63
64}
65
Real operator()(const MultiPath &multiPath) const override
HimalayaMultiPathPricer(ext::shared_ptr< Payoff > payoff, DiscountFactor discount)
Correlated multiple asset paths.
Definition: multipath.hpp:39
Size pathSize() const
Definition: multipath.hpp:48
Size assetNumber() const
Definition: multipath.hpp:47
#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_
#define QL_MIN_REAL
Definition: qldefines.hpp:175
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Monte Carlo engine for Himalaya options.
Definition: any.hpp:35
STL namespace.
Option payoff classes.