Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
lgmfdsolver.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21
22#include <ql/math/interpolations/cubicinterpolation.hpp>
23#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
25
26namespace QuantExt {
27
28LgmFdSolver::LgmFdSolver(const QuantLib::ext::shared_ptr<LinearGaussMarkovModel>& model, const Real maxTime,
29 const QuantLib::FdmSchemeDesc scheme, const Size stateGridPoints, const Size timeStepsPerYear,
30 const Real mesherEpsilon)
31 : model_(model), maxTime_(maxTime), scheme_(scheme), stateGridPoints_(stateGridPoints),
32 timeStepsPerYear_(timeStepsPerYear), mesherEpsilon_(mesherEpsilon) {
33 mesher_ = QuantLib::ext::make_shared<FdmMesherComposite>(QuantLib::ext::make_shared<FdmSimpleProcess1dMesher>(
34 stateGridPoints, QuantLib::ext::dynamic_pointer_cast<StochasticProcess1D>(model->stateProcess()), maxTime_,
37 operator_ = QuantLib::ext::make_shared<QuantExt::FdmLgmOp>(
38 mesher_, QuantLib::ext::dynamic_pointer_cast<StochasticProcess1D>(model->stateProcess()));
39 solver_ = QuantLib::ext::make_shared<FdmBackwardSolver>(
40 operator_, std::vector<QuantLib::ext::shared_ptr<BoundaryCondition<FdmLinearOp>>>(), nullptr, scheme_);
41}
42
44
46
47const QuantLib::ext::shared_ptr<LinearGaussMarkovModel>& LgmFdSolver::model() const { return model_; }
48
49RandomVariable LgmFdSolver::rollback(const RandomVariable& v, const Real t1, const Real t0, Size steps) const {
50 if (QuantLib::close_enough(t0, t1) || v.deterministic())
51 return v;
52 QL_REQUIRE(t0 < t1, "LgmCFdSolver::rollback(): t0 (" << t0 << ") < t1 (" << t1 << ") required.");
53 if (steps == Null<Size>())
54 steps = std::max<Size>(1, static_cast<Size>(static_cast<double>(timeStepsPerYear_) * (t1 - t0) + 0.5));
55 Array workingArray(v.size());
56 v.copyToArray(workingArray);
57 solver_->rollback(workingArray, t1, t0, steps, 0);
58 if (QuantLib::close_enough(t0, 0.0)) {
59 Array x = mesher_->locations(0);
60 MonotonicCubicNaturalSpline interpolation(x.begin(), x.end(), workingArray.begin());
61 interpolation.enableExtrapolation();
62 return RandomVariable(gridSize(), interpolation(0.0));
63 } else {
64 return RandomVariable(workingArray);
65 }
66}
67
68} // namespace QuantExt
QuantLib::ext::shared_ptr< FdmLinearOpComposite > operator_
Definition: lgmfdsolver.hpp:57
RandomVariable rollback(const RandomVariable &v, const Real t1, const Real t0, Size steps=Null< Size >()) const override
Definition: lgmfdsolver.cpp:49
const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model() const override
Definition: lgmfdsolver.cpp:47
Size gridSize() const override
Definition: lgmfdsolver.cpp:43
QuantLib::ext::shared_ptr< FdmBackwardSolver > solver_
Definition: lgmfdsolver.hpp:58
QuantLib::ext::shared_ptr< FdmMesher > mesher_
Definition: lgmfdsolver.hpp:56
RandomVariable stateGrid(const Real t) const override
Definition: lgmfdsolver.cpp:45
QuantLib::FdmSchemeDesc scheme_
Definition: lgmfdsolver.hpp:51
QuantLib::ext::shared_ptr< LinearGaussMarkovModel > model_
Definition: lgmfdsolver.hpp:49
LgmFdSolver(const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > &model, const Real maxTime=50.0, const QuantLib::FdmSchemeDesc scheme=QuantLib::FdmSchemeDesc::Douglas(), const Size stateGridPoints=64, const Size timeStepsPerYear=24, const Real mesherEpsilon=1E-4)
Definition: lgmfdsolver.cpp:28
RandomVariable mesherLocations_
Definition: lgmfdsolver.hpp:60
finite difference operator LGM model
void copyToArray(QuantLib::Array &array) const
std::vector< Size > steps