/src/quantlib/ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2010, 2014 Klaus Spanderen |
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 | | <https://www.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 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
21 | | #include <ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.hpp> |
22 | | #include <utility> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | FdmSimpleSwingCondition::FdmSimpleSwingCondition( |
27 | | std::vector<Time> exerciseTimes, |
28 | | ext::shared_ptr<FdmMesher> mesher, |
29 | | ext::shared_ptr<FdmInnerValueCalculator> calculator, |
30 | | Size swingDirection, |
31 | | Size minExercises) |
32 | 0 | : exerciseTimes_(std::move(exerciseTimes)), mesher_(std::move(mesher)), |
33 | 0 | calculator_(std::move(calculator)), minExercises_(minExercises), |
34 | 0 | swingDirection_(swingDirection) {} |
35 | | |
36 | 0 | void FdmSimpleSwingCondition::applyTo(Array& a, Time t) const { |
37 | |
|
38 | 0 | const auto iter |
39 | 0 | = std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t); |
40 | 0 | const Size maxExerciseValue=mesher_->layout()->dim()[swingDirection_]-1; |
41 | |
|
42 | 0 | if (iter != exerciseTimes_.end()) { |
43 | 0 | Array retVal= a; |
44 | |
|
45 | 0 | const Size d = std::distance(iter, exerciseTimes_.end()); |
46 | |
|
47 | 0 | QL_REQUIRE(mesher_->layout()->size() == a.size(), |
48 | 0 | "inconsistent array dimensions"); |
49 | | |
50 | 0 | for (const auto& iter : *mesher_->layout()) { |
51 | | |
52 | 0 | const std::vector<Size>& coor = iter.coordinates(); |
53 | | |
54 | 0 | const Size exercisesUsed = coor[swingDirection_]; |
55 | | |
56 | 0 | if (exercisesUsed < maxExerciseValue) { |
57 | 0 | const Real cashflow = calculator_->innerValue(iter, t); |
58 | 0 | const Real currentValue = a[iter.index()]; |
59 | 0 | const Real valuePlusOneExercise |
60 | 0 | = a[mesher_->layout()->neighbourhood(iter, swingDirection_, 1)]; |
61 | | |
62 | 0 | if ( currentValue < valuePlusOneExercise + cashflow |
63 | 0 | || exercisesUsed + d <= minExercises_) { |
64 | 0 | retVal[iter.index()] = valuePlusOneExercise + cashflow; |
65 | 0 | } |
66 | 0 | } |
67 | 0 | } |
68 | 0 | a = retVal; |
69 | 0 | } |
70 | 0 | } |
71 | | } |