|
| 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 | + |
| 19 | +#include <qle/models/lgmfdsolver.hpp> |
| 20 | +#include <qle/methods/fdmlgmop.hpp> |
| 21 | + |
| 22 | +#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
| 23 | +#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp> |
| 24 | + |
| 25 | + |
| 26 | +namespace QuantExt { |
| 27 | + |
| 28 | +LgmFdSolver::LgmFdSolver(const boost::shared_ptr<LinearGaussMarkovModel>& model, const Real maxTime, |
| 29 | + const Size stateGridPoints, const Size timeStepsPerYear, const Real mesherEpsilon) |
| 30 | + : model_(model), maxTime_(maxTime), stateGridPoints_(stateGridPoints), timeStepsPerYear_(timeStepsPerYear), |
| 31 | + mesherEpsilon_(mesherEpsilon) { |
| 32 | + mesher_ = boost::make_shared<FdmMesherComposite>(boost::make_shared<FdmSimpleProcess1dMesher>( |
| 33 | + stateGridPoints, boost::dynamic_pointer_cast<StochasticProcess1D>(model->stateProcess()), maxTime_, |
| 34 | + timeStepsPerYear_, mesherEpsilon_)); |
| 35 | + mesherLocations_ = RandomVariable(mesher_->locations(0)); |
| 36 | + operator_ = boost::make_shared<QuantExt::FdmLgmOp>( |
| 37 | + mesher_, boost::dynamic_pointer_cast<StochasticProcess1D>(model->stateProcess())); |
| 38 | + solver_ = boost::make_shared<FdmBackwardSolver>( |
| 39 | + operator_, std::vector<boost::shared_ptr<BoundaryCondition<FdmLinearOp>>>(), nullptr, FdmSchemeDesc::Douglas()); |
| 40 | +} |
| 41 | + |
| 42 | +Size LgmFdSolver::gridSize() const { return stateGridPoints_; } |
| 43 | + |
| 44 | +RandomVariable LgmFdSolver::stateGrid() const { return mesherLocations_; } |
| 45 | + |
| 46 | +const boost::shared_ptr<LinearGaussMarkovModel>& LgmFdSolver::model() const { return model_; } |
| 47 | + |
| 48 | +RandomVariable LgmFdSolver::rollback(const RandomVariable& v, const Real t1, const Real t0) const { |
| 49 | + Size steps = std::max<Size>(1, static_cast<Size>(static_cast<double>(timeStepsPerYear_) * (t1 - t0) + 0.5)); |
| 50 | + Array workingArray(v.size()); |
| 51 | + v.copyToArray(workingArray); |
| 52 | + solver_->rollback(workingArray, t1, t0, steps, 0); |
| 53 | + return RandomVariable(workingArray); |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace QuantExt |
0 commit comments