QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
choiasianengine.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) 2025 Klaus Spanderen
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/exercise.hpp>
28
29namespace QuantLib {
30
32 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
33 Real lambda,
34 Size maxNrIntegrationSteps)
35 : process_(std::move(process)),
36 lambda_(lambda),
37 maxNrIntegrationSteps_(maxNrIntegrationSteps) {
39 }
40
43 "must be Average::Type Arithmetic ");
44
45 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
46 "not a European Option");
47
48 const ext::shared_ptr<PlainVanillaPayoff> payoff =
49 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
50 QL_REQUIRE(payoff, "non plain vanilla payoff given");
51
52 std::vector<Date> fixingDates = arguments_.fixingDates;
53 std::sort(fixingDates.begin(), fixingDates.end());
54
55 Size futureFixings = fixingDates.size();
56 Size pastFixings = arguments_.pastFixings;
57 Real runningAccumulator = arguments_.runningAccumulator;
58
59 const Date exerciseDate = arguments_.exercise->lastDate();
60 const Handle<YieldTermStructure> rTS = process_->riskFreeRate();
61
62 if ( futureFixings > 0
63 && process_->time(fixingDates.front()) == Time(0)) {
64 // push today fixing to past fixings
65 fixingDates.erase(fixingDates.begin());
66 futureFixings--;
67 pastFixings++;
68 runningAccumulator += process_->x0();
69 }
70
71 if (futureFixings == 0) {
72 QL_REQUIRE(pastFixings > 0, "no past fixings given");
73 results_.value = (*payoff)(runningAccumulator/pastFixings)
74 * rTS->discount(exerciseDate);
75
76 return;
77 }
78
79 QL_REQUIRE(fixingDates.back() <= exerciseDate,
80 "last fixing date must be before exercise date");
81 QL_REQUIRE(process_->time(fixingDates.front()) >= 0.0,
82 "first fixing date is in the past");
83
84 QL_REQUIRE(std::adjacent_find(fixingDates.begin(), fixingDates.end())
85 == fixingDates.end(), "two fixing dates are the same");
86
87 const Real accruedAverage = (pastFixings != 0)
88 ? Real(runningAccumulator / (pastFixings + futureFixings))
89 : 0.0;
90
91 const Real strike = payoff->strike() - accruedAverage;
92 QL_REQUIRE(strike >= 0.0, "effective strike should to be positive");
93
94 const Handle<YieldTermStructure> qTS = process_->dividendYield();
95 const Handle<BlackVolTermStructure> volTS = process_->blackVolatility();
96 const Date volRefDate = volTS->referenceDate();
97 const DayCounter volDc = volTS->dayCounter();
98
99 if (futureFixings > 1) {
100 std::vector<Time> fixingTimes(futureFixings), variances(futureFixings);
101 for (Size i=0; i < futureFixings; ++i) {
102 const Date& fixingDate = fixingDates[i];
103 fixingTimes[i] = volDc.yearFraction(volRefDate, fixingDate);
104 variances[i] = process_->blackVolatility()->blackVariance(fixingDate, strike);
105 }
106
107 Matrix rho(futureFixings, futureFixings);
108 for (Size i=0; i < rho.rows(); ++i)
109 for (Size j=i; j < rho.columns(); ++j)
110 rho[i][j] = rho[j][i] =
111 variances[std::min(i,j)] / std::sqrt(variances[i]*variances[j]);
112
113 const Handle<YieldTermStructure> zeroTS(
114 ext::make_shared<FlatForward>(rTS->referenceDate(), 0.0, rTS->dayCounter())
115 );
116
117 std::vector<ext::shared_ptr<GeneralizedBlackScholesProcess> > processes;
118 processes.reserve(futureFixings);
119 for (Size i=0; i < futureFixings; ++i) {
120 const Date& fixingDate = fixingDates[i];
121 const Volatility sig = volTS->blackVol(fixingDate, payoff->strike())
122 * std::sqrt(fixingTimes[i]/fixingTimes.back());
123
124 processes.emplace_back(
125 ext::make_shared<GeneralizedBlackScholesProcess>(
127 ext::make_shared<SimpleQuote>(
128 process_->x0()*qTS->discount(fixingDate)/rTS->discount(fixingDate)
129 )
130 ),
131 zeroTS, zeroTS,
133 ext::make_shared<BlackConstantVol>(
134 volRefDate, volTS->calendar(),
135 Handle<Quote>(ext::make_shared<SimpleQuote>(sig)),
136 volDc
137 )
138 )
139 )
140 );
141 }
142
143 BasketOption basketOption(
144 ext::make_shared<AverageBasketPayoff>(
145 ext::make_shared<PlainVanillaPayoff>(payoff->optionType(), strike),
146 Array(futureFixings, 1.0/(futureFixings + pastFixings))
147 ),
148 ext::make_shared<EuropeanExercise>(fixingDates.back())
149 );
150 basketOption.setPricingEngine(
151 ext::make_shared<ChoiBasketEngine>(
153 );
154
155 results_.value = basketOption.NPV() * rTS->discount(exerciseDate);
156 }
157 else if (futureFixings == 1) {
159 payoff->optionType(),
160 strike,
161 process_->x0()/(pastFixings + futureFixings)
162 *qTS->discount(fixingDates.back())/rTS->discount(fixingDates.back()),
163 std::sqrt(volTS->blackVariance(fixingDates.back(), strike)),
164 rTS->discount(exerciseDate)
165 );
166 }
167 }
168}
Basket option on a number of assets.
Black constant volatility, no time dependence, no strike dependence.
Black formula.
Black Scholes arithmetic Asian option engine.
Jaehyuk Choi: Sum of all Black-Scholes-Merton Models.
1-D array used in linear algebra.
Definition: array.hpp:52
Basket option on a number of assets.
ChoiAsianEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > p, Real lambda=15, Size maxNrIntegrationSteps=2<< 21)
void calculate() const override
const ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Shared handle to an observable.
Definition: handle.hpp:41
Real NPV() const
returns the net present value of the instrument.
Definition: instrument.hpp:167
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
Matrix used in linear algebra.
Definition: matrix.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
flat forward rate term structure
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Real rho
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:37
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
STL namespace.
simple quote class