QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdhullwhiteswaptionengine.cpp
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 <http://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/fdhullwhiteswaptionengine.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/fdmhullwhitesolver.hpp>
29#include <ql/methods/finitedifferences/utilities/fdmaffinemodelswapinnervalue.hpp>
30#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
31
32namespace QuantLib {
33
35 const ext::shared_ptr<HullWhite>& model,
36 Size tGrid, Size xGrid,
37 Size dampingSteps, Real invEps,
38 const FdmSchemeDesc& schemeDesc)
41 tGrid_(tGrid),
42 xGrid_(xGrid),
43 dampingSteps_(dampingSteps),
44 invEps_(invEps),
45 schemeDesc_(schemeDesc) {
46 }
47
49 QL_REQUIRE(!model_.empty(), "no model specified");
50
51 // 1. Term structure
52 const Handle<YieldTermStructure> ts = model_->termStructure();
53
54 // 2. Mesher
55 const DayCounter dc = ts->dayCounter();
56 const Date referenceDate = ts->referenceDate();
57 const Time maturity = dc.yearFraction(referenceDate,
58 arguments_.exercise->lastDate());
59
60 auto process = ext::make_shared<OrnsteinUhlenbeckProcess>(model_->a(), model_->sigma());
61 auto shortRateMesher = ext::make_shared<FdmSimpleProcess1dMesher>(xGrid_, process, maturity, 1, invEps_);
62 auto mesher = ext::make_shared<FdmMesherComposite>(shortRateMesher);
63
64 // 3. Inner Value Calculator
65 const std::vector<Date>& exerciseDates = arguments_.exercise->dates();
66 std::map<Time, Date> t2d;
67
68 for (auto exerciseDate : exerciseDates) {
69 const Time t = dc.yearFraction(referenceDate, exerciseDate);
70 QL_REQUIRE(t >= 0, "exercise dates must not contain past date");
71
72 t2d[t] = exerciseDate;
73 }
74
75 const Handle<YieldTermStructure> disTs = model_->termStructure();
77 = arguments_.swap->iborIndex()->forwardingTermStructure();
78
79 QL_REQUIRE(fwdTs->dayCounter() == disTs->dayCounter(),
80 "day counter of forward and discount curve must match");
81 QL_REQUIRE(fwdTs->referenceDate() == disTs->referenceDate(),
82 "reference date of forward and discount curve must match");
83
84 auto fwdModel = ext::make_shared<HullWhite>(fwdTs, model_->a(), model_->sigma());
85 auto calculator = ext::make_shared<FdmAffineModelSwapInnerValue<HullWhite>>(
86 model_.currentLink(), fwdModel,
87 arguments_.swap, t2d, mesher, 0);
88
89 // 4. Step conditions
90 auto conditions =
92 DividendSchedule(), arguments_.exercise,
93 mesher, calculator, referenceDate, dc);
94
95 // 5. Boundary conditions
96 const FdmBoundaryConditionSet boundaries;
97
98 // 6. Solver
99 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
100 calculator, maturity,
102
103 FdmHullWhiteSolver solver(model_, solverDesc, schemeDesc_);
104
105 results_.value = solver.valueAt(0.0);
106 }
107}
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
FdHullWhiteSwaptionEngine(const ext::shared_ptr< HullWhite > &model, Size tGrid=100, Size xGrid=100, Size dampingSteps=0, Real invEps=1e-5, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas())
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Base class for some pricing engine on a particular model.
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
const ext::shared_ptr< T > & currentLink() const
dereferencing
Definition: handle.hpp:148
Single-factor Hull-White (extended Vasicek) model class.
Definition: hullwhite.hpp:49
Swaption class
Definition: swaption.hpp:81
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet