QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
basketoption.hpp
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) 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 <http://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
31#include <ql/math/array.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 class BasketPayoff : public Payoff {
37 private:
38 ext::shared_ptr<Payoff> basePayoff_;
39 public:
40 explicit BasketPayoff(ext::shared_ptr<Payoff> p) : basePayoff_(std::move(p)) {}
41 ~BasketPayoff() override = default;
42 std::string name() const override { return basePayoff_->name(); }
43 std::string description() const override { return basePayoff_->description(); }
44 Real operator()(Real price) const override { return (*basePayoff_)(price); }
45 virtual Real operator()(const Array &a) const {
46 return (*basePayoff_)(accumulate(a));
47 }
48 virtual Real accumulate(const Array &a) const = 0;
49 ext::shared_ptr<Payoff> basePayoff() { return basePayoff_; }
50 };
51
53 public:
54 explicit MinBasketPayoff(const ext::shared_ptr<Payoff> &p)
55 : BasketPayoff(p) {}
56 Real accumulate(const Array& a) const override {
57 return *std::min_element(a.begin(), a.end());
58 }
59 };
60
62 public:
63 explicit MaxBasketPayoff(const ext::shared_ptr<Payoff> &p)
64 : BasketPayoff(p) {}
65 Real accumulate(const Array& a) const override {
66 return *std::max_element(a.begin(), a.end());
67 }
68 };
69
71 public:
72 AverageBasketPayoff(const ext::shared_ptr<Payoff>& p, Array a)
73 : BasketPayoff(p), weights_(std::move(a)) {}
74 AverageBasketPayoff(const ext::shared_ptr<Payoff> &p,
75 Size n)
76 : BasketPayoff(p), weights_(n, 1.0/static_cast<Real>(n)) {}
77 Real accumulate(const Array& a) const override {
78 return std::inner_product(weights_.begin(),
79 weights_.end(),
80 a.begin(), Real(0.0));
81 }
82
83 private:
85 };
86
87
89 public:
90 explicit SpreadBasketPayoff(const ext::shared_ptr<Payoff> &p)
91 : BasketPayoff(p) {}
92 Real accumulate(const Array& a) const override {
93 QL_REQUIRE(a.size() == 2,
94 "payoff is only defined for two underlyings");
95 return a[0]-a[1];
96 }
97 };
98
99 //! Basket option on a number of assets
100 /*! \ingroup instruments */
102 public:
103 class engine;
104 BasketOption(const ext::shared_ptr<BasketPayoff>&,
105 const ext::shared_ptr<Exercise>&);
106 };
107
108 //! %Basket-option %engine base class
110 : public GenericEngine<BasketOption::arguments,
111 BasketOption::results> {};
112
113}
114
115
116#endif
117
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
AverageBasketPayoff(const ext::shared_ptr< Payoff > &p, Array a)
AverageBasketPayoff(const ext::shared_ptr< Payoff > &p, Size n)
Real accumulate(const Array &a) const override
Basket-option engine base class
Basket option on a number of assets.
Real operator()(Real price) const override
~BasketPayoff() override=default
std::string description() const override
ext::shared_ptr< Payoff > basePayoff()
virtual Real operator()(const Array &a) const
std::string name() const override
ext::shared_ptr< Payoff > basePayoff_
virtual Real accumulate(const Array &a) const =0
BasketPayoff(ext::shared_ptr< Payoff > p)
template base class for option pricing engines
MaxBasketPayoff(const ext::shared_ptr< Payoff > &p)
Real accumulate(const Array &a) const override
MinBasketPayoff(const ext::shared_ptr< Payoff > &p)
Real accumulate(const Array &a) const override
Base class for options on multiple assets.
Abstract base class for option payoffs.
Definition: payoff.hpp:36
Real accumulate(const Array &a) const override
SpreadBasketPayoff(const ext::shared_ptr< Payoff > &p)
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Option on multiple assets.
Definition: any.hpp:35
STL namespace.
Payoffs for various options.