Coverage Report

Created: 2025-11-04 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/meshers/fdmblackscholesmultistrikemesher.cpp
Line
Count
Source
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
 <https://www.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
24
#include <ql/processes/blackscholesprocess.hpp>
25
#include <ql/termstructures/yieldtermstructure.hpp>
26
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
27
#include <ql/math/distributions/normaldistribution.hpp>
28
#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
29
#include <ql/methods/finitedifferences/meshers/concentrating1dmesher.hpp>
30
#include <ql/methods/finitedifferences/meshers/fdmblackscholesmultistrikemesher.hpp>
31
32
namespace QuantLib {
33
34
    FdmBlackScholesMultiStrikeMesher::FdmBlackScholesMultiStrikeMesher(
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
0
    : Fdm1dMesher(size) {
41
42
0
        const Real spot = process->x0();
43
0
        QL_REQUIRE(spot > 0.0, "negative or null underlying given");
44
45
0
        const DiscountFactor d =   process->dividendYield()->discount(maturity)
46
0
                                 / process->riskFreeRate()->discount(maturity);
47
0
        const Real minStrike= *std::min_element(strikes.begin(), strikes.end());
48
0
        const Real maxStrike= *std::max_element(strikes.begin(), strikes.end());
49
                
50
0
        const Real Fmin = spot*spot/maxStrike*d;
51
0
        const Real Fmax = spot*spot/minStrike*d;
52
        
53
0
        QL_REQUIRE(Fmin > 0.0, "negative forward given");
54
55
        // Set the grid boundaries
56
0
        const Real normInvEps = InverseCumulativeNormal()(1-eps);
57
0
        const Real sigmaSqrtTmin 
58
0
            = process->blackVolatility()->blackVol(maturity, minStrike)
59
0
                                                        *std::sqrt(maturity);
60
0
        const Real sigmaSqrtTmax 
61
0
            = process->blackVolatility()->blackVol(maturity, maxStrike)
62
0
                                                        *std::sqrt(maturity);
63
        
64
0
        const Real xMin
65
0
            = std::min(0.8*std::log(0.8*spot*spot/maxStrike),
66
0
                       std::log(Fmin) - sigmaSqrtTmin*normInvEps*scaleFactor
67
0
                                      - sigmaSqrtTmin*sigmaSqrtTmin/2.0);
68
0
        const Real xMax
69
0
            = std::max(1.2*std::log(0.8*spot*spot/minStrike),
70
0
                       std::log(Fmax) + sigmaSqrtTmax*normInvEps*scaleFactor
71
0
                                      - sigmaSqrtTmax*sigmaSqrtTmax/2.0);
72
73
0
        ext::shared_ptr<Fdm1dMesher> helper;
74
0
        if (   cPoint.first != Null<Real>() 
75
0
            && std::log(cPoint.first) >=xMin && std::log(cPoint.first) <=xMax) {
76
            
77
0
            helper = ext::shared_ptr<Fdm1dMesher>(
78
0
                new Concentrating1dMesher(xMin, xMax, size, 
79
0
                    std::pair<Real,Real>(std::log(cPoint.first),cPoint.second)));
80
0
        }
81
0
        else {
82
0
            helper = ext::shared_ptr<Fdm1dMesher>(
83
0
                                        new Uniform1dMesher(xMin, xMax, size));
84
            
85
0
        }
86
87
0
        locations_ = helper->locations();
88
0
        for (Size i=0; i < locations_.size(); ++i) {
89
0
            dplus_[i]  = helper->dplus(i);
90
0
            dminus_[i] = helper->dminus(i);
91
0
        }
92
0
    }            
93
}