QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmbackwardsolver.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Andreas Gaida
5 Copyright (C) 2009 Ralph Schreyer
6 Copyright (C) 2009 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
25#include <ql/mathconstants.hpp>
26#include <ql/methods/finitedifferences/finitedifferencemodel.hpp>
27#include <ql/methods/finitedifferences/schemes/craigsneydscheme.hpp>
28#include <ql/methods/finitedifferences/schemes/cranknicolsonscheme.hpp>
29#include <ql/methods/finitedifferences/schemes/douglasscheme.hpp>
30#include <ql/methods/finitedifferences/schemes/expliciteulerscheme.hpp>
31#include <ql/methods/finitedifferences/schemes/hundsdorferscheme.hpp>
32#include <ql/methods/finitedifferences/schemes/impliciteulerscheme.hpp>
33#include <ql/methods/finitedifferences/schemes/methodoflinesscheme.hpp>
34#include <ql/methods/finitedifferences/schemes/modifiedcraigsneydscheme.hpp>
35#include <ql/methods/finitedifferences/schemes/trbdf2scheme.hpp>
36#include <ql/methods/finitedifferences/solvers/fdmbackwardsolver.hpp>
37#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
38#include <utility>
39
40
41namespace QuantLib {
42
44 : type(aType), theta(aTheta), mu(aMu) { }
45
47
49 return {FdmSchemeDesc::CrankNicolsonType, 0.5, 0.0};
50 }
51
53
55 return {FdmSchemeDesc::ModifiedCraigSneydType, 1.0 / 3.0, 1.0 / 3.0};
56 }
57
59 return {FdmSchemeDesc::HundsdorferType, 0.5 + std::sqrt(3.0) / 6, 0.5};
60 }
61
63 return {FdmSchemeDesc::HundsdorferType, 1.0 - std::sqrt(2.0) / 2, 0.5};
64 }
65
67 return {FdmSchemeDesc::ExplicitEulerType, 0.0, 0.0};
68 }
69
71 return {FdmSchemeDesc::ImplicitEulerType, 0.0, 0.0};
72 }
73
75 return {FdmSchemeDesc::MethodOfLinesType, eps, relInitStepSize};
76 }
77
79
81 ext::shared_ptr<FdmLinearOpComposite> map,
83 const ext::shared_ptr<FdmStepConditionComposite>& condition,
84 const FdmSchemeDesc& schemeDesc)
85 : map_(std::move(map)), bcSet_(std::move(bcSet)),
86 condition_((condition) != nullptr ?
87 condition :
88 ext::make_shared<FdmStepConditionComposite>(
89 std::list<std::vector<Time> >(), FdmStepConditionComposite::Conditions())),
90 schemeDesc_(schemeDesc) {}
91
93 Time from, Time to,
94 Size steps, Size dampingSteps) {
95
96 const Time deltaT = from - to;
97 const Size allSteps = steps + dampingSteps;
98 const Time dampingTo = from - (deltaT*dampingSteps)/allSteps;
99
100 if ((dampingSteps != 0U) && schemeDesc_.type != FdmSchemeDesc::ImplicitEulerType) {
101 ImplicitEulerScheme implicitEvolver(map_, bcSet_);
103 dampingModel(implicitEvolver, condition_->stoppingTimes());
104 dampingModel.rollback(rhs, from, dampingTo,
105 dampingSteps, *condition_);
106 }
107
108 switch (schemeDesc_.type) {
110 {
112 map_, bcSet_);
114 hsModel(hsEvolver, condition_->stoppingTimes());
115 hsModel.rollback(rhs, dampingTo, to, steps, *condition_);
116 }
117 break;
119 {
122 dsModel(dsEvolver, condition_->stoppingTimes());
123 dsModel.rollback(rhs, dampingTo, to, steps, *condition_);
124 }
125 break;
127 {
130 cnModel(cnEvolver, condition_->stoppingTimes());
131 cnModel.rollback(rhs, dampingTo, to, steps, *condition_);
132
133 }
134 break;
136 {
138 map_, bcSet_);
140 csModel(csEvolver, condition_->stoppingTimes());
141 csModel.rollback(rhs, dampingTo, to, steps, *condition_);
142 }
143 break;
145 {
148 map_, bcSet_);
150 mcsModel(csEvolver, condition_->stoppingTimes());
151 mcsModel.rollback(rhs, dampingTo, to, steps, *condition_);
152 }
153 break;
155 {
156 ImplicitEulerScheme implicitEvolver(map_, bcSet_);
158 implicitModel(implicitEvolver, condition_->stoppingTimes());
159 implicitModel.rollback(rhs, from, to, allSteps, *condition_);
160 }
161 break;
163 {
164 ExplicitEulerScheme explicitEvolver(map_, bcSet_);
166 explicitModel(explicitEvolver, condition_->stoppingTimes());
167 explicitModel.rollback(rhs, dampingTo, to, steps, *condition_);
168 }
169 break;
171 {
172 MethodOfLinesScheme methodOfLines(
175 molModel(methodOfLines, condition_->stoppingTimes());
176 molModel.rollback(rhs, dampingTo, to, steps, *condition_);
177 }
178 break;
180 {
181 const FdmSchemeDesc trDesc
183
184 const ext::shared_ptr<CraigSneydScheme> hsEvolver(
185 ext::make_shared<CraigSneydScheme>(
186 trDesc.theta, trDesc.mu, map_, bcSet_));
187
190
192 trBDF2Model(trBDF2, condition_->stoppingTimes());
193 trBDF2Model.rollback(rhs, dampingTo, to, steps, *condition_);
194 }
195 break;
196 default:
197 QL_FAIL("Unknown scheme type");
198 }
199 }
200}
1-D array used in linear algebra.
Definition: array.hpp:52
void rollback(array_type &a, Time from, Time to, Size steps, Size dampingSteps)
FdmBackwardSolver(ext::shared_ptr< FdmLinearOpComposite > map, FdmBoundaryConditionSet bcSet, const ext::shared_ptr< FdmStepConditionComposite > &condition, const FdmSchemeDesc &schemeDesc)
const FdmBoundaryConditionSet bcSet_
const ext::shared_ptr< FdmLinearOpComposite > map_
const ext::shared_ptr< FdmStepConditionComposite > condition_
const FdmSchemeDesc schemeDesc_
Generic finite difference model.
void rollback(array_type &a, Time from, Time to, Size steps)
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
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
static FdmSchemeDesc MethodOfLines(Real eps=0.001, Real relInitStepSize=0.01)
static FdmSchemeDesc TrBDF2()
FdmSchemeDesc(FdmSchemeType type, Real theta, Real mu)
static FdmSchemeDesc CrankNicolson()
static FdmSchemeDesc Douglas()
static FdmSchemeDesc CraigSneyd()
static FdmSchemeDesc ModifiedCraigSneyd()
static FdmSchemeDesc ExplicitEuler()
static FdmSchemeDesc ImplicitEuler()
static FdmSchemeDesc ModifiedHundsdorfer()
const FdmSchemeType type
static FdmSchemeDesc Hundsdorfer()