/src/quantlib/ql/methods/finitedifferences/solvers/fdm1dimsolver.cpp
Line | Count | Source (jump to first uncovered line) |
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/math/interpolations/cubicinterpolation.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/fdm1dimsolver.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 | | |
30 | | namespace QuantLib { |
31 | | |
32 | | Fdm1DimSolver::Fdm1DimSolver(const FdmSolverDesc& solverDesc, |
33 | | const FdmSchemeDesc& schemeDesc, |
34 | | ext::shared_ptr<FdmLinearOpComposite> op) |
35 | 0 | : solverDesc_(solverDesc), schemeDesc_(schemeDesc), op_(std::move(op)), |
36 | 0 | thetaCondition_(ext::make_shared<FdmSnapshotCondition>( |
37 | 0 | 0.99 * std::min(1.0 / 365.0, |
38 | 0 | solverDesc.condition->stoppingTimes().empty() ? |
39 | 0 | solverDesc.maturity : |
40 | 0 | solverDesc.condition->stoppingTimes().front()))), |
41 | 0 | conditions_(FdmStepConditionComposite::joinConditions(thetaCondition_, solverDesc.condition)), |
42 | 0 | x_(solverDesc.mesher->layout()->size()), initialValues_(solverDesc.mesher->layout()->size()), |
43 | 0 | resultValues_(solverDesc.mesher->layout()->size()) { |
44 | |
|
45 | 0 | for (const auto& iter : *solverDesc.mesher->layout()) { |
46 | 0 | initialValues_[iter.index()] |
47 | 0 | = solverDesc_.calculator->avgInnerValue(iter, |
48 | 0 | solverDesc.maturity); |
49 | 0 | x_[iter.index()] = solverDesc.mesher->location(iter, 0); |
50 | 0 | } |
51 | 0 | } Unexecuted instantiation: QuantLib::Fdm1DimSolver::Fdm1DimSolver(QuantLib::FdmSolverDesc const&, QuantLib::FdmSchemeDesc const&, boost::shared_ptr<QuantLib::FdmLinearOpComposite>) Unexecuted instantiation: QuantLib::Fdm1DimSolver::Fdm1DimSolver(QuantLib::FdmSolverDesc const&, QuantLib::FdmSchemeDesc const&, boost::shared_ptr<QuantLib::FdmLinearOpComposite>) |
52 | | |
53 | | |
54 | 0 | void Fdm1DimSolver::performCalculations() const { |
55 | 0 | Array rhs(initialValues_.size()); |
56 | 0 | std::copy(initialValues_.begin(), initialValues_.end(), rhs.begin()); |
57 | |
|
58 | 0 | FdmBackwardSolver(op_, solverDesc_.bcSet, conditions_, schemeDesc_) |
59 | 0 | .rollback(rhs, solverDesc_.maturity, 0.0, |
60 | 0 | solverDesc_.timeSteps, solverDesc_.dampingSteps); |
61 | |
|
62 | 0 | std::copy(rhs.begin(), rhs.end(), resultValues_.begin()); |
63 | 0 | interpolation_ = ext::make_shared<MonotonicCubicNaturalSpline>(x_.begin(), x_.end(), |
64 | 0 | resultValues_.begin()); |
65 | 0 | } |
66 | | |
67 | 0 | Real Fdm1DimSolver::interpolateAt(Real x) const { |
68 | 0 | calculate(); |
69 | 0 | return (*interpolation_)(x); |
70 | 0 | } |
71 | | |
72 | 0 | Real Fdm1DimSolver::thetaAt(Real x) const { |
73 | 0 | if (conditions_->stoppingTimes().front() == 0.0) |
74 | 0 | return Null<Real>(); |
75 | | |
76 | 0 | calculate(); |
77 | 0 | Array thetaValues(resultValues_.size()); |
78 | |
|
79 | 0 | const Array& rhs = thetaCondition_->getValues(); |
80 | 0 | std::copy(rhs.begin(), rhs.end(), thetaValues.begin()); |
81 | |
|
82 | 0 | Real temp = MonotonicCubicNaturalSpline( |
83 | 0 | x_.begin(), x_.end(), thetaValues.begin())(x); |
84 | 0 | return ( temp - interpolateAt(x) ) / thetaCondition_->getTime(); |
85 | 0 | } |
86 | | |
87 | | |
88 | 0 | Real Fdm1DimSolver::derivativeX(Real x) const { |
89 | 0 | calculate(); |
90 | 0 | return interpolation_->derivative(x); |
91 | 0 | } |
92 | | |
93 | 0 | Real Fdm1DimSolver::derivativeXX(Real x) const { |
94 | 0 | calculate(); |
95 | 0 | return interpolation_->secondDerivative(x); |
96 | 0 | } |
97 | | } |