QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdm3dimsolver.cpp
Go to the documentation of this file.
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
29#include <utility>
30
31namespace QuantLib {
32
34 const FdmSchemeDesc& schemeDesc,
35 ext::shared_ptr<FdmLinearOpComposite> op)
36 : solverDesc_(solverDesc), schemeDesc_(schemeDesc), op_(std::move(op)),
37 thetaCondition_(ext::make_shared<FdmSnapshotCondition>(
38 0.99 * std::min(1.0 / 365.0,
39 solverDesc.condition->stoppingTimes().empty() ?
40 solverDesc.maturity :
41 solverDesc.condition->stoppingTimes().front()))),
42 conditions_(FdmStepConditionComposite::joinConditions(thetaCondition_, solverDesc.condition)),
43 initialValues_(solverDesc.mesher->layout()->size()),
44 resultValues_(
45 solverDesc.mesher->layout()->dim()[2],
46 Matrix(solverDesc.mesher->layout()->dim()[1], solverDesc.mesher->layout()->dim()[0])),
47 interpolation_(solverDesc.mesher->layout()->dim()[2]) {
48
49 x_.reserve(solverDesc.mesher->layout()->dim()[0]);
50 y_.reserve(solverDesc.mesher->layout()->dim()[1]);
51 z_.reserve(solverDesc.mesher->layout()->dim()[2]);
52
53 for (const auto& iter : *solverDesc.mesher->layout()) {
54 initialValues_[iter.index()]
55 = solverDesc.calculator->avgInnerValue(iter,
56 solverDesc.maturity);
57
58
59 if ((iter.coordinates()[1] == 0U) && (iter.coordinates()[2] == 0U)) {
60 x_.push_back(solverDesc.mesher->location(iter, 0));
61 }
62 if ((iter.coordinates()[0] == 0U) && (iter.coordinates()[2] == 0U)) {
63 y_.push_back(solverDesc.mesher->location(iter, 1));
64 }
65 if ((iter.coordinates()[0] == 0U) && (iter.coordinates()[1] == 0U)) {
66 z_.push_back(solverDesc.mesher->location(iter, 2));
67 }
68 }
69 }
70
72 Array rhs(initialValues_.size());
73 std::copy(initialValues_.begin(), initialValues_.end(), rhs.begin());
74
76 .rollback(rhs, solverDesc_.maturity, 0.0,
78
79 for (Size i=0; i < z_.size(); ++i) {
80 std::copy(rhs.begin()+i *y_.size()*x_.size(),
81 rhs.begin()+(i+1)*y_.size()*x_.size(),
82 resultValues_[i].begin());
83
84 interpolation_[i] = ext::make_shared<BicubicSpline>(x_.begin(), x_.end(),
85 y_.begin(), y_.end(),
86 resultValues_[i]);
87 }
88 }
89
91 calculate();
92
93 Array zArray(z_.size());
94 for (Size i=0; i < z_.size(); ++i) {
95 zArray[i] = (*interpolation_[i])(x, y);
96 }
97 return MonotonicCubicNaturalSpline(z_.begin(), z_.end(),
98 zArray.begin())(z);
99 }
100
102 if (conditions_->stoppingTimes().front() == 0.0)
103 return Null<Real>();
104
105 calculate();
106
107 const Array& rhs = thetaCondition_->getValues();
108 std::vector<Matrix> thetaValues(z_.size(), Matrix(y_.size(),x_.size()));
109 for (Size i=0; i < z_.size(); ++i) {
110 std::copy(rhs.begin()+i *y_.size()*x_.size(),
111 rhs.begin()+(i+1)*y_.size()*x_.size(),
112 thetaValues[i].begin());
113 }
114
115 Array zArray(z_.size());
116 for (Size i=0; i < z_.size(); ++i) {
117 zArray[i] = BicubicSpline(x_.begin(),x_.end(),
118 y_.begin(),y_.end(), thetaValues[i])(x,y);
119 }
120
121 return (MonotonicCubicNaturalSpline(z_.begin(), z_.end(),
122 zArray.begin())(z)
123 - interpolateAt(x, y, z)) / thetaCondition_->getTime();
124 }
125}
bicubic spline interpolation between discrete points
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator begin() const
Definition: array.hpp:503
bicubic-spline interpolation between discrete points
void performCalculations() const override
std::vector< Real > initialValues_
std::vector< Matrix > resultValues_
const ext::shared_ptr< FdmStepConditionComposite > conditions_
const ext::shared_ptr< FdmSnapshotCondition > thetaCondition_
std::vector< Real > z_
Real interpolateAt(Real x, Real y, Rate z) const
const FdmSolverDesc solverDesc_
std::vector< Real > y_
Real thetaAt(Real x, Real y, Rate z) const
std::vector< ext::shared_ptr< BicubicSpline > > interpolation_
Fdm3DimSolver(const FdmSolverDesc &solverDesc, const FdmSchemeDesc &schemeDesc, ext::shared_ptr< FdmLinearOpComposite > op)
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
template class providing a null value for a given type.
Definition: null.hpp:76
cubic interpolation between discrete points
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
mesher for a fdm grid
step condition for value inspection
composite of fdm step conditions
generic finite difference model
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
const ext::shared_ptr< FdmInnerValueCalculator > calculator
const FdmBoundaryConditionSet bcSet
const ext::shared_ptr< FdmMesher > mesher