QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
fdmsimplestoragecondition.cpp
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) 2011 Klaus Spanderen
5 Copyright (C) 2014 Ralph Schreyer
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 <algorithm>
25#include <utility>
26
27namespace QuantLib {
28
30 std::vector<Time> exerciseTimes,
31 ext::shared_ptr<FdmMesher> mesher,
32 ext::shared_ptr<FdmInnerValueCalculator> calculator,
33 Real changeRate)
34 : exerciseTimes_(std::move(exerciseTimes)), mesher_(std::move(mesher)),
35 calculator_(std::move(calculator)), changeRate_(changeRate) {
36
37 x_.reserve(mesher_->layout()->dim()[0]);
38 y_.reserve(mesher_->layout()->dim()[1]);
39
40 for (const auto& iter : *mesher_->layout()) {
41 if (iter.coordinates()[1] == 0U) {
42 x_.push_back(mesher_->location(iter, 0));
43 }
44 if (iter.coordinates()[0] == 0U) {
45 y_.push_back(mesher_->location(iter, 1));
46 }
47 }
48 }
49
51 const auto iter
52 = std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t);
53
54 if (iter != exerciseTimes_.end()) {
55 Array retVal(a.size());
56
57 Matrix m(y_.size(), x_.size());
58 std::copy(a.begin(), a.end(), m.begin());
59 BilinearInterpolation interpl(x_.begin(), x_.end(),
60 y_.begin(), y_.end(), m);
61
62 QL_REQUIRE(mesher_->layout()->size() == a.size(),
63 "inconsistent array dimensions");
64
65 for (const auto& iter : *mesher_->layout()) {
66 const std::vector<Size>& coor = iter.coordinates();
67 const Real x = x_[coor[0]];
68 const Real y = y_[coor[1]];
69
70 const Real price = calculator_->innerValue(iter, t);
71
72 const Real maxWithDraw = std::min(y-y_.front(), changeRate_);
73 const Real sellPrice = interpl(x, y-maxWithDraw);
74
75 const Real maxInject = std::min(y_.back()-y, changeRate_);
76 const Real buyPrice = interpl(x, y+maxInject);
77
78 // bang-bang-wait strategy
79 Real currentValue = std::max({
80 a[iter.index()],
81 Real(buyPrice - price*maxInject),
82 Real(sellPrice + price*maxWithDraw)
83 });
84
85 // check if intermediate grid points give a better value
86 auto yIter = std::upper_bound(y_.begin(), y_.end(), y - maxWithDraw);
87
88 while (yIter != y_.end() && *yIter < y + maxInject) {
89 if (*yIter != y) {
90 const Real change = *yIter - y;
91 const Real storagePrice(interpl(x, *yIter));
92
93 currentValue = std::max(currentValue,
94 storagePrice - change*price);
95 }
96 ++yIter;
97 }
98
99 retVal[iter.index()] = currentValue;
100 }
101 a = retVal;
102 }
103 }
104}
bilinear interpolation between discrete points
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:499
Size size() const
dimension of the array
Definition: array.hpp:483
const_iterator begin() const
Definition: array.hpp:491
bilinear interpolation between discrete points
const ext::shared_ptr< FdmMesher > mesher_
const ext::shared_ptr< FdmInnerValueCalculator > calculator_
void applyTo(Array &a, Time t) const override
FdmSimpleStorageCondition(std::vector< Time > exerciseTimes, ext::shared_ptr< FdmMesher > mesher, ext::shared_ptr< FdmInnerValueCalculator > calculator, Real changeRate)
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_iterator begin() const
Definition: matrix.hpp:327
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
const ext::shared_ptr< CEVCalculator > calculator_
memory layout of a fdm linear operator
simple storage step condition
const ext::shared_ptr< FdmMesher > mesher_
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:37
STL namespace.