Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/swaption/fdg2swaptionengine.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
#include <ql/exercise.hpp>
21
#include <ql/indexes/iborindex.hpp>
22
#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
23
#include <ql/pricingengines/swaption/fdg2swaptionengine.hpp>
24
#include <ql/methods/finitedifferences/solvers/fdmsolverdesc.hpp>
25
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
26
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
27
#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
28
#include <ql/methods/finitedifferences/solvers/fdmg2solver.hpp>
29
#include <ql/methods/finitedifferences/utilities/fdmaffinemodelswapinnervalue.hpp>
30
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
31
32
namespace QuantLib {
33
34
    FdG2SwaptionEngine::FdG2SwaptionEngine(
35
        const ext::shared_ptr<G2>& model,
36
        Size tGrid, Size xGrid, Size yGrid,
37
        Size dampingSteps, Real invEps,
38
        const FdmSchemeDesc& schemeDesc)
39
0
    : GenericModelEngine<G2, Swaption::arguments, Swaption::results>(model),
40
0
      tGrid_(tGrid),
41
0
      xGrid_(xGrid),
42
0
      yGrid_(yGrid),
43
0
      dampingSteps_(dampingSteps),
44
0
      invEps_(invEps),
45
0
      schemeDesc_(schemeDesc) {
46
0
    }
47
48
0
    void FdG2SwaptionEngine::calculate() const {
49
0
        QL_REQUIRE(!model_.empty(), "no model specified");
50
51
        // 1. Term structure
52
0
        const Handle<YieldTermStructure> ts = model_->termStructure();
53
54
        // 2. Mesher
55
0
        const DayCounter dc = ts->dayCounter();
56
0
        const Date referenceDate = ts->referenceDate();
57
0
        const Time maturity = dc.yearFraction(referenceDate,
58
0
                                              arguments_.exercise->lastDate());
59
60
0
        const ext::shared_ptr<OrnsteinUhlenbeckProcess> process1(
61
0
            new OrnsteinUhlenbeckProcess(model_->a(), model_->sigma()));
62
63
0
        const ext::shared_ptr<OrnsteinUhlenbeckProcess> process2(
64
0
            new OrnsteinUhlenbeckProcess(model_->b(), model_->eta()));
65
66
0
        const ext::shared_ptr<Fdm1dMesher> xMesher(
67
0
            new FdmSimpleProcess1dMesher(xGrid_,process1,maturity,1,invEps_));
68
69
0
        const ext::shared_ptr<Fdm1dMesher> yMesher(
70
0
            new FdmSimpleProcess1dMesher(yGrid_,process2,maturity,1,invEps_));
71
72
0
        const ext::shared_ptr<FdmMesher> mesher(
73
0
            new FdmMesherComposite(xMesher, yMesher));
74
75
        // 3. Inner Value Calculator
76
0
        const std::vector<Date>& exerciseDates = arguments_.exercise->dates();
77
0
        std::map<Time, Date> t2d;
78
79
0
        for (auto exerciseDate : exerciseDates) {
80
0
            const Time t = dc.yearFraction(referenceDate, exerciseDate);
81
0
            QL_REQUIRE(t >= 0, "exercise dates must not contain past date");
82
83
0
            t2d[t] = exerciseDate;
84
0
        }
85
86
0
        const Handle<YieldTermStructure> disTs = model_->termStructure();
87
0
        const Handle<YieldTermStructure> fwdTs
88
0
            = arguments_.swap->iborIndex()->forwardingTermStructure();
89
90
0
        QL_REQUIRE(fwdTs->dayCounter() == disTs->dayCounter(),
91
0
                "day counter of forward and discount curve must match");
92
0
        QL_REQUIRE(fwdTs->referenceDate() == disTs->referenceDate(),
93
0
                "reference date of forward and discount curve must match");
94
95
0
        const ext::shared_ptr<G2> fwdModel(
96
0
            new G2(fwdTs, model_->a(), model_->sigma(),
97
0
                   model_->b(), model_->eta(), model_->rho()));
98
99
0
        const ext::shared_ptr<FdmInnerValueCalculator> calculator(
100
0
             new FdmAffineModelSwapInnerValue<G2>(
101
0
                 model_.currentLink(), fwdModel,
102
0
                 arguments_.swap, t2d, mesher, 0));
103
104
        // 4. Step conditions
105
0
        const ext::shared_ptr<FdmStepConditionComposite> conditions =
106
0
             FdmStepConditionComposite::vanillaComposite(
107
0
                 DividendSchedule(), arguments_.exercise,
108
0
                 mesher, calculator, referenceDate, dc);
109
110
        // 5. Boundary conditions
111
0
        const FdmBoundaryConditionSet boundaries;
112
113
        // 6. Solver
114
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
115
0
                                     calculator, maturity,
116
0
                                     tGrid_, dampingSteps_ };
117
118
0
        FdmG2Solver solver(model_, solverDesc, schemeDesc_);
119
120
0
        results_.value = solver.valueAt(0.0, 0.0);
121
0
    }
122
}