QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analyticcontinuouspartialfloatinglookback.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Warren Chou
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/exercise.hpp>
22#include <ql/pricingengines/lookback/analyticcontinuouspartialfloatinglookback.hpp>
23#include <utility>
24
25namespace QuantLib {
26
29 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
30 : process_(std::move(process)) {
32 }
33
35
36 ext::shared_ptr<FloatingTypePayoff> payoff =
37 ext::dynamic_pointer_cast<FloatingTypePayoff>(arguments_.payoff);
38 QL_REQUIRE(payoff, "Non-floating payoff given");
39
40 QL_REQUIRE(process_->x0() > 0.0, "negative or null underlying");
41
42 switch (payoff->optionType()) {
43 case Option::Call:
44 results_.value = A(1);
45 break;
46 case Option::Put:
47 results_.value = A(-1);
48 break;
49 default:
50 QL_FAIL("Unknown type");
51 }
52 }
53
55 return process_->x0();
56 }
57
59 return process_->time(arguments_.exercise->lastDate());
60 }
61
63 return process_->blackVolatility()->blackVol(residualTime(), minmax());
64 }
65
67 return volatility() * std::sqrt(residualTime());
68 }
69
71 return process_->riskFreeRate()->zeroRate(residualTime(), Continuous,
73 }
74
76 const {
77 return process_->riskFreeRate()->discount(residualTime());
78 }
79
81 return process_->dividendYield()->zeroRate(residualTime(),
83 }
84
86 const {
87 return process_->dividendYield()->discount(residualTime());
88 }
89
91 return arguments_.minmax;
92 }
93
95 return arguments_.lambda;
96 }
97
100 }
101
102
104 bool fullLookbackPeriod = lookbackPeriodEndTime() == residualTime();
105 Real carry = riskFreeRate() - dividendYield();
106 Volatility vol = volatility();
107 Real x = 2.0*carry/(vol*vol);
108 Real s = underlying()/minmax();
109
110 Real ls = std::log(s);
111 Real d1 = ls/stdDeviation() + 0.5*(x+1.0)*stdDeviation();
112 Real d2 = d1 - stdDeviation();
113
114 Real e1 = 0, e2 = 0;
115 if (!fullLookbackPeriod)
116 {
117 e1 = (carry + vol * vol / 2) * (residualTime() - lookbackPeriodEndTime()) / (vol * std::sqrt(residualTime() - lookbackPeriodEndTime()));
118 e2 = e1 - vol * std::sqrt(residualTime() - lookbackPeriodEndTime());
119 }
120
121 Real f1 = (ls + (carry + vol * vol / 2) * lookbackPeriodEndTime()) / (vol * std::sqrt(lookbackPeriodEndTime()));
122 Real f2 = f1 - vol * std::sqrt(lookbackPeriodEndTime());
123
124 Real l1 = std::log(lambda()) / vol;
125 Real g1 = l1 / std::sqrt(residualTime());
126
127 Real n1 = f_(eta*(d1 - g1));
128 Real n2 = f_(eta*(d2 - g1));
129
130 BivariateCumulativeNormalDistributionWe04DP cnbn1(1), cnbn2(0), cnbn3(-1);
131 if (!fullLookbackPeriod) {
135 }
136
137 Real n3 = cnbn1(eta*(-f1+2.0* carry * std::sqrt(lookbackPeriodEndTime()) / vol), eta*(-d1+x*stdDeviation()-g1));
138 Real n4 = 0, n5 = 0, n6 = 0, n7 = 0;
139 if (!fullLookbackPeriod)
140 {
141 Real g2 = l1 / std::sqrt(residualTime() - lookbackPeriodEndTime());
142 n4 = cnbn2(-eta*(d1+g1), eta*(e1 + g2));
143 n5 = cnbn2(-eta*(d1-g1), eta*(e1 - g2));
144 n6 = cnbn3(eta*-f2, eta*(d2 - g1));
145 n7 = f_(eta*(e2 - g2));
146 }
147 else
148 {
149 n4 = f_(-eta*(d1+g1));
150 }
151
152 Real n8 = f_(-eta*f1);
153 Real pow_s = std::pow(s, -x);
154 Real pow_l = std::pow(lambda(), x);
155
156 if (!fullLookbackPeriod)
157 {
158 return eta*(underlying() * dividendDiscount() * n1 -
159 lambda() * minmax() * riskFreeDiscount() * n2 +
160 underlying() * riskFreeDiscount() * lambda() / x *
161 (pow_s * n3 - dividendDiscount() / riskFreeDiscount() * pow_l * n4)
162 + underlying() * dividendDiscount() * n5 +
163 riskFreeDiscount() * lambda() * minmax() * n6 -
164 std::exp(-carry * (residualTime() - lookbackPeriodEndTime())) *
165 dividendDiscount() * (1 + 0.5 * vol * vol / carry) * lambda() *
166 underlying() * n7 * n8);
167 }
168 else
169 {
170 //Simpler calculation
171 return eta*(underlying() * dividendDiscount() * n1 -
172 lambda() * minmax() * riskFreeDiscount() * n2 +
173 underlying() * riskFreeDiscount() * lambda() / x *
174 (pow_s * n3 - dividendDiscount() / riskFreeDiscount() * pow_l * n4));
175 }
176 }
177}
178
AnalyticContinuousPartialFloatingLookbackEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
Cumulative bivariate normal distibution function (West 2004)
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
STL namespace.