Coverage Report

Created: 2026-06-08 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/barrier/fdblackscholesrebateengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Andreas Gaida
5
 Copyright (C) 2008, 2009 Ralph Schreyer
6
 Copyright (C) 2008 Klaus Spanderen
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/exercise.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
24
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
25
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26
#include <ql/methods/finitedifferences/solvers/fdmblackscholessolver.hpp>
27
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
28
#include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp>
29
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
30
#include <ql/pricingengines/barrier/fdblackscholesrebateengine.hpp>
31
#include <utility>
32
33
namespace QuantLib {
34
35
    FdBlackScholesRebateEngine::FdBlackScholesRebateEngine(
36
        ext::shared_ptr<GeneralizedBlackScholesProcess> process,
37
        Size tGrid,
38
        Size xGrid,
39
        Size dampingSteps,
40
        const FdmSchemeDesc& schemeDesc,
41
        bool localVol,
42
        Real illegalLocalVolOverwrite)
43
0
    : process_(std::move(process)),
44
0
      tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
45
0
      schemeDesc_(schemeDesc), localVol_(localVol),
46
0
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
47
48
0
        registerWith(process_);
49
0
    }
50
51
    FdBlackScholesRebateEngine::FdBlackScholesRebateEngine(
52
        ext::shared_ptr<GeneralizedBlackScholesProcess> process,
53
        DividendSchedule dividends,
54
        Size tGrid,
55
        Size xGrid,
56
        Size dampingSteps,
57
        const FdmSchemeDesc& schemeDesc,
58
        bool localVol,
59
        Real illegalLocalVolOverwrite)
60
676
    : process_(std::move(process)), dividends_(std::move(dividends)),
61
676
      tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
62
676
      schemeDesc_(schemeDesc), localVol_(localVol),
63
676
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
64
65
676
        registerWith(process_);
66
676
    }
67
68
676
    void FdBlackScholesRebateEngine::calculate() const {
69
70
        // 1. Mesher
71
676
        const ext::shared_ptr<StrikedTypePayoff> payoff =
72
676
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
73
676
        const Time maturity = process_->time(arguments_.exercise->lastDate());
74
75
676
        Real xMin=Null<Real>();
76
676
        Real xMax=Null<Real>();
77
676
        if (   arguments_.barrierType == Barrier::DownIn
78
386
            || arguments_.barrierType == Barrier::DownOut) {
79
290
            xMin = std::log(arguments_.barrier);
80
290
        }
81
676
        if (   arguments_.barrierType == Barrier::UpIn
82
386
            || arguments_.barrierType == Barrier::UpOut) {
83
386
            xMax = std::log(arguments_.barrier);
84
386
        }
85
86
676
        const ext::shared_ptr<Fdm1dMesher> equityMesher(
87
676
            new FdmBlackScholesMesher(
88
676
                xGrid_, process_, maturity, payoff->strike(),
89
676
                xMin, xMax, 0.0001, 1.5,
90
676
                std::make_pair(Null<Real>(), Null<Real>()),
91
676
                dividends_));
92
        
93
676
        const ext::shared_ptr<FdmMesher> mesher (
94
676
            new FdmMesherComposite(equityMesher));
95
        
96
        // 2. Calculator
97
676
        const ext::shared_ptr<StrikedTypePayoff> rebatePayoff(
98
676
                new CashOrNothingPayoff(Option::Call, 0.0, arguments_.rebate));
99
676
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
100
676
                                new FdmLogInnerValue(rebatePayoff, mesher, 0));
101
102
        // 3. Step conditions
103
676
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
104
676
                   "only european style option are supported");
105
        
106
676
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
107
676
            FdmStepConditionComposite::vanillaComposite(
108
676
                                dividends_, arguments_.exercise, 
109
676
                                mesher, calculator, 
110
676
                                process_->riskFreeRate()->referenceDate(),
111
676
                                process_->riskFreeRate()->dayCounter());
112
113
        // 4. Boundary conditions
114
676
        FdmBoundaryConditionSet  boundaries;
115
676
        if (   arguments_.barrierType == Barrier::DownIn
116
386
            || arguments_.barrierType == Barrier::DownOut) {
117
290
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
118
290
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
119
290
                                         FdmDirichletBoundary::Lower)));
120
121
290
        }
122
676
        if (   arguments_.barrierType == Barrier::UpIn
123
386
            || arguments_.barrierType == Barrier::UpOut) {
124
386
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
125
386
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
126
386
                                         FdmDirichletBoundary::Upper)));
127
386
        }
128
129
        // 5. Solver
130
676
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
131
676
                                     maturity, tGrid_, dampingSteps_ };
132
133
676
        const ext::shared_ptr<FdmBlackScholesSolver> solver(
134
676
                new FdmBlackScholesSolver(
135
676
                                Handle<GeneralizedBlackScholesProcess>(process_),
136
676
                                payoff->strike(), solverDesc, schemeDesc_,
137
676
                                localVol_, illegalLocalVolOverwrite_));
138
139
676
        const Real spot = process_->x0();
140
676
        results_.value = solver->valueAt(spot);
141
676
        results_.delta = solver->deltaAt(spot);
142
676
        results_.gamma = solver->gammaAt(spot);
143
676
        results_.theta = solver->thetaAt(spot);
144
676
    }
145
}