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/fdornsteinuhlenbeckvanillaengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2016 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
#include <ql/exercise.hpp>
21
#include <ql/experimental/finitedifferences/fdornsteinuhlenbeckvanillaengine.hpp>
22
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
24
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25
#include <ql/methods/finitedifferences/operators/fdmornsteinuhlenbeckop.hpp>
26
#include <ql/methods/finitedifferences/solvers/fdm1dimsolver.hpp>
27
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
28
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
29
#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
30
#include <ql/termstructures/yieldtermstructure.hpp>
31
#include <utility>
32
33
namespace QuantLib {
34
35
    namespace {
36
        class FdmOUInnerValue : public FdmInnerValueCalculator {
37
          public:
38
            FdmOUInnerValue(ext::shared_ptr<Payoff> payoff,
39
                            ext::shared_ptr<FdmMesher> mesher,
40
                            Size direction)
41
0
            : payoff_(std::move(payoff)), mesher_(std::move(mesher)), direction_(direction) {}
42
43
44
0
            Real innerValue(const FdmLinearOpIterator& iter, Time) override {
45
0
                const Real s = mesher_->location(iter, direction_);
46
0
                return (*payoff_)(s);
47
0
            }
48
49
0
            Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
50
0
                return innerValue(iter, t);
51
0
            }
52
53
          private:
54
            const ext::shared_ptr<Payoff> payoff_;
55
            const ext::shared_ptr<FdmMesher> mesher_;
56
            const Size direction_;
57
        };
58
    }
59
60
    FdOrnsteinUhlenbeckVanillaEngine::FdOrnsteinUhlenbeckVanillaEngine(
61
        ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
62
        const ext::shared_ptr<YieldTermStructure>& rTS,
63
        Size tGrid,
64
        Size xGrid,
65
        Size dampingSteps,
66
        Real epsilon,
67
        const FdmSchemeDesc& schemeDesc)
68
0
    : process_(std::move(process)), rTS_(rTS), tGrid_(tGrid), xGrid_(xGrid),
69
0
      dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
70
0
        registerWith(process_);
71
0
        registerWith(rTS);
72
0
    }
73
74
    FdOrnsteinUhlenbeckVanillaEngine::FdOrnsteinUhlenbeckVanillaEngine(
75
        ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
76
        const ext::shared_ptr<YieldTermStructure>& rTS,
77
        DividendSchedule dividends,
78
        Size tGrid,
79
        Size xGrid,
80
        Size dampingSteps,
81
        Real epsilon,
82
        const FdmSchemeDesc& schemeDesc)
83
0
    : process_(std::move(process)), rTS_(rTS),
84
0
      dividends_(std::move(dividends)), tGrid_(tGrid), xGrid_(xGrid),
85
0
      dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
86
0
        registerWith(process_);
87
0
        registerWith(rTS);
88
0
    }
89
90
0
    void FdOrnsteinUhlenbeckVanillaEngine::calculate() const {
91
92
        // 1. Mesher
93
0
        const ext::shared_ptr<StrikedTypePayoff> payoff =
94
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
95
96
0
        const DayCounter dc = rTS_->dayCounter();
97
0
        const Date referenceDate = rTS_->referenceDate();
98
99
0
        const Time maturity = dc.yearFraction(
100
0
            referenceDate, arguments_.exercise->lastDate());
101
102
0
        const ext::shared_ptr<Fdm1dMesher> equityMesher(
103
0
            new FdmSimpleProcess1dMesher(
104
0
                xGrid_, process_, maturity, 1, epsilon_));
105
106
0
        const ext::shared_ptr<FdmMesher> mesher (
107
0
            new FdmMesherComposite(equityMesher));
108
109
        // 2. Calculator
110
0
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
111
0
            new FdmOUInnerValue(payoff, mesher, 0));
112
113
        // 3. Step conditions
114
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
115
0
            FdmStepConditionComposite::vanillaComposite(
116
0
                                    dividends_, arguments_.exercise,
117
0
                                    mesher, calculator,
118
0
                                    referenceDate, dc);
119
120
        // 4. Boundary conditions
121
0
        const FdmBoundaryConditionSet boundaries;
122
123
        // 5. Solver
124
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
125
0
                                     maturity, tGrid_, dampingSteps_ };
126
127
0
        const ext::shared_ptr<FdmOrnsteinUhlenbeckOp> op(
128
0
            new FdmOrnsteinUhlenbeckOp(mesher, process_, rTS_, 0));
129
130
0
        const ext::shared_ptr<Fdm1DimSolver> solver(
131
0
                new Fdm1DimSolver(solverDesc, schemeDesc_, op));
132
133
0
        const Real spot = process_->x0();
134
135
0
        results_.value = solver->interpolateAt(spot);
136
0
        results_.delta = solver->derivativeX(spot);
137
0
        results_.gamma = solver->derivativeXX(spot);
138
0
        results_.theta = solver->thetaAt(spot);
139
0
    }
140
}