Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/finitedifferences/fdsimpleklugeextouvppengine.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 fdsimpleklugeouvppengine.cpp
21
    \brief Finite Differences engine for simple vpp options
22
*/
23
24
25
#include <ql/experimental/finitedifferences/fdmexpextouinnervaluecalculator.hpp>
26
#include <ql/experimental/finitedifferences/fdmklugeextousolver.hpp>
27
#include <ql/experimental/finitedifferences/fdmvppstepconditionfactory.hpp>
28
#include <ql/experimental/finitedifferences/fdsimpleklugeextouvppengine.hpp>
29
#include <ql/experimental/processes/extendedornsteinuhlenbeckprocess.hpp>
30
#include <ql/experimental/processes/extouwithjumpsprocess.hpp>
31
#include <ql/experimental/processes/klugeextouprocess.hpp>
32
#include <ql/instruments/basketoption.hpp>
33
#include <ql/instruments/vanillaswingoption.hpp>
34
#include <ql/methods/finitedifferences/meshers/exponentialjump1dmesher.hpp>
35
#include <ql/methods/finitedifferences/meshers/fdm1dmesher.hpp>
36
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
37
#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
38
#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
39
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
40
#include <ql/methods/finitedifferences/solvers/fdmsolverdesc.hpp>
41
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
42
#include <ql/termstructures/yieldtermstructure.hpp>
43
#include <list>
44
#include <utility>
45
46
namespace QuantLib {
47
48
    namespace {
49
        class FdmSparkSpreadInnerValue : public FdmInnerValueCalculator {
50
51
          public:
52
            FdmSparkSpreadInnerValue(ext::shared_ptr<BasketPayoff> basketPayoff,
53
                                     ext::shared_ptr<FdmInnerValueCalculator> fuelPrice,
54
                                     ext::shared_ptr<FdmInnerValueCalculator> powerPrice)
55
0
            : basketPayoff_(std::move(basketPayoff)), fuelPrice_(std::move(fuelPrice)),
56
0
              powerPrice_(std::move(powerPrice)) {}
57
58
0
            Real innerValue(const FdmLinearOpIterator& iter, Time t) override {
59
0
                Array s(2);
60
0
                s[0] = powerPrice_->innerValue(iter, t);
61
0
                s[1] = fuelPrice_->innerValue(iter, t);
62
63
0
                return (*basketPayoff_)(s);
64
0
            }
65
0
            Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
66
0
                return innerValue(iter, t);
67
0
            }
68
69
          private:
70
            const ext::shared_ptr<BasketPayoff> basketPayoff_;
71
            const ext::shared_ptr<FdmInnerValueCalculator> fuelPrice_;
72
            const ext::shared_ptr<FdmInnerValueCalculator> powerPrice_;
73
        };
74
    }
75
76
77
    FdSimpleKlugeExtOUVPPEngine::FdSimpleKlugeExtOUVPPEngine(
78
        ext::shared_ptr<KlugeExtOUProcess> process,
79
        ext::shared_ptr<YieldTermStructure> rTS,
80
        ext::shared_ptr<Shape> fuelShape,
81
        ext::shared_ptr<Shape> powerShape,
82
        Real fuelCostAddon,
83
        Size tGrid,
84
        Size xGrid,
85
        Size yGrid,
86
        Size gGrid,
87
        const FdmSchemeDesc& schemeDesc)
88
0
    : process_(std::move(process)), rTS_(std::move(rTS)), fuelCostAddon_(fuelCostAddon),
89
0
      fuelShape_(std::move(fuelShape)), powerShape_(std::move(powerShape)), tGrid_(tGrid),
90
0
      xGrid_(xGrid), yGrid_(yGrid), gGrid_(gGrid), schemeDesc_(schemeDesc) {}
