QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticcomplexchooserengine.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) 2014 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>
23#include <utility>
24
25using std::pow;
26using std::log;
27using std::exp;
28using std::sqrt;
29
30namespace QuantLib {
31
33 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
34 : process_(std::move(process)) {
36 }
37
39 Real S = process_->x0();
40 Real b;
41 Real v;
45 Time Tc = callMaturity() - T;
46 Time Tp = putMaturity() - T;
47
48 Real i = criticalValue();
49
51 v = volatility(T);
52 Real d1 = (log(S / i) + (b + pow(v, 2) / 2)*T) / (v*sqrt(T));
53 Real d2 = d1 - v*sqrt(T);
54
55 b = riskFreeRate(T + Tc) - dividendYield(T + Tc);
56 v = volatility(Tc);
57 Real y1 = (log(S / Xc) + (b + pow(v, 2) / 2)*Tc) / (v*sqrt(Tc));
58
59 b = riskFreeRate(T + Tp) - dividendYield(T + Tp);
60 v = volatility(Tp);
61 Real y2 = (log(S / Xp) + (b + pow(v, 2) / 2)*Tp) / (v*sqrt(Tp));
62
63 Real rho1 = sqrt(T / Tc);
64 Real rho2 = sqrt(T / Tp);
65 b = riskFreeRate(T + Tc) - dividendYield(T + Tc);
66 Real r = riskFreeRate(T + Tc);
67 Real ComplexChooser = S * exp((b - r)*Tc) * BivariateCumulativeNormalDistributionDr78(rho1)(d1, y1)
68 - Xc * exp(-r*Tc)*BivariateCumulativeNormalDistributionDr78(rho1)(d2, y1 - v * sqrt(Tc)) ;
69 b = riskFreeRate(T + Tp) - dividendYield(T + Tp);
70 r = riskFreeRate(T + Tp);
71 ComplexChooser -= S * exp((b - r)*Tp) * BivariateCumulativeNormalDistributionDr78(rho2)(-d1, -y2);
72 ComplexChooser += Xp * exp(-r*Tp) * BivariateCumulativeNormalDistributionDr78(rho2)(-d2, -y2 + v * sqrt(Tp));
73
74 results_.value = ComplexChooser;
75 }
76
78 Real spot, Option::Type optionType) const {
79 Real vol;
80 DiscountFactor growth;
81 DiscountFactor discount;
83
84 // payoff
85 ext::shared_ptr<PlainVanillaPayoff > vanillaPayoff;
86 if (optionType == Option::Call){
87 //TC-T
88 Time t=callMaturity()-2*T;
89 vanillaPayoff = ext::make_shared<PlainVanillaPayoff>(
91 //QuantLib requires sigma * sqrt(t) rather than just sigma/volatility
92 vol = volatility(t) * std::sqrt(t);
93 growth = dividendDiscount(t);
94 discount = riskFreeDiscount(t);
95 } else{
96 Time t=putMaturity()-2*T;
97 vanillaPayoff = ext::make_shared<PlainVanillaPayoff>(
99 vol = volatility(t) * std::sqrt(t);
100 growth = dividendDiscount(t);
101 discount = riskFreeDiscount(t);
102 }
103
104 BlackScholesCalculator bs(vanillaPayoff, spot, growth, vol, discount);
105 return bs;
106 }
107
109 Real Sv = process_->x0();
110
112 Real ci = bs.value();
113 Real dc = bs.delta();
114
116 Real Pi = bs.value();
117 Real dp = bs.delta();
118
119 Real yi = ci - Pi;
120 Real di = dc - dp;
121 Real epsilon = 0.001;
122
123 //Newton-Raphson process
124 while (std::fabs(yi) > epsilon){
125 Sv = Sv - yi / di;
126
128 ci = bs.value();
129 dc = bs.delta();
130
132 Pi = bs.value();
133 dp = bs.delta();
134
135 yi = ci - Pi;
136 di = dc - dp;
137 }
138 return Sv;
139 }
140
141
143 if (optionType == Option::Call)
144 return arguments_.strikeCall;
145 else
146 return arguments_.strikePut;
147 }
148
150 return process_->time(arguments_.choosingDate);
151 }
152
154 return process_->time(arguments_.exercisePut->lastDate());
155 }
156
158 return process_->time(arguments_.exerciseCall->lastDate());
159 }
160
162 return process_->blackVolatility()->blackVol(t, arguments_.strikeCall);
163 }
164
166 return process_->dividendYield()->zeroRate(t, Continuous, NoFrequency);
167 }
168
170 return process_->dividendYield()->discount(t);
171 }
172
174 return process_->riskFreeRate()->zeroRate(t, Continuous, NoFrequency);
175 }
176
178 return process_->riskFreeRate()->discount(t);
179 }
180
181}
bivariate cumulative normal distribution
BlackScholesCalculator bsCalculator(Real spot, Option::Type optionType) const
AnalyticComplexChooserEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Cumulative bivariate normal distribution function.
Black-Scholes 1973 calculator class.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
const DefaultType & t
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 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.
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v
Analytic engine for complex chooser option.