Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/finitedifferences/fdsimpleextoujumpswingengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2011 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
/*! \file fdsimpleextoujumpswingengine.cpp
21
    \brief Finite Differences engine for simple swing options
22
*/
23
24
25
#include <ql/experimental/finitedifferences/fdmextoujumpmodelinnervalue.hpp>
26
#include <ql/experimental/finitedifferences/fdmsimple3dextoujumpsolver.hpp>
27
#include <ql/experimental/finitedifferences/fdsimpleextoujumpswingengine.hpp>
28
#include <ql/experimental/processes/extendedornsteinuhlenbeckprocess.hpp>
29
#include <ql/experimental/processes/extouwithjumpsprocess.hpp>
30
#include <ql/methods/finitedifferences/meshers/exponentialjump1dmesher.hpp>
31
#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
32
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
33
#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
34
#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
35
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
36
#include <ql/methods/finitedifferences/solvers/fdm3dimsolver.hpp>
37
#include <ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.hpp>
38
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
39
#include <ql/termstructures/yieldtermstructure.hpp>
40
#include <utility>
41
42
namespace QuantLib {
43
44
    FdSimpleExtOUJumpSwingEngine::FdSimpleExtOUJumpSwingEngine(
45
        ext::shared_ptr<ExtOUWithJumpsProcess> process,
46
        ext::shared_ptr<YieldTermStructure> rTS,
47
        Size tGrid,
48
        Size xGrid,
49
        Size yGrid,
50
        ext::shared_ptr<Shape> shape,
51
        const FdmSchemeDesc& schemeDesc)
52
0
    : process_(std::move(process)), rTS_(std::move(rTS)), shape_(std::move(shape)), tGrid_(tGrid),
53
0
      xGrid_(xGrid), yGrid_(yGrid), schemeDesc_(schemeDesc) {}
54
55
0
    void FdSimpleExtOUJumpSwingEngine::calculate() const {
56
57
        // 1. Exercise
58
0
        ext::shared_ptr<SwingExercise> swingExercise(
59
0
            ext::dynamic_pointer_cast<SwingExercise>(arguments_.exercise));
60
61
0
        QL_REQUIRE(swingExercise, "Swing exercise supported only");
62
63
        // 2. Mesher
64
0
        const std::vector<Time> exerciseTimes
65
0
            = swingExercise->exerciseTimes(rTS_->dayCounter(),
66
0
                                           rTS_->referenceDate());
67
68
0
        const Time maturity = exerciseTimes.back();
69
0
        const ext::shared_ptr<StochasticProcess1D> ouProcess(
70
0
                              process_->getExtendedOrnsteinUhlenbeckProcess());
71
0
        const ext::shared_ptr<Fdm1dMesher> xMesher(
72
0
                     new FdmSimpleProcess1dMesher(xGrid_, ouProcess,maturity));
73
74
0
        const ext::shared_ptr<Fdm1dMesher> yMesher(
75
0
                        new ExponentialJump1dMesher(yGrid_,
76
0
                                                    process_->beta(),
77
0
                                                    process_->jumpIntensity(),
78
0
                                                    process_->eta()));
79
0
        const ext::shared_ptr<Fdm1dMesher> exerciseMesher(
80
0
                       new Uniform1dMesher(
81
0
                           0, static_cast<Real>(arguments_.maxExerciseRights),
82
0
                           arguments_.maxExerciseRights+1));
83
84
0
        const ext::shared_ptr<FdmMesher> mesher(
85
0
            new FdmMesherComposite(xMesher, yMesher, exerciseMesher));
86
87
        // 3. Calculator
88
0
        ext::shared_ptr<FdmInnerValueCalculator> calculator(
89
0
                                                    new FdmZeroInnerValue());
90
        // 4. Step conditions
91
0
        std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
92
0
        std::list<std::vector<Time> > stoppingTimes;
93
94
        // 4.1 Bermudan step conditions
95
0
        stoppingTimes.push_back(exerciseTimes);
96
97
0
        ext::shared_ptr<FdmInnerValueCalculator> exerciseCalculator(
98
0
            new FdmExtOUJumpModelInnerValue(arguments_.payoff, mesher, shape_));
99
100
0
        stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >(
101
0
            new FdmSimpleSwingCondition(
102
0
                exerciseTimes, mesher, exerciseCalculator,
103
0
                2, arguments_.minExerciseRights)));
104
105
0
        ext::shared_ptr<FdmStepConditionComposite> conditions(
106
0
                new FdmStepConditionComposite(stoppingTimes, stepConditions));
107
108
109
        // 5. Boundary conditions
110
0
        const FdmBoundaryConditionSet boundaries;
111
112
        // 6. set-up solver
113
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
114
0
                                     calculator, maturity, tGrid_, 0 };
115
116
0
        const ext::shared_ptr<FdmSimple3dExtOUJumpSolver> solver(
117
0
            new FdmSimple3dExtOUJumpSolver(
118
0
                                    Handle<ExtOUWithJumpsProcess>(process_),
119
0
                                    rTS_, solverDesc, schemeDesc_));
120
121
0
        const Real x = process_->initialValues()[0];
122
0
        const Real y = process_->initialValues()[1];
123
124
0
        results_.value = solver->valueAt(x, y, 0.0);
125
0
    }
126
}