Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/pricingengines/barrier/fdhestonrebateengine.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) 2008 Andreas Gaida
5
 Copyright (C) 2008, 2009 Ralph Schreyer
6
 Copyright (C) 2008, 2009 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
 <http://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/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmhestonvariancemesher.hpp>
24
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
25
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26
#include <ql/methods/finitedifferences/solvers/fdmbackwardsolver.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/fdhestonrebateengine.hpp>
31
#include <utility>
32
33
namespace QuantLib {
34
35
    QL_DEPRECATED_DISABLE_WARNING
36
37
    FdHestonRebateEngine::FdHestonRebateEngine(const ext::shared_ptr<HestonModel>& model,
38
                                               Size tGrid,
39
                                               Size xGrid,
40
                                               Size vGrid,
41
                                               Size dampingSteps,
42
                                               const FdmSchemeDesc& schemeDesc,
43
                                               ext::shared_ptr<LocalVolTermStructure> leverageFct,
44
                                               const Real mixingFactor)
45
0
    : GenericModelEngine<HestonModel,
46
0
                         BarrierOption::arguments,
47
0
                         BarrierOption::results>(model),
48
0
      tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
49
0
      schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
50
51
    FdHestonRebateEngine::FdHestonRebateEngine(const ext::shared_ptr<HestonModel>& model,
52
                                               DividendSchedule dividends,
53
                                               Size tGrid,
54
                                               Size xGrid,
55
                                               Size vGrid,
56
                                               Size dampingSteps,
57
                                               const FdmSchemeDesc& schemeDesc,
58
                                               ext::shared_ptr<LocalVolTermStructure> leverageFct,
59
                                               const Real mixingFactor)
60
0
    : GenericModelEngine<HestonModel,
61
0
                         BarrierOption::arguments,
62
0
                         BarrierOption::results>(model),
63
0
      dividends_(std::move(dividends)),
64
0
      tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
65
0
      schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
66
67
    QL_DEPRECATED_ENABLE_WARNING
68
69
0
    void FdHestonRebateEngine::calculate() const {
70
71
        // 1. Mesher
72
0
        const ext::shared_ptr<HestonProcess>& process = model_->process();
73
0
        const Time maturity = process->time(arguments_.exercise->lastDate());
74
75
        // 1.1 The variance mesher
76
0
        const Size tGridMin = 5;
77
0
        const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50);
78
79
0
        const ext::shared_ptr<FdmHestonLocalVolatilityVarianceMesher> vMesher
80
0
            = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>(
81
0
                  vGrid_, process, leverageFct_, maturity, tGridAvgSteps, 0.0001, mixingFactor_);
82
83
        // 1.2 The equity mesher
84
0
        const ext::shared_ptr<StrikedTypePayoff> payoff =
85
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
86
87
0
        Real xMin=Null<Real>();
88
0
        Real xMax=Null<Real>();
89
0
        if (   arguments_.barrierType == Barrier::DownIn
90
0
            || arguments_.barrierType == Barrier::DownOut) {
91
0
            xMin = std::log(arguments_.barrier);
92
0
        }
93
0
        if (   arguments_.barrierType == Barrier::UpIn
94
0
            || arguments_.barrierType == Barrier::UpOut) {
95
0
            xMax = std::log(arguments_.barrier);
96
0
        }
97
98
0
        const ext::shared_ptr<Fdm1dMesher> equityMesher(
99
0
            new FdmBlackScholesMesher(
100
0
                xGrid_,
101
0
                FdmBlackScholesMesher::processHelper(
102
0
                    process->s0(), process->dividendYield(),
103
0
                    process->riskFreeRate(), vMesher->volaEstimate()),
104
0
                maturity, payoff->strike(),
105
0
                xMin, xMax, 0.0001, 1.5,
106
0
                std::make_pair(Null<Real>(), Null<Real>()),
107
0
                dividends_));
108
109
0
        const ext::shared_ptr<FdmMesher> mesher (
110
0
            new FdmMesherComposite(equityMesher, vMesher));
111
112
        // 2. Calculator
113
0
        const ext::shared_ptr<StrikedTypePayoff> rebatePayoff(
114
0
                new CashOrNothingPayoff(Option::Call, 0.0, arguments_.rebate));
115
0
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
116
0
                                new FdmLogInnerValue(rebatePayoff, mesher, 0));
117
118
        // 3. Step conditions
119
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
120
0
                   "only european style option are supported");
121
122
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions = 
123
0
             FdmStepConditionComposite::vanillaComposite(
124
0
                                 dividends_, arguments_.exercise, 
125
0
                                 mesher, calculator, 
126
0
                                 process->riskFreeRate()->referenceDate(),
127
0
                                 process->riskFreeRate()->dayCounter());
128
129
        // 4. Boundary conditions
130
0
        FdmBoundaryConditionSet boundaries;
131
0
        if (   arguments_.barrierType == Barrier::DownIn
132
0
            || arguments_.barrierType == Barrier::DownOut) {
133
0
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
134
0
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
135
0
                                         FdmDirichletBoundary::Lower)));
136
137
0
        }
138
0
        if (   arguments_.barrierType == Barrier::UpIn
139
0
            || arguments_.barrierType == Barrier::UpOut) {
140
0
            boundaries.push_back(FdmBoundaryConditionSet::value_type(
141
0
                new FdmDirichletBoundary(mesher, arguments_.rebate, 0,
142
0
                                         FdmDirichletBoundary::Upper)));
143
0
        }
144
145
        // 5. Solver
146
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
147
0
                                     calculator, maturity,
148
0
                                     tGrid_, dampingSteps_ };
149
150
0
        ext::shared_ptr<FdmHestonSolver> solver(new FdmHestonSolver(
151
0
                    Handle<HestonProcess>(process), solverDesc, schemeDesc_,
152
0
                    Handle<FdmQuantoHelper>(), leverageFct_, mixingFactor_));
153
154
0
        const Real spot = process->s0()->value();
155
0
        results_.value = solver->valueAt(spot, process->v0());
156
0
        results_.delta = solver->deltaAt(spot, process->v0());
157
0
        results_.gamma = solver->gammaAt(spot, process->v0());
158
0
        results_.theta = solver->thetaAt(spot, process->v0());
159
0
    }
160
}