QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
pde.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 Joseph Wang
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/*! \file pde.hpp
21 \brief General class for one dimensional PDE's
22*/
23
24#ifndef quantlib_pde_hpp
25#define quantlib_pde_hpp
26
27#include <ql/math/array.hpp>
30
31namespace QuantLib {
33 public:
34 virtual ~PdeSecondOrderParabolic() = default;
35 virtual Real diffusion(Time t, Real x) const = 0;
36 virtual Real drift(Time t, Real x) const = 0;
37 virtual Real discount(Time t, Real x) const = 0;
38 virtual void
40 for (Size i = 1; i < tg.size() - 1; i++) {
41 Real sigma = diffusion(t, tg.grid(i));
42 Real nu = drift(t, tg.grid(i));
43 Real r = discount(t, tg.grid(i));
44 Real sigma2 = sigma * sigma;
45
46 Real pd = -(sigma2 / tg.dxm(i) - nu) / tg.dx(i);
47 Real pu = -(sigma2 / tg.dxp(i) + nu) / tg.dx(i);
48 Real pm = sigma2 / (tg.dxm(i) * tg.dxp(i)) + r;
49 L.setMidRow(i, pd, pm, pu);
50 }
51 }
52 };
53
54 template <class PdeClass>
56 public:
57 PdeConstantCoeff(const typename PdeClass::argument_type &process,
58 Time t, Real x) {
59 PdeClass pde(process);
60 diffusion_ = pde.diffusion(t, x);
61 drift_ = pde.drift(t, x);
62 discount_ = pde.discount(t, x);
63 }
64 Real diffusion(Time, Real) const override { return diffusion_; }
65 Real drift(Time, Real) const override { return drift_; }
66 Real discount(Time, Real) const override { return discount_; }
67
68 private:
72 };
73
74 template <class PdeClass>
76 public:
77 template <class T>
78 GenericTimeSetter(const Array &grid, T process) :
79 grid_(grid), pde_(process) {}
80 void setTime(Time t, TridiagonalOperator& L) const override {
81 pde_.generateOperator(t, grid_, L);
82 }
83
84 private:
85 typename PdeClass::grid_type grid_;
86 PdeClass pde_;
87 };
88
89 template <class PdeClass>
91 public:
92 template <class T>
93 PdeOperator(const Array& grid,
94 T process,
95 Time residualTime = 0.0) :
98 ext::shared_ptr<GenericTimeSetter<PdeClass> >(
99 new GenericTimeSetter<PdeClass>(grid, process));
100 setTime(residualTime);
101 }
102 };
103}
104
105
106#endif
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
PdeClass::grid_type grid_
Definition: pde.hpp:85
GenericTimeSetter(const Array &grid, T process)
Definition: pde.hpp:78
void setTime(Time t, TridiagonalOperator &L) const override
Definition: pde.hpp:80
Real drift(Time, Real) const override
Definition: pde.hpp:65
Real diffusion(Time, Real) const override
Definition: pde.hpp:64
PdeConstantCoeff(const typename PdeClass::argument_type &process, Time t, Real x)
Definition: pde.hpp:57
Real discount(Time, Real) const override
Definition: pde.hpp:66
PdeOperator(const Array &grid, T process, Time residualTime=0.0)
Definition: pde.hpp:93
virtual Real discount(Time t, Real x) const =0
virtual Real drift(Time t, Real x) const =0
virtual void generateOperator(Time t, const TransformedGrid &tg, TridiagonalOperator &L) const
Definition: pde.hpp:39
virtual ~PdeSecondOrderParabolic()=default
virtual Real diffusion(Time t, Real x) const =0
encapsulation of time-setting logic
Base implementation for tridiagonal operator.
ext::shared_ptr< TimeSetter > timeSetter_
void setMidRow(Size, Real, Real, Real)
const DefaultType & t
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Real sigma
Definition: any.hpp:35
ext::shared_ptr< YieldTermStructure > r
Real nu
Definition: sabr.cpp:200
encapuslates a grid
tridiagonal operator