/src/quantlib/ql/pricingengines/exotic/analyticcompoundoptionengine.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2009 Dimitri Reiswich |
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 | | <https://www.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/pricingengines/exotic/analyticcompoundoptionengine.hpp> |
21 | | #include <ql/math/solvers1d/brent.hpp> |
22 | | #include <ql/pricingengines/blackformula.hpp> |
23 | | #include <utility> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | namespace { |
28 | | |
29 | | // Helper Class needed to solve an implicit problem of finding a |
30 | | // spot to a corresponding option price. |
31 | | class ImpliedSpotHelper { |
32 | | public: |
33 | | ImpliedSpotHelper(DiscountFactor dividendDiscount, |
34 | | DiscountFactor riskFreeDiscount, |
35 | | Real standardDeviation, |
36 | | ext::shared_ptr<PlainVanillaPayoff> payoff, |
37 | | Real strike) |
38 | 0 | : dividendDiscount_(dividendDiscount), riskFreeDiscount_(riskFreeDiscount), |
39 | 0 | standardDeviation_(standardDeviation), strike_(strike), payoff_(std::move(payoff)) {} |
40 | 0 | Real operator()(Real spot) const { |
41 | 0 | Real forwardPrice = spot*dividendDiscount_/riskFreeDiscount_; |
42 | 0 | Real value = blackFormula(payoff_, forwardPrice, |
43 | 0 | standardDeviation_,riskFreeDiscount_); |
44 | 0 | return value - strike_; |
45 | 0 | } |
46 | | private: |
47 | | DiscountFactor dividendDiscount_; |
48 | | DiscountFactor riskFreeDiscount_; |
49 | | Real standardDeviation_; |
50 | | Real strike_; |
51 | | ext::shared_ptr<PlainVanillaPayoff> payoff_; |
52 | | }; |
53 | | |
54 | | } |
55 | | |
56 | | AnalyticCompoundOptionEngine::AnalyticCompoundOptionEngine( |
57 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process) |
58 | 0 | : process_(std::move(process)) { |
59 | 0 | registerWith(process_); |
60 | 0 | } |
61 | | |
62 | 0 | void AnalyticCompoundOptionEngine::calculate() const { |
63 | |
|
64 | 0 | QL_REQUIRE(strikeDaughter()>0.0, |
65 | 0 | "Daughter strike must be positive"); |
66 | | |
67 | 0 | QL_REQUIRE(strikeMother()>0.0, |
68 | 0 | "Mother strike must be positive"); |
69 | | |
70 | 0 | QL_REQUIRE(spot() > 0.0, "negative or null underlying given"); |
71 | | |
72 | | /* Solver Setup ***************************************************/ |
73 | 0 | Date helpDate(process_->riskFreeRate()->referenceDate()); |
74 | 0 | Date helpMaturity=helpDate+(maturityDaughter()-maturityMother())*Days; |
75 | 0 | Real vol =process_->blackVolatility()->blackVol(helpMaturity, |
76 | 0 | strikeDaughter()); |
77 | |
|
78 | 0 | Time helpTimeToMat=process_->time(helpMaturity); |
79 | 0 | vol=vol*std::sqrt(helpTimeToMat); |
80 | |
|
81 | 0 | DiscountFactor dividendDiscount = |
82 | 0 | process_->dividendYield()->discount(helpMaturity); |
83 | |
|
84 | 0 | DiscountFactor riskFreeDiscount = |
85 | 0 | process_->riskFreeRate()->discount(helpMaturity); |
86 | | |
87 | |
|
88 | 0 | auto f = ext::make_shared<ImpliedSpotHelper>(dividendDiscount, riskFreeDiscount, |
89 | 0 | vol, payoffDaughter(), strikeMother()); |
90 | |
|
91 | 0 | Brent solver; |
92 | 0 | solver.setMaxEvaluations(1000); |
93 | 0 | Real accuracy = 1.0e-6; |
94 | |
|
95 | 0 | Real sSolved=solver.solve(*f, accuracy, strikeDaughter(), 1.0e-6, strikeDaughter()*1000.0); |
96 | 0 | Real X=transformX(sSolved); // transform stock to return as in Wystup's book |
97 | | /* Solver Setup Finished*****************************************/ |
98 | |
|
99 | 0 | Real phi=typeDaughter(); // -1 or 1 |
100 | 0 | Real w=typeMother(); // -1 or 1 |
101 | |
|
102 | 0 | Real rho=std::sqrt(residualTimeMother()/residualTimeDaughter()); |
103 | 0 | BivariateCumulativeNormalDistributionDr78 N2(w*rho) ; |
104 | |
|
105 | 0 | DiscountFactor ddD=dividendDiscountDaughter(); |
106 | 0 | DiscountFactor rdD=riskFreeDiscountDaughter(); |
107 | | //DiscountFactor ddM=dividendDiscountMother(); |
108 | 0 | DiscountFactor rdM=riskFreeDiscountMother(); |
109 | |
|
110 | 0 | Real XmSM=X-stdDeviationMother(); |
111 | 0 | Real S=spot(); |
112 | 0 | Real dP=dPlus(); |
113 | 0 | Real dPT12=dPlusTau12(sSolved); |
114 | 0 | Real vD=volatilityDaughter(); |
115 | |
|
116 | 0 | Real dM=dMinus(); |
117 | 0 | Real strD=strikeDaughter(); |
118 | 0 | Real strM=strikeMother(); |
119 | 0 | Real rTM=residualTimeMother(); |
120 | 0 | Real rTD=residualTimeDaughter(); |
121 | |
|
122 | 0 | Real rD=riskFreeRateDaughter(); |
123 | 0 | Real dD=dividendRateDaughter(); |
124 | |
|
125 | 0 | Real N2XmSM=N2(-phi*w*XmSM,phi*dP); |
126 | 0 | Real N2X=N2(-phi*w*X,phi*dM); |
127 | 0 | Real NeX=N_(-phi*w*e(X)); |
128 | 0 | Real NX=N_(-phi*w*X); |
129 | 0 | Real NT12=N_(phi*dPT12); |
130 | 0 | Real ndP=n_(dP); |
131 | 0 | Real nXm=n_(XmSM); |
132 | 0 | Real invMTime=1/std::sqrt(rTM); |
133 | 0 | Real invDTime=1/std::sqrt(rTD); |
134 | |
|
135 | 0 | Real tempRes=phi*w*S*ddD*N2XmSM-phi*w*strD*rdD*N2X-w*strM*rdM*NX; |
136 | 0 | Real tempDelta=phi*w*ddD*N2XmSM; |
137 | 0 | Real tempGamma=(ddD/(vD*S))*(invMTime*nXm*NT12+w*invDTime*ndP*NeX); |
138 | 0 | Real tempVega=ddD*S*((1/invMTime)*nXm*NT12+w*(1/invDTime)*ndP*NeX); |
139 | 0 | Real tempTheta=phi*w*dD*S*ddD*N2XmSM-phi*w*rD*strD*rdD*N2X-w*rD*strM*rdM*NX; |
140 | 0 | tempTheta-=0.5*vD*S*ddD*(invMTime*nXm*NT12+w*invDTime*ndP*NeX); |
141 | |
|
142 | 0 | results_.value=tempRes; |
143 | 0 | results_.delta=tempDelta; |
144 | 0 | results_.gamma=tempGamma; |
145 | 0 | results_.vega=tempVega; |
146 | 0 | results_.theta=tempTheta; |
147 | 0 | } |
148 | | |
149 | 0 | Real AnalyticCompoundOptionEngine::typeDaughter() const { |
150 | | // returns -1 or 1 according to put or call |
151 | 0 | return (Real) payoffDaughter()->optionType(); |
152 | 0 | } |
153 | | |
154 | 0 | Real AnalyticCompoundOptionEngine::typeMother() const { |
155 | 0 | return (Real) payoffMother()->optionType(); |
156 | 0 | } |
157 | | |
158 | 0 | Date AnalyticCompoundOptionEngine::maturityDaughter() const { |
159 | 0 | return arguments_.daughterExercise->lastDate(); |
160 | 0 | } |
161 | | |
162 | 0 | Date AnalyticCompoundOptionEngine::maturityMother() const { |
163 | 0 | return arguments_.exercise->lastDate(); |
164 | 0 | } |
165 | | |
166 | 0 | Time AnalyticCompoundOptionEngine::residualTimeDaughter() const { |
167 | 0 | return process_->time(maturityDaughter()); |
168 | 0 | } |
169 | | |
170 | 0 | Time AnalyticCompoundOptionEngine::residualTimeMother() const { |
171 | 0 | return process_->time(maturityMother()); |
172 | 0 | } |
173 | | |
174 | 0 | Time AnalyticCompoundOptionEngine::residualTimeMotherDaughter() const { |
175 | 0 | return residualTimeDaughter()-residualTimeMother(); |
176 | 0 | } |
177 | | |
178 | | |
179 | 0 | Real AnalyticCompoundOptionEngine::volatilityDaughter() const { |
180 | 0 | return process_->blackVolatility()->blackVol(maturityDaughter(), |
181 | 0 | strikeDaughter()); |
182 | 0 | } |
183 | | |
184 | | |
185 | 0 | Real AnalyticCompoundOptionEngine::volatilityMother() const { |
186 | 0 | return process_->blackVolatility()->blackVol(maturityMother(), |
187 | 0 | strikeMother()); |
188 | 0 | } |
189 | | |
190 | 0 | Real AnalyticCompoundOptionEngine::stdDeviationDaughter() const { |
191 | 0 | return volatilityDaughter()*std::sqrt(residualTimeDaughter()); |
192 | 0 | } |
193 | | |
194 | 0 | Real AnalyticCompoundOptionEngine::stdDeviationMother() const { |
195 | 0 | return volatilityMother()*std::sqrt(residualTimeMother()); |
196 | 0 | } |
197 | | |
198 | | |
199 | | ext::shared_ptr<PlainVanillaPayoff> |
200 | 0 | AnalyticCompoundOptionEngine::payoffDaughter() const { |
201 | 0 | ext::shared_ptr<PlainVanillaPayoff> dPayoff = |
202 | 0 | ext::dynamic_pointer_cast<PlainVanillaPayoff>( |
203 | 0 | arguments_.daughterPayoff); |
204 | 0 | QL_REQUIRE(dPayoff, "non-plain payoff given"); |
205 | 0 | return dPayoff; |
206 | 0 | } |
207 | | |
208 | | ext::shared_ptr<PlainVanillaPayoff> |
209 | 0 | AnalyticCompoundOptionEngine::payoffMother() const { |
210 | 0 | ext::shared_ptr<PlainVanillaPayoff> mPayoff = |
211 | 0 | ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff); |
212 | 0 | QL_REQUIRE(mPayoff, "non-plain payoff given"); |
213 | 0 | return mPayoff; |
214 | 0 | } |
215 | | |
216 | 0 | Real AnalyticCompoundOptionEngine::strikeMother() const { |
217 | 0 | return payoffMother()->strike(); |
218 | 0 | } |
219 | | |
220 | 0 | Real AnalyticCompoundOptionEngine::strikeDaughter() const { |
221 | 0 | return payoffDaughter()->strike(); |
222 | 0 | } |
223 | | |
224 | 0 | DiscountFactor AnalyticCompoundOptionEngine::riskFreeDiscountDaughter() const { |
225 | 0 | return process_->riskFreeRate()->discount(residualTimeDaughter()); |
226 | 0 | } |
227 | | |
228 | 0 | DiscountFactor AnalyticCompoundOptionEngine::riskFreeDiscountMother() const { |
229 | 0 | return process_->riskFreeRate()->discount(residualTimeMother()); |
230 | 0 | } |
231 | | |
232 | 0 | DiscountFactor AnalyticCompoundOptionEngine::riskFreeDiscountMotherDaughter() const { |
233 | 0 | return process_->riskFreeRate()->discount(residualTimeMotherDaughter()); |
234 | 0 | } |
235 | | |
236 | 0 | DiscountFactor AnalyticCompoundOptionEngine::dividendDiscountDaughter() const { |
237 | 0 | return process_->dividendYield()->discount(residualTimeDaughter()); |
238 | 0 | } |
239 | | |
240 | 0 | DiscountFactor AnalyticCompoundOptionEngine::dividendDiscountMother() const { |
241 | 0 | return process_->dividendYield()->discount(residualTimeMother()); |
242 | 0 | } |
243 | | |
244 | 0 | DiscountFactor AnalyticCompoundOptionEngine::dividendDiscountMotherDaughter() const { |
245 | 0 | return process_->dividendYield()->discount(residualTimeMotherDaughter()); |
246 | 0 | } |
247 | | |
248 | 0 | Real AnalyticCompoundOptionEngine::dPlus() const { |
249 | 0 | Real forward = spot() * dividendDiscountDaughter() / riskFreeDiscountDaughter(); |
250 | 0 | Real sd=stdDeviationDaughter(); |
251 | 0 | return std::log(forward/strikeDaughter())/sd+0.5*sd; |
252 | 0 | } |
253 | | |
254 | 0 | Real AnalyticCompoundOptionEngine::dMinus() const { |
255 | 0 | return dPlus()-stdDeviationDaughter(); |
256 | 0 | } |
257 | | |
258 | 0 | Real AnalyticCompoundOptionEngine::dPlusTau12(Real S) const { |
259 | 0 | Real forward = S * dividendDiscountMotherDaughter() / riskFreeDiscountMotherDaughter(); |
260 | 0 | Real sd=volatilityDaughter()*std::sqrt(residualTimeMotherDaughter()); |
261 | 0 | return std::log(forward/strikeDaughter())/sd+0.5*sd; |
262 | 0 | } |
263 | | |
264 | 0 | Real AnalyticCompoundOptionEngine::spot() const { |
265 | 0 | return process_->x0(); |
266 | 0 | } |
267 | | |
268 | 0 | Real AnalyticCompoundOptionEngine::riskFreeRateDaughter() const { |
269 | 0 | return process_->riskFreeRate()->zeroRate(residualTimeDaughter(), |
270 | 0 | Continuous, |
271 | 0 | NoFrequency); |
272 | 0 | } |
273 | | |
274 | 0 | Real AnalyticCompoundOptionEngine::dividendRateDaughter() const { |
275 | 0 | return process_->dividendYield()->zeroRate(residualTimeDaughter(), |
276 | 0 | Continuous, |
277 | 0 | NoFrequency); |
278 | 0 | } |
279 | | |
280 | 0 | Real AnalyticCompoundOptionEngine::transformX(Real X) const { |
281 | |
|
282 | 0 | Real sd=stdDeviationMother(); |
283 | 0 | Real resX=riskFreeDiscountMother()*X/(spot()*dividendDiscountMother()); |
284 | 0 | resX=resX*std::exp(0.5*sd*sd); |
285 | 0 | resX=std::log(resX); |
286 | |
|
287 | 0 | return resX/sd; |
288 | 0 | } |
289 | | |
290 | 0 | Real AnalyticCompoundOptionEngine::e(Real X) const { |
291 | 0 | Real rtM=residualTimeMother(); |
292 | 0 | Real rtD=residualTimeDaughter(); |
293 | |
|
294 | 0 | return (X*std::sqrt(rtD)+std::sqrt(rtM)*dMinus())/std::sqrt(rtD-rtM); |
295 | 0 | } |
296 | | |
297 | | } |