Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/pricingengines/basket/fd2dblackscholesvanillaengine.cpp
Line
Count
Source (jump to first uncovered line)
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 ext::shared_ptr<Fdm1dMesher> em1(
60
0
            new FdmBlackScholesMesher(
61
0
                    xGrid_, p1_, maturity, p1_->x0(), 
62
0
                    Null<Real>(), Null<Real>(), 0.0001, 1.5, 
63
0
                    std::pair<Real, Real>(p1_->x0(), 0.1)));
64
65
0
        const ext::shared_ptr<Fdm1dMesher> em2(
66
0
            new FdmBlackScholesMesher(
67
0
                    yGrid_, p2_, maturity, p2_->x0(),
68
0
                    Null<Real>(), Null<Real>(), 0.0001, 1.5, 
69
0
                    std::pair<Real, Real>(p2_->x0(), 0.1)));
70
71
0
        const ext::shared_ptr<FdmMesher> mesher (
72
0
            new FdmMesherComposite(em1, em2));
73
74
        // 3. Calculator
75
0
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
76
0
                                new FdmLogBasketInnerValue(payoff, mesher));
77
78
        // 4. Step conditions
79
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
80
0
            FdmStepConditionComposite::vanillaComposite(
81
0
                                    DividendSchedule(), arguments_.exercise, 
82
0
                                    mesher, calculator, 
83
0
                                    p1_->riskFreeRate()->referenceDate(),
84
0
                                    p1_->riskFreeRate()->dayCounter());
85
86
        // 5. Boundary conditions
87
0
        const FdmBoundaryConditionSet boundaries;
88
89
        // 6. Solver
90
0
        const FdmSolverDesc solverDesc = { mesher, boundaries,
91
0
                                           conditions, calculator,
92
0
                                           maturity, tGrid_, dampingSteps_ };
93
94
0
        ext::shared_ptr<Fdm2dBlackScholesSolver> solver(
95
0
                new Fdm2dBlackScholesSolver(
96
0
                             Handle<GeneralizedBlackScholesProcess>(p1_),
97
0
                             Handle<GeneralizedBlackScholesProcess>(p2_),
98
0
                             correlation_, solverDesc, schemeDesc_,
99
0
                             localVol_, illegalLocalVolOverwrite_));
100
101
0
        const Real x = p1_->x0();
102
0
        const Real y = p2_->x0();
103
104
0
        results_.value = solver->valueAt(x, y);
105
0
        results_.delta = solver->deltaXat(x, y) + solver->deltaYat(x, y);
106
0
        results_.gamma = solver->gammaXat(x, y) + solver->gammaYat(x, y)
107
0
             + 2*solver->gammaXYat(x, y);
108
0
        results_.theta = solver->thetaAt(x, y);
109
0
    }
110
}