Coverage Report

Created: 2026-03-11 06:44

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
0
    : process_(std::move(process)), dividends_(std::move(dividends)),
61
0
      tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
62
0
      schemeDesc_(schemeDesc), localVol_(localVol),
63
0
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
64
65
0
        registerWith(process_);
66
0
    }
67
68
0
    void FdBlackScholesRebateEngine::calculate() const {
69
70
        // 1. Mesher
71
0
        const ext::shared_ptr<StrikedTypePayoff> payoff =
72
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
73
0
        const Time maturity = process_->time(arguments_.exercise->lastDate());
74
75
0
        Real xMin=Null<Real>();
76
0
        Real xMax=Null<Real>();
77
0
        if (   arguments_.barrierType == Barrier::DownIn
78
0
            || arguments_.barrierType == Barrier::DownOut) {
79
0
            xMin = std::log(arguments_.barrier);
80
0
        }
81
0
        if (   arguments_.barrierType == Barrier::UpIn
82
0
            || arguments_.barrierType == Barrier::UpOut) {
83
0
            xMax = std::log(arguments_.barrier);
84
0
        }
85
86
0
        const ext::shared_ptr<Fdm1dMesher> equityMesher(
87
0
            new FdmBlackScholesMesher(
88
0
                xGrid_, process_, maturity, payoff->strike(),
89
0
                xMin, xMax, 0.0001, 1.5,
90
0
                std::make_pair(Null<Real>(), Null<Real>()),
91
0
                dividends_));
92
        
93
0
        const ext::shared_ptr<FdmMesher> mesher (
94
0
            new FdmMesherComposite(equityMesher));
95
        
96
        // 2. Calculator
97
0
        const ext::shared_ptr<StrikedTypePayoff> rebatePayoff(
98
0
                new CashOrNothingPayoff(Option::Call, 0.0, arguments_.rebate));
99
0
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
100
0
                                new FdmLogInnerValue(rebatePayoff, mesher, 0));
101
102
        // 3. Step conditions
103
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
104
0
                   "only european style option are supported");
105
        
106
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
107
0
            FdmStepConditionComposite::vanillaComposite(
108
0
                                dividends_, arguments_.exercise, 
109
0
                                mesher, calculator, 
110
0
                                process_->riskFreeRate()->referenceDate(),
111
0
                                process_->riskFreeRate()->dayCounter());
112
113
        // 4. Boundary conditions
114
0
        FdmBoundaryConditionSet  boundaries;
115
0
        if (   arguments_.barrierType == Barrier::DownIn
116
0
            || arguments_.barrierType == Barrier::DownOut) {
117
0
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
118
0
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
119
0
                                         FdmDirichletBoundary::Lower)));
120
121
0
        }
122
0
        if (   arguments_.barrierType == Barrier::UpIn
123
0
            || arguments_.barrierType == Barrier::UpOut) {
124
0
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
125
0
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
126
0
                                         FdmDirichletBoundary::Upper)));
127
0
        }
128
129
        // 5. Solver
130
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
131
0
                                     maturity, tGrid_, dampingSteps_ };
132
133
0
        const ext::shared_ptr<FdmBlackScholesSolver> solver(
134
0
                new FdmBlackScholesSolver(
135
0
                                Handle<GeneralizedBlackScholesProcess>(process_),
136
0
                                payoff->strike(), solverDesc, schemeDesc_,
137
0
                                localVol_, illegalLocalVolOverwrite_));
138
139
0
        const Real spot = process_->x0();
140
0
        results_.value = solver->valueAt(spot);
141
0
        results_.delta = solver->deltaAt(spot);
142
0
        results_.gamma = solver->gammaAt(spot);
143
0
        results_.theta = solver->thetaAt(spot);
144
0
    }
145
}