QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analytictwoassetcorrelationengine.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::log;
26
27namespace QuantLib {
28
30 ext::shared_ptr<GeneralizedBlackScholesProcess> p1,
31 ext::shared_ptr<GeneralizedBlackScholesProcess> p2,
32 Handle<Quote> correlation)
33 : p1_(std::move(p1)), p2_(std::move(p2)), correlation_(std::move(correlation)) {
37 }
38
41
42 const ext::shared_ptr<PlainVanillaPayoff> payoff =
43 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
44 QL_REQUIRE(payoff, "non-plain payoff given");
45 QL_REQUIRE(payoff->strike()>0.0, "strike must be positive");
46 ext::shared_ptr<Exercise> exercise = arguments_.exercise;
47 Real strike = payoff->strike();//X1
48 Real spot = p1_->x0();
49 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
50
51 Volatility sigma1 =
52 p1_->blackVolatility()->blackVol(p1_->time(exercise->lastDate()),
53 payoff->strike());
54 Volatility sigma2 =
55 p2_->blackVolatility()->blackVol(p2_->time(exercise->lastDate()),
56 payoff->strike());
57
58 Time T = p2_->time(arguments_.exercise->lastDate());
59
60 Real s1=p1_->x0();
61 Real s2=p2_->x0();
62 Rate q1= p1_->dividendYield()->zeroRate(T, Continuous, NoFrequency);
63 Rate q2= p2_->dividendYield()->zeroRate(T, Continuous, NoFrequency);
64 Rate r=p1_->riskFreeRate()->zeroRate(T, Continuous, NoFrequency);
65 Rate b1=r-q1;
66 Rate b2=r-q2;
67 Real rho = correlation_->value();
68
69 Real y1=(log(s1/strike)+(b1-(sigma1*sigma1)/2)*T)/(sigma1*std::sqrt(T));
70 Real y2=(log(s2/arguments_.X2)+(b2-(sigma2*sigma2)/2)*T)/(sigma2*std::sqrt(T));
71
72 switch (payoff->optionType()) {
73 case Option::Call:
74 results_.value=s2*std::exp((b2-r)*T)*M(y2+sigma2*std::sqrt(T),y1+rho*sigma2*std::sqrt(T))-arguments_.X2*std::exp(-r*T)*M(y2,y1);
75 break;
76 case Option::Put:
77 results_.value=arguments_.X2*std::exp(-r*T)*M(-y2,-y1)-s2*std::exp((b2-r)*T)*M(-y2-sigma2*std::sqrt(T),-y1-rho*sigma2*std::sqrt(T));
78 break;
79 default:
80 QL_FAIL("unknown option type");
81 }
82 }
83
84}
const Real correlation_
Analytic engine for two-asset correlation options.
bivariate cumulative normal distribution
ext::shared_ptr< GeneralizedBlackScholesProcess > p1_
AnalyticTwoAssetCorrelationEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > p1, ext::shared_ptr< GeneralizedBlackScholesProcess > p2, Handle< Quote > correlation)
ext::shared_ptr< GeneralizedBlackScholesProcess > p2_
Cumulative bivariate 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.
@ 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.
ext::shared_ptr< YieldTermStructure > r