Coverage Report

Created: 2026-09-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/basket/fd2dblackscholesvanillaengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2010 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
#include <ql/exercise.hpp>
21
#include <ql/methods/finitedifferences/solvers/fdm2dblackscholessolver.hpp>
22
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
23
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
25
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
26
#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
27
#include <ql/pricingengines/basket/fd2dblackscholesvanillaengine.hpp>
28
29
namespace QuantLib {
30
31
    Fd2dBlackScholesVanillaEngine::Fd2dBlackScholesVanillaEngine(
32
        const ext::shared_ptr<GeneralizedBlackScholesProcess>& p1,
33
        const ext::shared_ptr<GeneralizedBlackScholesProcess>& p2,
34
        Real correlation,
35
        Size xGrid, Size yGrid,
36
        Size tGrid, Size dampingSteps,
37
        const FdmSchemeDesc& schemeDesc,
38
        bool localVol,
39
        Real illegalLocalVolOverwrite)
40
0
    : p1_(p1),
41
0
      p2_(p2),
42
0
      correlation_(correlation),
43
0
      xGrid_(xGrid), yGrid_(yGrid), tGrid_(tGrid),
44
0
      dampingSteps_(dampingSteps),
45
0
      schemeDesc_(schemeDesc),
46
0
      localVol_(localVol),
47
0
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
48
0
        registerWith(p1);
49
0
        registerWith(p2);
50
0
    }
51
52
0
    void Fd2dBlackScholesVanillaEngine::calculate() const {
53
        // 1. Payoff
54
0
        const ext::shared_ptr<BasketPayoff> payoff =
55
0
            ext::dynamic_pointer_cast<BasketPayoff>(arguments_.payoff);
56
57
        // 2. Mesher
58
0
        const Time maturity = p1_->time(arguments_.exercise->lastDate());
59
0
        const auto em1 = ext::make_shared<FdmBlackScholesMesher>(
60
0
                    xGrid_, p1_, maturity, p1_->x0(), 
61
0
                    Null<Real>(), Null<Real>(), 0.0001, 1.5, 
62
0
                    std::pair<Real, Real>(p1_->x0(), 0.1));
63
64
0
        const auto em2 = ext::make_shared<FdmBlackScholesMesher>(
65
0
                    yGrid_, p2_, maturity, p2_->x0(),
66
0
                    Null<Real>(), Null<Real>(), 0.0001, 1.5, 
67
0
                    std::pair<Real, Real>(p2_->x0(), 0.1));
68
69
0
        const auto mesher = ext::make_shared<FdmMesherComposite>(em1, em2);
70
71
        // 3. Calculator
72
0
        const auto calculator = ext::make_shared<FdmLogBasketInnerValue>(payoff, mesher);
73
74
        // 4. Step conditions
75
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
76
0
            FdmStepConditionComposite::vanillaComposite(
77
0
                                    DividendSchedule(), arguments_.exercise, 
78
0
                                    mesher, calculator, 
79
0
                                    p1_->riskFreeRate()->referenceDate(),
80
0
                                    p1_->riskFreeRate()->dayCounter());
81
82
        // 5. Boundary conditions
83
0
        const FdmBoundaryConditionSet boundaries;
84
85
        // 6. Solver
86
0
        const FdmSolverDesc solverDesc = { mesher, boundaries,
87
0
                                           conditions, calculator,
88
0
                                           maturity, tGrid_, dampingSteps_ };
89
90
0
        auto solver = ext::make_shared<Fdm2dBlackScholesSolver>(
91
0
                             Handle<GeneralizedBlackScholesProcess>(p1_),
92
0
                             Handle<GeneralizedBlackScholesProcess>(p2_),
93
0
                             correlation_, solverDesc, schemeDesc_,
94
0
                             localVol_, illegalLocalVolOverwrite_);
95
96
0
        const Real x = p1_->x0();
97
0
        const Real y = p2_->x0();
98
99
0
        results_.value = solver->valueAt(x, y);
100
0
        results_.delta = solver->deltaXat(x, y) + solver->deltaYat(x, y);
101
0
        results_.gamma = solver->gammaXat(x, y) + solver->gammaYat(x, y)
102
0
             + 2*solver->gammaXYat(x, y);
103
0
        results_.theta = solver->thetaAt(x, y);
104
0
    }
105
}