QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analyticeuropeanmargrabeengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 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>
21#include <ql/pricingengines/exotic/analyticeuropeanmargrabeengine.hpp>
22#include <ql/instruments/payoffs.hpp>
23#include <ql/math/distributions/normaldistribution.hpp>
24#include <utility>
25
26namespace QuantLib {
27
29 ext::shared_ptr<GeneralizedBlackScholesProcess> process1,
30 ext::shared_ptr<GeneralizedBlackScholesProcess> process2,
31 Real correlation)
32 : process1_(std::move(process1)), process2_(std::move(process2)), rho_(correlation) {
35 }
36
38
39 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
40 "not an European Option");
41
42 ext::shared_ptr<EuropeanExercise> exercise =
43 ext::dynamic_pointer_cast<EuropeanExercise>(arguments_.exercise);
44 QL_REQUIRE(exercise, "not an European Option");
45
46 ext::shared_ptr<NullPayoff> payoff =
47 ext::dynamic_pointer_cast<NullPayoff>(arguments_.payoff);
48 QL_REQUIRE(payoff, "non a Null Payoff type");
49
50 Integer quantity1 = arguments_.Q1;
51 Integer quantity2 = arguments_.Q2;
52
53 Real s1 = process1_->stateVariable()->value();
54 Real s2 = process2_->stateVariable()->value();
55
56 Real variance1 = process1_->blackVolatility()->blackVariance(
57 exercise->lastDate(), s1);
58 Real variance2 = process2_->blackVolatility()->blackVariance(
59 exercise->lastDate(), s2);
60
61 DiscountFactor riskFreeDiscount =
62 process1_->riskFreeRate()->discount(exercise->lastDate());
63
64 DiscountFactor dividendDiscount1 =
65 process1_->dividendYield()->discount(exercise->lastDate());
66 DiscountFactor dividendDiscount2 =
67 process2_->dividendYield()->discount(exercise->lastDate());
68
69 Real forward1 = process1_->stateVariable()->value() *
70 dividendDiscount1 / riskFreeDiscount;
71 Real forward2 = process2_->stateVariable()->value() *
72 dividendDiscount2 / riskFreeDiscount;
73
74 Real stdDev1 = std::sqrt(variance1);
75 Real stdDev2 = std::sqrt(variance2);
76 Real variance = variance1 + variance2 - 2*rho_*stdDev1*stdDev2;
77 Real stdDev = std::sqrt(variance);
78 Real d1 = (std::log((quantity1*forward1)/(quantity2*forward2))
79 + 0.5*variance) / stdDev;
80 Real d2 = d1 - stdDev;
81 Real Nd1, Nd2, nd1, nd2;
84 Nd1 = cum(d1);
85 Nd2 = cum(d2);
86 nd1 = norm(d1);
87 nd2 = norm(d2);
88 DayCounter rfdc = process1_->riskFreeRate()->dayCounter();
89 Time t = rfdc.yearFraction(process1_->riskFreeRate()->referenceDate(),
90 arguments_.exercise->lastDate());
91 Real sqt = std::sqrt(t);
92 Real q1 = -std::log(dividendDiscount1)/(sqt*sqt);
93 Real q2 = -std::log(dividendDiscount2)/(sqt*sqt);
94
96 riskFreeDiscount * (quantity1*forward1*Nd1 - quantity2*forward2*Nd2);
97
98 // Greeks
99 results_.delta1 = riskFreeDiscount*(quantity1*forward1*Nd1)/s1;
100 results_.delta2 = -riskFreeDiscount*(quantity2*forward2*Nd2)/s2;
101 results_.gamma1 = (riskFreeDiscount*(quantity1*forward1*nd1)/s1)/(quantity1*s1*stdDev);
102 results_.gamma2 = (-riskFreeDiscount*(quantity2*forward2*nd2)/s2)/(-quantity2*s2*stdDev);
103 Real vega = riskFreeDiscount*(quantity1*forward1*nd1)*sqt;
104 results_.theta = -((stdDev*vega/sqt)/(2*t)-(q1*quantity1*s1*results_.delta1)-(q2*quantity2*s2*results_.delta2));
105 results_.rho = 0.0;
106 }
107
108}
ext::shared_ptr< GeneralizedBlackScholesProcess > process2_
ext::shared_ptr< GeneralizedBlackScholesProcess > process1_
AnalyticEuropeanMargrabeEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process1, ext::shared_ptr< GeneralizedBlackScholesProcess > process2, Real correlation)
Cumulative normal distribution function.
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Normal distribution function.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
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
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
STL namespace.