QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdm1dimsolver.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/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
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 x_(solverDesc.mesher->layout()->size()), initialValues_(solverDesc.mesher->layout()->size()),
43 resultValues_(solverDesc.mesher->layout()->size()) {
44
45 for (const auto& iter : *solverDesc.mesher->layout()) {
46 initialValues_[iter.index()]
47 = solverDesc_.calculator->avgInnerValue(iter,
48 solverDesc.maturity);
49 x_[iter.index()] = solverDesc.mesher->location(iter, 0);
50 }
51 }
52
53
55 Array rhs(initialValues_.size());
56 std::copy(initialValues_.begin(), initialValues_.end(), rhs.begin());
57
59 .rollback(rhs, solverDesc_.maturity, 0.0,
61
62 std::copy(rhs.begin(), rhs.end(), resultValues_.begin());
63 interpolation_ = ext::make_shared<MonotonicCubicNaturalSpline>(x_.begin(), x_.end(),
65 }
66
68 calculate();
69 return (*interpolation_)(x);
70 }
71
73 if (conditions_->stoppingTimes().front() == 0.0)
74 return Null<Real>();
75
76 calculate();
77 Array thetaValues(resultValues_.size());
78
79 const Array& rhs = thetaCondition_->getValues();
80 std::copy(rhs.begin(), rhs.end(), thetaValues.begin());
81
83 x_.begin(), x_.end(), thetaValues.begin())(x);
84 return ( temp - interpolateAt(x) ) / thetaCondition_->getTime();
85 }
86
87
89 calculate();
90 return interpolation_->derivative(x);
91 }
92
94 calculate();
95 return interpolation_->secondDerivative(x);
96 }
97}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
void performCalculations() const override
std::vector< Real > initialValues_
const ext::shared_ptr< FdmStepConditionComposite > conditions_
Real derivativeX(Real x) const
ext::shared_ptr< CubicInterpolation > interpolation_
const ext::shared_ptr< FdmSnapshotCondition > thetaCondition_
const FdmSolverDesc solverDesc_
Real thetaAt(Real x) const
Real derivativeXX(Real x) const
Fdm1DimSolver(const FdmSolverDesc &solverDesc, const FdmSchemeDesc &schemeDesc, ext::shared_ptr< FdmLinearOpComposite > op)
Real interpolateAt(Real x) 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
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