QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdm2dimsolver.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 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/math/interpolations/bicubicsplineinterpolation.hpp>
21#include <ql/methods/finitedifferences/finitedifferencemodel.hpp>
22#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
23#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24#include <ql/methods/finitedifferences/solvers/fdm2dimsolver.hpp>
25#include <ql/methods/finitedifferences/stepconditions/fdmsnapshotcondition.hpp>
26#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
27#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
28#include <utility>
29
30namespace QuantLib {
31
33 const FdmSchemeDesc& schemeDesc,
34 ext::shared_ptr<FdmLinearOpComposite> op)
35 : solverDesc_(solverDesc), schemeDesc_(schemeDesc), op_(std::move(op)),
36 thetaCondition_(ext::make_shared<FdmSnapshotCondition>(
37 0.99 * std::min(1.0 / 365.0,
38 solverDesc.condition->stoppingTimes().empty() ?
39 solverDesc.maturity :
40 solverDesc.condition->stoppingTimes().front()))),
41 conditions_(FdmStepConditionComposite::joinConditions(thetaCondition_, solverDesc.condition)),
42 initialValues_(solverDesc.mesher->layout()->size()),
43 resultValues_(solverDesc.mesher->layout()->dim()[1], solverDesc.mesher->layout()->dim()[0]) {
44
45 x_.reserve(solverDesc.mesher->layout()->dim()[0]);
46 y_.reserve(solverDesc.mesher->layout()->dim()[1]);
47
48 for (const auto& iter : *solverDesc.mesher->layout()) {
49 initialValues_[iter.index()]
50 = solverDesc_.calculator->avgInnerValue(iter,
51 solverDesc.maturity);
52
53 if (iter.coordinates()[1] == 0U) {
54 x_.push_back(solverDesc.mesher->location(iter, 0));
55 }
56 if (iter.coordinates()[0] == 0U) {
57 y_.push_back(solverDesc.mesher->location(iter, 1));
58 }
59 }
60 }
61
62
64 Array rhs(initialValues_.size());
65 std::copy(initialValues_.begin(), initialValues_.end(), rhs.begin());
66
68 .rollback(rhs, solverDesc_.maturity, 0.0,
70
71 std::copy(rhs.begin(), rhs.end(), resultValues_.begin());
72 interpolation_ = ext::make_shared<BicubicSpline>(x_.begin(), x_.end(),
73 y_.begin(), y_.end(),
75 }
76
78 calculate();
79 return (*interpolation_)(x, y);
80 }
81
83 if (conditions_->stoppingTimes().front() == 0.0)
84 return Null<Real>();
85
86 calculate();
88
89 const Array& rhs = thetaCondition_->getValues();
90 std::copy(rhs.begin(), rhs.end(), thetaValues.begin());
91
92 return (BicubicSpline(x_.begin(), x_.end(), y_.begin(), y_.end(),
93 thetaValues)(x, y) - interpolateAt(x, y))
94 / thetaCondition_->getTime();
95 }
96
97
99 calculate();
100 return interpolation_->derivativeX(x, y);
101 }
102
104 calculate();
105 return interpolation_->derivativeY(x, y);
106 }
107
109 calculate();
110 return interpolation_->secondDerivativeX(x, y);
111 }
112
114 calculate();
115 return interpolation_->secondDerivativeY(x, y);
116 }
117
119 calculate();
120 return interpolation_->derivativeXY(x, y);
121 }
122
123}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
const_iterator begin() const
Definition: array.hpp:503
bicubic-spline interpolation between discrete points
void performCalculations() const override
std::vector< Real > initialValues_
Real derivativeXX(Real x, Real y) const
Real interpolateAt(Real x, Real y) const
const ext::shared_ptr< FdmStepConditionComposite > conditions_
const ext::shared_ptr< FdmSnapshotCondition > thetaCondition_
ext::shared_ptr< BicubicSpline > interpolation_
Real thetaAt(Real x, Real y) const
const FdmSolverDesc solverDesc_
std::vector< Real > y_
Real derivativeY(Real x, Real y) const
Real derivativeX(Real x, Real y) const
Real derivativeXY(Real x, Real y) const
Fdm2DimSolver(const FdmSolverDesc &solverDesc, const FdmSchemeDesc &schemeDesc, ext::shared_ptr< FdmLinearOpComposite > op)
Real derivativeYY(Real x, Real y) const
std::vector< Real > x_
const ext::shared_ptr< FdmLinearOpComposite > op_
const FdmSchemeDesc schemeDesc_
void rollback(array_type &a, Time from, Time to, Size steps, Size dampingSteps)
virtual void calculate() const
Definition: lazyobject.hpp:253
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_iterator begin() const
Definition: matrix.hpp:327
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
template class providing a null value for a given type.
Definition: null.hpp:76
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
STL namespace.
const ext::shared_ptr< FdmInnerValueCalculator > calculator
const FdmBoundaryConditionSet bcSet
const ext::shared_ptr< FdmMesher > mesher