/src/quantlib/ql/pricingengines/lookback/analyticcontinuouspartialfixedlookback.cpp
Line | Count | Source |
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 | | <https://www.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/analyticcontinuouspartialfixedlookback.hpp> |
23 | | #include <utility> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | AnalyticContinuousPartialFixedLookbackEngine::AnalyticContinuousPartialFixedLookbackEngine( |
28 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process) |
29 | 0 | : process_(std::move(process)) { |
30 | 0 | registerWith(process_); |
31 | 0 | } |
32 | | |
33 | 0 | void AnalyticContinuousPartialFixedLookbackEngine::calculate() const { |
34 | |
|
35 | 0 | ext::shared_ptr<PlainVanillaPayoff> payoff = |
36 | 0 | ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff); |
37 | 0 | QL_REQUIRE(payoff, "Non-plain payoff given"); |
38 | | |
39 | 0 | QL_REQUIRE(process_->x0() > 0.0, "negative or null underlying"); |
40 | | |
41 | 0 | switch (payoff->optionType()) { |
42 | 0 | case Option::Call: |
43 | 0 | QL_REQUIRE(payoff->strike()>=0.0, |
44 | 0 | "Strike must be positive or null"); |
45 | 0 | results_.value = A(1); |
46 | 0 | break; |
47 | 0 | case Option::Put: |
48 | 0 | QL_REQUIRE(payoff->strike()>0.0, |
49 | 0 | "Strike must be positive"); |
50 | 0 | results_.value = A(-1); |
51 | 0 | break; |
52 | 0 | default: |
53 | 0 | QL_FAIL("Unknown type"); |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | |
58 | 0 | Real AnalyticContinuousPartialFixedLookbackEngine::underlying() const { |
59 | 0 | return process_->x0(); |
60 | 0 | } |
61 | | |
62 | 0 | Real AnalyticContinuousPartialFixedLookbackEngine::strike() const { |
63 | 0 | ext::shared_ptr<PlainVanillaPayoff> payoff = |
64 | 0 | ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff); |
65 | 0 | QL_REQUIRE(payoff, "Non-plain payoff given"); |
66 | 0 | return payoff->strike(); |
67 | 0 | } |
68 | | |
69 | 0 | Time AnalyticContinuousPartialFixedLookbackEngine::residualTime() const { |
70 | 0 | return process_->time(arguments_.exercise->lastDate()); |
71 | 0 | } |
72 | | |
73 | 0 | Volatility AnalyticContinuousPartialFixedLookbackEngine::volatility() const { |
74 | 0 | return process_->blackVolatility()->blackVol(residualTime(), strike()); |
75 | 0 | } |
76 | | |
77 | 0 | Real AnalyticContinuousPartialFixedLookbackEngine::stdDeviation() const { |
78 | 0 | return volatility() * std::sqrt(residualTime()); |
79 | 0 | } |
80 | | |
81 | 0 | Rate AnalyticContinuousPartialFixedLookbackEngine::riskFreeRate() const { |
82 | 0 | return process_->riskFreeRate()->zeroRate(residualTime(), Continuous, |
83 | 0 | NoFrequency); |
84 | 0 | } |
85 | | |
86 | | DiscountFactor AnalyticContinuousPartialFixedLookbackEngine::riskFreeDiscount() |
87 | 0 | const { |
88 | 0 | return process_->riskFreeRate()->discount(residualTime()); |
89 | 0 | } |
90 | | |
91 | 0 | Rate AnalyticContinuousPartialFixedLookbackEngine::dividendYield() const { |
92 | 0 | return process_->dividendYield()->zeroRate(residualTime(), |
93 | 0 | Continuous, NoFrequency); |
94 | 0 | } |
95 | | |
96 | | DiscountFactor AnalyticContinuousPartialFixedLookbackEngine::dividendDiscount() |
97 | 0 | const { |
98 | 0 | return process_->dividendYield()->discount(residualTime()); |
99 | 0 | } |
100 | | |
101 | 0 | Time AnalyticContinuousPartialFixedLookbackEngine::lookbackPeriodStartTime() const { |
102 | 0 | return process_->time(arguments_.lookbackPeriodStart); |
103 | 0 | } |
104 | | |
105 | 0 | Real AnalyticContinuousPartialFixedLookbackEngine::A(Real eta) const { |
106 | 0 | bool differentStartOfLookback = lookbackPeriodStartTime() != residualTime(); |
107 | 0 | Real carry = riskFreeRate() - dividendYield(); |
108 | |
|
109 | 0 | Volatility vol = volatility(); |
110 | 0 | Real x = 2.0*carry/(vol*vol); |
111 | 0 | Real s = underlying()/strike(); |
112 | 0 | Real ls = std::log(s); |
113 | 0 | Real d1 = ls/stdDeviation() + 0.5*(x+1.0)*stdDeviation(); |
114 | 0 | Real d2 = d1 - stdDeviation(); |
115 | |
|
116 | 0 | Real e1 = 0, e2 = 0; |
117 | 0 | if (differentStartOfLookback) |
118 | 0 | { |
119 | 0 | e1 = (carry + vol * vol / 2) * (residualTime() - lookbackPeriodStartTime()) / (vol * std::sqrt(residualTime() - lookbackPeriodStartTime())); |
120 | 0 | e2 = e1 - vol * std::sqrt(residualTime() - lookbackPeriodStartTime()); |
121 | 0 | } |
122 | |
|
123 | 0 | Real f1 = (ls + (carry + vol * vol / 2) * lookbackPeriodStartTime()) / (vol * std::sqrt(lookbackPeriodStartTime())); |
124 | 0 | Real f2 = f1 - vol * std::sqrt(lookbackPeriodStartTime()); |
125 | |
|
126 | 0 | Real n1 = f_(eta*d1); |
127 | 0 | Real n2 = f_(eta*d2); |
128 | |
|
129 | 0 | BivariateCumulativeNormalDistributionWe04DP cnbn1(-1), cnbn2(0), cnbn3(0); |
130 | 0 | if (differentStartOfLookback) { |
131 | 0 | cnbn1 = BivariateCumulativeNormalDistributionWe04DP (-std::sqrt(lookbackPeriodStartTime() / residualTime())); |
132 | 0 | cnbn2 = BivariateCumulativeNormalDistributionWe04DP (std::sqrt(1 - lookbackPeriodStartTime() / residualTime())); |
133 | 0 | cnbn3 = BivariateCumulativeNormalDistributionWe04DP (-std::sqrt(1 - lookbackPeriodStartTime() / residualTime())); |
134 | 0 | } |
135 | |
|
136 | 0 | Real n3 = cnbn1(eta*(d1-x*stdDeviation()), eta*(-f1+2.0* carry * std::sqrt(lookbackPeriodStartTime()) / vol)); |
137 | 0 | Real n4 = cnbn2(eta*e1, eta*d1); |
138 | 0 | Real n5 = cnbn3(-eta*e1, eta*d1); |
139 | 0 | Real n6 = cnbn1(eta*f2, -eta*d2); |
140 | 0 | Real n7 = f_(eta*f1); |
141 | 0 | Real n8 = f_(-eta*e2); |
142 | |
|
143 | 0 | Real pow_s = std::pow(s, -x); |
144 | 0 | Real carryDiscount = std::exp(-carry * (residualTime() - lookbackPeriodStartTime())); |
145 | 0 | return eta*(underlying() * dividendDiscount() * n1 |
146 | 0 | - strike() * riskFreeDiscount() * n2 |
147 | 0 | + underlying() * riskFreeDiscount() / x |
148 | 0 | * (-pow_s * n3 + dividendDiscount() / riskFreeDiscount() * n4) |
149 | 0 | - underlying() * dividendDiscount() * n5 |
150 | 0 | - strike() * riskFreeDiscount() * n6 |
151 | 0 | + carryDiscount * dividendDiscount() |
152 | 0 | * (1 - 0.5 * vol * vol / carry) * |
153 | 0 | underlying() * n7 * n8); |
154 | 0 | } |
155 | | } |
156 | | |