QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analytictwoassetbarrierengine.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) 2012 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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>
24#include <utility>
25
26namespace QuantLib {
27
29 ext::shared_ptr<GeneralizedBlackScholesProcess> process1,
30 ext::shared_ptr<GeneralizedBlackScholesProcess> process2,
32 : process1_(std::move(process1)), process2_(std::move(process2)), rho_(std::move(rho)) {
36 }
37
39 ext::shared_ptr<PlainVanillaPayoff> payoff =
40 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
41 QL_REQUIRE(payoff, "non-plain payoff given");
42 QL_REQUIRE(payoff->strike()>0.0,"strike must be positive");
43
44 Real spot2 = process2_->x0();
45 // option is triggered by S2
46 QL_REQUIRE(spot2 > 0.0, "negative or null underlying given");
47 QL_REQUIRE(!triggered(spot2), "barrier touched");
48
50
51 switch (payoff->optionType()) {
52 case Option::Call:
53 switch (barrierType) {
55 results_.value = A(1,-1) +B(1,-1) ;
56 break;
57 case Barrier::UpOut:
58 results_.value = A(1,1) + B(1,1) ;
59 break;
60 case Barrier::DownIn:
61 results_.value = call()-(A(1,-1) +B(1,-1) );
62 break;
63 case Barrier::UpIn:
64 results_.value = call()-(A(1,1) +B(1,1));
65 break;
66 }
67 break;
68 case Option::Put:
69 switch (barrierType) {
71 results_.value = A(-1,-1)+B(-1,-1) ;
72 break;
73 case Barrier::UpOut:
74 results_.value = A(-1,1)+B(-1,1) ;
75 break;
76 case Barrier::DownIn:
77 results_.value = put()-(A(-1,-1) +B(-1,-1) );
78 break;
79 case Barrier::UpIn:
80 results_.value = put()-(A(-1,1) +B(-1,1) );
81 break;
82 }
83 break;
84 default:
85 QL_FAIL("unknown type");
86 }
87 }
88
90 return process1_->x0();
91 }
92
94 return process2_->x0();
95 }
96
98 ext::shared_ptr<PlainVanillaPayoff> payoff =
99 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
100 QL_REQUIRE(payoff, "non-plain payoff given");
101 return payoff->strike();
102 }
103
105 return process1_->time(arguments_.exercise->lastDate());
106 }
107
109 return process1_->blackVolatility()->blackVol(residualTime(), strike());
110 }
111
113 return process2_->blackVolatility()->blackVol(residualTime(), strike());
114 }
115
117 return arguments_.barrier;
118 }
119
121 return rho_->value();
122 }
123
125 return process1_->riskFreeRate()->zeroRate(residualTime(),
127 }
128
129
131 return process1_->dividendYield()->zeroRate(residualTime(),
133 }
134
136 return process2_->dividendYield()->zeroRate(residualTime(),
138 }
139
141 return riskFreeRate() - dividendYield1();
142 }
143
145 return riskFreeRate() - dividendYield2();
146 }
147
150 (volatility1()*std::sqrt(residualTime()));
151 }
152
154 return d1() - volatility1()*std::sqrt(residualTime());
155 }
156
158 return d1()+ (2*rho()*std::log(barrier()/underlying2()))/(volatility2()*std::sqrt(residualTime()));
159 }
160
162 return d2()+ (2*rho()*std::log(barrier()/underlying2()))/(volatility2()*std::sqrt(residualTime()));
163 }
164
167 (volatility2()*std::sqrt(residualTime()));
168 }
169
171 return e1()+rho()*volatility1()*std::sqrt(residualTime());
172 }
173
175 return e1()-(2*std::log(barrier()/underlying2()))/(volatility2()*std::sqrt(residualTime()));
176 }
177
179 return e2()-(2*std::log(barrier()/underlying2()))/(volatility2()*std::sqrt(residualTime()));
180 }
181
183 return b-(vol*vol)/2;
184 }
185
188 return underlying1()*nd(d1())-strike()*std::exp(-riskFreeRate()*residualTime())*nd(d2());
189 }
190
193 return strike()*std::exp(-riskFreeRate()*residualTime())*nd(-d2())-underlying1()*nd(-d1());
194 }
195
197 Real S1 = underlying1(), S2 = underlying2();
198 Rate b1 = costOfCarry1(), b2 = costOfCarry2();
199 Rate r = riskFreeRate();
200 Time T = residualTime();
201 Real H = barrier(), X = strike();
202 Volatility sigma1 = volatility1(), sigma2 = volatility2();
203 Real rho = rho_->value();
204
205 Rate mu1 = b1 - sigma1*sigma1/2.0;
206 Rate mu2 = b2 - sigma2*sigma2/2.0;
207
208 Real d1 = (std::log(S1/X)+(mu1+sigma1*sigma1)*T)/
209 (sigma1*std::sqrt(T));
210 Real d2 = d1 - sigma1*std::sqrt(T);
211 Real d3 = d1 + (2*rho*std::log(H/S2))/(sigma2*std::sqrt(T));
212 Real d4 = d2 + (2*rho*std::log(H/S2))/(sigma2*std::sqrt(T));
213
214 Real e1 = (std::log(H/S2)-(mu2+rho*sigma1*sigma2)*T)/
215 (sigma2*std::sqrt(T));
216 Real e2 = e1 + rho*sigma1*std::sqrt(T);
217 Real e3 = e1 - (2*std::log(H/S2))/(sigma2*std::sqrt(T));
218 Real e4 = e2 - (2*std::log(H/S2))/(sigma2*std::sqrt(T));
219
220 Real w =
221 eta*S1*std::exp((b1-r)*T) *
222 (M(eta*d1, phi*e1,-eta*phi*rho)
223 -std::exp((2*(mu2+rho*sigma1*sigma2)*std::log(H/S2))/(sigma2*sigma2))
224 *M(eta*d3, phi*e3, -eta*phi*rho))
225
226 - eta*X*std::exp(-r*T) *
227 (M(eta*d2, phi*e2, -eta*phi*rho)
228 -std::exp((2*mu2*std::log(H/S2))/(sigma2*sigma2))*
229 M(eta*d4, phi*e4, -eta*phi*rho) ) ;
230
231 return w;
232 }
233
235 return 0.0;
236 }
237
240 return f(m_a, m_b);
241 }
242
243}
244
Analytic engine for barrier option on two assets.
bivariate cumulative normal distribution
ext::shared_ptr< GeneralizedBlackScholesProcess > process2_
Real M(Real m_a, Real m_b, Real rho) const
ext::shared_ptr< GeneralizedBlackScholesProcess > process1_
AnalyticTwoAssetBarrierEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process1, ext::shared_ptr< GeneralizedBlackScholesProcess > process2, Handle< Quote > rho)
Cumulative bivariate normal distribution function.
Cumulative normal distribution function.
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Option exercise classes and payoff function.
ext::function< Real(Real)> b
@ 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 Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Real rho
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.
normal, cumulative and inverse cumulative distributions
ext::shared_ptr< YieldTermStructure > r