QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmlocalvolfwdop.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Johannes Göttker-Schnetmann
5 Copyright (C) 2015 Klaus Spanderen
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/math/functional.hpp>
22#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
23#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlocalvolfwdop.hpp>
25#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
26#include <utility>
27
28namespace QuantLib {
29
30 FdmLocalVolFwdOp::FdmLocalVolFwdOp(const ext::shared_ptr<FdmMesher>& mesher,
31 const ext::shared_ptr<Quote>& spot,
32 ext::shared_ptr<YieldTermStructure> rTS,
33 ext::shared_ptr<YieldTermStructure> qTS,
34 const ext::shared_ptr<LocalVolTermStructure>& localVol,
35 Size direction)
36 : mesher_(mesher), rTS_(std::move(rTS)), qTS_(std::move(qTS)), localVol_(localVol),
37 x_((localVol) != nullptr ? Array(Exp(mesher->locations(direction))) : Array()),
38 dxMap_(FirstDerivativeOp(direction, mesher)), dxxMap_(SecondDerivativeOp(direction, mesher)),
39 mapT_(direction, mesher), direction_(direction) {}
40
42 const Rate r = rTS_->forwardRate(t1, t2, Continuous).rate();
43 const Rate q = qTS_->forwardRate(t1, t2, Continuous).rate();
44
45 Array v(mesher_->layout()->size());
46 for (const auto& iter : *mesher_->layout()) {
47 const Size i = iter.index();
48
49 v[i] = squared(localVol_->localVol(0.5*(t1+t2), x_[i], true));
50 }
51 mapT_.axpyb(Array(1, 1.0), dxMap_.multR(- r + q + 0.5*v),
52 dxxMap_.multR(0.5*v), Array(1, 0.0));
53 }
54
55 Size FdmLocalVolFwdOp::size() const { return 1U; }
56
58 return mapT_.apply(u);
59 }
60
62 Size direction, const Array& r) const {
63 if (direction == direction_)
64 return mapT_.apply(r);
65 else {
66 return Array(r.size(), 0.0);
67 }
68 }
69
71 return Array(r.size(), 0.0);
72 }
73
75 Size direction, const Array& r, Real dt) const {
76 if (direction == direction_)
77 return mapT_.solve_splitting(r, dt, 1.0);
78 else {
79 return r;
80 }
81 }
82
84 const Array& r, Real dt) const {
85 return solve_splitting(direction_, r, dt);
86 }
87
88 std::vector<SparseMatrix> FdmLocalVolFwdOp::toMatrixDecomp() const {
89 return std::vector<SparseMatrix>(1, mapT_.toMatrix());
90 }
91
92}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const override
Array apply_direction(Size direction, const Array &r) const override
Array preconditioner(const Array &r, Real s) const override
std::vector< SparseMatrix > toMatrixDecomp() const override
const TripleBandLinearOp dxxMap_
void setTime(Time t1, Time t2) override
Time is required.
Array apply_mixed(const Array &r) const override
const ext::shared_ptr< YieldTermStructure > qTS_
const FirstDerivativeOp dxMap_
FdmLocalVolFwdOp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< Quote > &spot, ext::shared_ptr< YieldTermStructure > rTS, ext::shared_ptr< YieldTermStructure > qTS, const ext::shared_ptr< LocalVolTermStructure > &localVol, Size direction=0)
const ext::shared_ptr< FdmMesher > mesher_
Array solve_splitting(Size direction, const Array &r, Real s) const override
const ext::shared_ptr< YieldTermStructure > rTS_
Array apply(const Array &r) const override
const ext::shared_ptr< LocalVolTermStructure > localVol_
SparseMatrix toMatrix() const override
TripleBandLinearOp multR(const Array &u) const
Array solve_splitting(const Array &r, Real a, Real b=1.0) const
void axpyb(const Array &a, const TripleBandLinearOp &x, const TripleBandLinearOp &y, const Array &b)
Array apply(const Array &r) const override
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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
T squared(T x)
Definition: functional.hpp:37
Array Exp(const Array &v)
Definition: array.hpp:875
STL namespace.