/src/quantlib/ql/instruments/basketoption.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003 Neil Firth |
5 | | Copyright (C) 2007 StatPro Italia srl |
6 | | Copyright (C) 2007 Joseph Wang |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <https://www.quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | /*! \file basketoption.hpp |
23 | | \brief Basket option on a number of assets |
24 | | */ |
25 | | |
26 | | #ifndef quantlib_basket_option_hpp |
27 | | #define quantlib_basket_option_hpp |
28 | | |
29 | | #include <ql/instruments/multiassetoption.hpp> |
30 | | #include <ql/instruments/payoffs.hpp> |
31 | | #include <ql/math/array.hpp> |
32 | | #include <utility> |
33 | | |
34 | | namespace QuantLib { |
35 | | |
36 | | class BasketPayoff : public Payoff { |
37 | | private: |
38 | | ext::shared_ptr<Payoff> basePayoff_; |
39 | | public: |
40 | 0 | explicit BasketPayoff(ext::shared_ptr<Payoff> p) : basePayoff_(std::move(p)) {} |
41 | 0 | ~BasketPayoff() override = default; |
42 | 0 | std::string name() const override { return basePayoff_->name(); } |
43 | 0 | std::string description() const override { return basePayoff_->description(); } |
44 | 0 | Real operator()(Real price) const override { return (*basePayoff_)(price); } |
45 | 0 | virtual Real operator()(const Array &a) const { |
46 | 0 | return (*basePayoff_)(accumulate(a)); |
47 | 0 | } |
48 | | virtual Real accumulate(const Array &a) const = 0; |
49 | 0 | ext::shared_ptr<Payoff> basePayoff() { return basePayoff_; } |
50 | | }; |
51 | | |
52 | | class MinBasketPayoff : public BasketPayoff { |
53 | | public: |
54 | | explicit MinBasketPayoff(const ext::shared_ptr<Payoff> &p) |
55 | 0 | : BasketPayoff(p) {} |
56 | 0 | Real accumulate(const Array& a) const override { |
57 | 0 | return *std::min_element(a.begin(), a.end()); |
58 | 0 | } |
59 | | }; |
60 | | |
61 | | class MaxBasketPayoff : public BasketPayoff { |
62 | | public: |
63 | | explicit MaxBasketPayoff(const ext::shared_ptr<Payoff> &p) |
64 | 0 | : BasketPayoff(p) {} |
65 | 0 | Real accumulate(const Array& a) const override { |
66 | 0 | return *std::max_element(a.begin(), a.end()); |
67 | 0 | } |
68 | | }; |
69 | | |
70 | | class AverageBasketPayoff : public BasketPayoff { |
71 | | public: |
72 | | AverageBasketPayoff(const ext::shared_ptr<Payoff>& p, Array weights) |
73 | 0 | : BasketPayoff(p), weights_(std::move(weights)) {} |
74 | | AverageBasketPayoff(const ext::shared_ptr<Payoff> &p, |
75 | | Size n) |
76 | 0 | : BasketPayoff(p), weights_(n, 1.0/static_cast<Real>(n)) {} |
77 | 0 | Real accumulate(const Array& a) const override { |
78 | 0 | return std::inner_product(weights_.begin(), |
79 | 0 | weights_.end(), |
80 | 0 | a.begin(), Real(0.0)); |
81 | 0 | } |
82 | | |
83 | 0 | const Array& weights() const { return weights_; } |
84 | | |
85 | | private: |
86 | | Array weights_; |
87 | | }; |
88 | | |
89 | | |
90 | | class SpreadBasketPayoff : public BasketPayoff { |
91 | | public: |
92 | | explicit SpreadBasketPayoff(const ext::shared_ptr<Payoff> &p) |
93 | 0 | : BasketPayoff(p) {} |
94 | 0 | Real accumulate(const Array& a) const override { |
95 | 0 | QL_REQUIRE(a.size() == 2, |
96 | 0 | "payoff is only defined for two underlyings"); |
97 | 0 | return a[0]-a[1]; |
98 | 0 | } |
99 | | }; |
100 | | |
101 | | //! Basket option on a number of assets |
102 | | /*! \ingroup instruments */ |
103 | | class BasketOption : public MultiAssetOption { |
104 | | public: |
105 | | class engine; |
106 | | BasketOption(const ext::shared_ptr<BasketPayoff>&, |
107 | | const ext::shared_ptr<Exercise>&); |
108 | | }; |
109 | | |
110 | | //! %Basket-option %engine base class |
111 | | class BasketOption::engine |
112 | | : public GenericEngine<BasketOption::arguments, |
113 | | BasketOption::results> {}; |
114 | | |
115 | | } |
116 | | |
117 | | |
118 | | #endif |
119 | | |