QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticeuropeanvasicekengine.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) 2020 Lew Wei Hao
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
28 namespace {
29
30 Real g_k(Real t, Real kappa){
31 return (1 - std::exp(- kappa * t )) / kappa;
32 }
33
34 class integrand_vasicek {
35 private:
39 const Real kappa_;
40 const Real T_;
41 public:
42 integrand_vasicek(Real sigma_s, Real sigma_r, Real correlation, Real kappa, Real T)
43 : sigma_s_(sigma_s), sigma_r_(sigma_r), correlation_(correlation), kappa_(kappa), T_(T){}
44 Real operator()(Real u) const {
45 Real g = g_k(T_ - u, kappa_);
46 return (sigma_s_ * sigma_s_) + (2 * correlation_ * sigma_s_ * sigma_r_ * g) + (sigma_r_ * sigma_r_ * g * g);
47 }
48 };
49
50 }
51
53 ext::shared_ptr<GeneralizedBlackScholesProcess> blackProcess,
54 ext::shared_ptr<Vasicek> vasicekProcess,
55 Real correlation)
56 : blackProcess_(std::move(blackProcess)), vasicekProcess_(std::move(vasicekProcess)),
57 simpsonIntegral_(new SimpsonIntegral(1e-5, 1000)), correlation_(correlation) {
58 registerWith(blackProcess_);
59 registerWith(vasicekProcess_);
60 }
61
63 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
64 "not an European option");
65
66 ext::shared_ptr<StrikedTypePayoff> payoff =
67 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
68
69 QL_REQUIRE(payoff, "non-striked payoff given");
70
72
73 Real t = 0;
74 Real T = blackProcess_->riskFreeRate()->dayCounter().yearFraction(blackProcess_->riskFreeRate().currentLink()->referenceDate(),arguments_.exercise->lastDate());
76 Real S_t = blackProcess_->x0();
77 Real K = payoff->strike();
78 Real sigma_s = blackProcess_->blackVolatility()->blackVol(t, K);
79 Real sigma_r = vasicekProcess_->sigma();
80 Real r_t = vasicekProcess_->r0();
81
82 Real zcb = vasicekProcess_->discountBond(t, T, r_t);
83 Real epsilon = payoff->optionType() == Option::Call ? 1 : -1;
84 Real upsilon = (*simpsonIntegral_)(integrand_vasicek(sigma_s, sigma_r, correlation_, kappa, T), t, T);
85 Real d_positive = (std::log((S_t / K) / zcb) + upsilon / 2) / std::sqrt(upsilon);
86 Real d_negative = (std::log((S_t / K) / zcb) - upsilon / 2) / std::sqrt(upsilon);
87 Real n_d1 = f(epsilon * d_positive);
88 Real n_d2 = f(epsilon * d_negative);
89
90 results_.value = epsilon * ((S_t * n_d1) - (zcb * K * n_d2));
91 }
92
93}
94
const Real sigma_r_
const Real kappa_
const Real sigma_s_
const Real correlation_
const Instrument::results * results_
Definition: cdsoption.cpp:63
ext::shared_ptr< GeneralizedBlackScholesProcess > blackProcess_
AnalyticBlackVasicekEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >, ext::shared_ptr< Vasicek >, Real correlation)
Cumulative normal distribution function.
Integral of a one-dimensional function.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
QL_REAL Real
real number
Definition: types.hpp:50
Real kappa
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.
normal, cumulative and inverse cumulative distributions
integral of a one-dimensional function using Simpson formula