91
92
0
    void FdSimpleKlugeExtOUVPPEngine::calculate() const {
93
94
0
        ext::shared_ptr<SwingExercise> swingExercise(
95
0
            ext::dynamic_pointer_cast<SwingExercise>(arguments_.exercise));
96
97
0
        QL_REQUIRE(swingExercise, "Swing exercise supported only");
98
99
0
        const FdmVPPStepConditionFactory stepConditionFactory(arguments_);
100
101
        // 1. Exercise definition
102
0
        const std::vector<Time> exerciseTimes
103
0
            = swingExercise->exerciseTimes(rTS_->dayCounter(),
104
0
                                           rTS_->referenceDate());
105
106
        // 2. mesher set-up
107
0
        const Time maturity = exerciseTimes.back();
108
0
        const ext::shared_ptr<ExtOUWithJumpsProcess> klugeProcess
109
0
            = process_->getKlugeProcess();
110
111
0
        const ext::shared_ptr<StochasticProcess1D> klugeOUProcess
112
0
            = klugeProcess->getExtendedOrnsteinUhlenbeckProcess();
113
114
0
        const ext::shared_ptr<Fdm1dMesher> xMesher(
115
0
            new FdmSimpleProcess1dMesher(xGrid_, klugeOUProcess, maturity));
116
117
0
        const ext::shared_ptr<Fdm1dMesher> yMesher(
118
0
            new ExponentialJump1dMesher(yGrid_,
119
0
                                        klugeProcess->beta(),
120
0
                                        klugeProcess->jumpIntensity(),
121
0
                                        klugeProcess->eta(), 1e-3));
122
123
0
        const ext::shared_ptr<Fdm1dMesher> gMesher(
124
0
            new FdmSimpleProcess1dMesher(gGrid_,
125
0
                                         process_->getExtOUProcess(),maturity));
126
127
0
        const ext::shared_ptr<Fdm1dMesher> exerciseMesher(
128
0
            stepConditionFactory.stateMesher());
129
130
0
        const ext::shared_ptr<FdmMesher> mesher (
131
0
            new FdmMesherComposite(xMesher, yMesher, gMesher, exerciseMesher));
132
133
        // 3. Calculator
134
0
        const ext::shared_ptr<FdmInnerValueCalculator> zeroInnerValue(
135
0
            new FdmZeroInnerValue());
136
137
0
        const ext::shared_ptr<Payoff> zeroStrikeCall(
138
0
            new PlainVanillaPayoff(Option::Call, 0.0));
139
140
0
        const ext::shared_ptr<FdmInnerValueCalculator> fuelPrice(
141
0
            new FdmExpExtOUInnerValueCalculator(zeroStrikeCall,
142
0
                                                mesher, fuelShape_, 2));
143
144
0
        const ext::shared_ptr<FdmInnerValueCalculator> powerPrice(
145
0
            new FdmExtOUJumpModelInnerValue(zeroStrikeCall,mesher,powerShape_));
146
147
0
        const ext::shared_ptr<FdmInnerValueCalculator> sparkSpread(
148
0
            new FdmSparkSpreadInnerValue(
149
0
                ext::dynamic_pointer_cast<BasketPayoff>(arguments_.payoff),
150
0
                fuelPrice, powerPrice));
151
152
        // 4. Step conditions
153
0
        std::list<std::vector<Time> > stoppingTimes;
154
0
        std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
155
156
        // 4.1 Bermudan step conditions
157
0
        stoppingTimes.push_back(exerciseTimes);
158
0
        const FdmVPPStepConditionMesher mesh = {3U, mesher};
159
160
0
        const ext::shared_ptr<FdmVPPStepCondition> stepCondition(
161
0
            stepConditionFactory.build(mesh, fuelCostAddon_,
162
0
                                       fuelPrice, sparkSpread));
163
164
0
        stepConditions.push_back(stepCondition);
165
166
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions(
167
0
            new FdmStepConditionComposite(stoppingTimes, stepConditions));
168
169
        // 5. Boundary conditions
170
0
        const FdmBoundaryConditionSet boundaries;
171
172
        // 6. set-up solver
173
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
174
0
                                     zeroInnerValue, maturity, tGrid_, 0 };
175
176
0
        const ext::shared_ptr<FdmKlugeExtOUSolver<4> > solver(
177
0
            new FdmKlugeExtOUSolver<4>(Handle<KlugeExtOUProcess>(process_),
178
0
                                       rTS_, solverDesc, schemeDesc_));
179
180
0
        std::vector<Real> x(4);
181
0
        x[0] = process_->initialValues()[0];
182
0
        x[1] = process_->initialValues()[1];
183
0
        x[2] = process_->initialValues()[2];
184
        
185
0
        const Real tol = 1e-8;
186
0
        const Real maxExerciseValue = exerciseMesher->locations().back();
187
0
        const Real minExerciseValue = exerciseMesher->locations().front();
188
189
0
        Array results(exerciseMesher->size());
190
0
        for (Size i=0; i < results.size(); ++i) {
191
192
0
            x[3] = std::max(minExerciseValue + tol,
193
0
                            std::min(exerciseMesher->location(i),
194
0
                                     maxExerciseValue - tol));
195
0
            results[i] = solver->valueAt(x);
196
0
        }
197
0
        results_.value = stepCondition->maxValue(results);
198
0
    }
199
}