QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmsimplestoragecondition.cpp
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 <ql/math/interpolations/bilinearinterpolation.hpp>
22#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
23#include <ql/methods/finitedifferences/stepconditions/fdmsimplestoragecondition.hpp>
24#include <utility>
25
26namespace QuantLib {
27
29 std::vector<Time> exerciseTimes,
30 ext::shared_ptr<FdmMesher> mesher,
31 ext::shared_ptr<FdmInnerValueCalculator> calculator,
32 Real changeRate)
33 : exerciseTimes_(std::move(exerciseTimes)), mesher_(std::move(mesher)),
34 calculator_(std::move(calculator)), changeRate_(changeRate) {
35
36 x_.reserve(mesher_->layout()->dim()[0]);
37 y_.reserve(mesher_->layout()->dim()[1]);
38
39 for (const auto& iter : *mesher_->layout()) {
40 if (iter.coordinates()[1] == 0U) {
41 x_.push_back(mesher_->location(iter, 0));
42 }
43 if (iter.coordinates()[0] == 0U) {
44 y_.push_back(mesher_->location(iter, 1));
45 }
46 }
47 }
48
50 const std::vector<Time>::const_iterator iter
51 = std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t);
52
53 if (iter != exerciseTimes_.end()) {
54 Array retVal(a.size());
55
56 Matrix m(y_.size(), x_.size());
57 std::copy(a.begin(), a.end(), m.begin());
58 BilinearInterpolation interpl(x_.begin(), x_.end(),
59 y_.begin(), y_.end(), m);
60
61 QL_REQUIRE(mesher_->layout()->size() == a.size(),
62 "inconsistent array dimensions");
63
64 for (const auto& iter : *mesher_->layout()) {
65 const std::vector<Size>& coor = iter.coordinates();
66 const Real x = x_[coor[0]];
67 const Real y = y_[coor[1]];
68
69 const Real price = calculator_->innerValue(iter, t);
70
71 const Real maxWithDraw = std::min(y-y_.front(), changeRate_);
72 const Real sellPrice = interpl(x, y-maxWithDraw);
73
74 const Real maxInject = std::min(y_.back()-y, changeRate_);
75 const Real buyPrice = interpl(x, y+maxInject);
76
77 // bang-bang-wait strategy
78 Real currentValue = std::max(a[iter.index()],
79 std::max(buyPrice - price*maxInject,
80 sellPrice + price*maxWithDraw));
81
82 // check if intermediate grid points give a better value
83 auto yIter = std::upper_bound(y_.begin(), y_.end(), y - maxWithDraw);
84
85 while (yIter != y_.end() && *yIter < y + maxInject) {
86 if (*yIter != y) {
87 const Real change = *yIter - y;
88 const Real storagePrice(interpl(x, *yIter));
89
90 currentValue = std::max(currentValue,
91 storagePrice - change*price);
92 }
93 ++yIter;
94 }
95
96 retVal[iter.index()] = currentValue;
97 }
98 a = retVal;
99 }
100 }
101}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
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
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
STL namespace.