QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmblackscholesmultistrikemesher.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) 2009 Klaus Spanderen
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/*! \file fdmblackscholesmultistrikemesher.cpp
21 \brief 1-d mesher for the Black-Scholes process (in ln(S))
22*/
23
31
32namespace QuantLib {
33
35 Size size,
36 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
37 Time maturity, const std::vector<Real>& strikes,
38 Real eps, Real scaleFactor,
39 const std::pair<Real, Real>& cPoint)
40 : Fdm1dMesher(size) {
41
42 const Real spot = process->x0();
43 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
44
45 const DiscountFactor d = process->dividendYield()->discount(maturity)
46 / process->riskFreeRate()->discount(maturity);
47 const Real minStrike= *std::min_element(strikes.begin(), strikes.end());
48 const Real maxStrike= *std::max_element(strikes.begin(), strikes.end());
49
50 const Real Fmin = spot*spot/maxStrike*d;
51 const Real Fmax = spot*spot/minStrike*d;
52
53 QL_REQUIRE(Fmin > 0.0, "negative forward given");
54
55 // Set the grid boundaries
56 const Real normInvEps = InverseCumulativeNormal()(1-eps);
57 const Real sigmaSqrtTmin
58 = process->blackVolatility()->blackVol(maturity, minStrike)
59 *std::sqrt(maturity);
60 const Real sigmaSqrtTmax
61 = process->blackVolatility()->blackVol(maturity, maxStrike)
62 *std::sqrt(maturity);
63
64 const Real xMin
65 = std::min(0.8*std::log(0.8*spot*spot/maxStrike),
66 std::log(Fmin) - sigmaSqrtTmin*normInvEps*scaleFactor
67 - sigmaSqrtTmin*sigmaSqrtTmin/2.0);
68 const Real xMax
69 = std::max(1.2*std::log(0.8*spot*spot/minStrike),
70 std::log(Fmax) + sigmaSqrtTmax*normInvEps*scaleFactor
71 - sigmaSqrtTmax*sigmaSqrtTmax/2.0);
72
73 ext::shared_ptr<Fdm1dMesher> helper;
74 if ( cPoint.first != Null<Real>()
75 && std::log(cPoint.first) >=xMin && std::log(cPoint.first) <=xMax) {
76
77 helper = ext::shared_ptr<Fdm1dMesher>(
78 new Concentrating1dMesher(xMin, xMax, size,
79 std::pair<Real,Real>(std::log(cPoint.first),cPoint.second)));
80 }
81 else {
82 helper = ext::shared_ptr<Fdm1dMesher>(
83 new Uniform1dMesher(xMin, xMax, size));
84
85 }
86
87 locations_ = helper->locations();
88 for (Size i=0; i < locations_.size(); ++i) {
89 dplus_[i] = helper->dplus(i);
90 dminus_[i] = helper->dminus(i);
91 }
92 }
93}
Black constant volatility, no time dependence, no strike dependence.
Black-Scholes processes.
std::vector< Real > locations_
Definition: fdm1dmesher.hpp:47
std::vector< Real > dplus_
Definition: fdm1dmesher.hpp:48
std::vector< Real > dminus_
Definition: fdm1dmesher.hpp:48
FdmBlackScholesMultiStrikeMesher(Size size, const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Time maturity, const std::vector< Real > &strikes, Real eps=0.0001, Real scaleFactor=1.5, const std::pair< Real, Real > &cPoint=(std::pair< Real, Real >(Null< Real >(), Null< Real >())))
Inverse cumulative normal distribution function.
template class providing a null value for a given type.
Definition: null.hpp:76
One-dimensional grid mesher concentrating around critical points.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
1-d mesher for the Black-Scholes process (in ln(S))
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
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
normal, cumulative and inverse cumulative distributions
One-dimensional simple uniform grid mesher.
Interest-rate term structure.