QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmsimpleswingcondition.cpp
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 <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#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
21#include <ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.hpp>
22#include <utility>
23
24namespace QuantLib {
25
27 std::vector<Time> exerciseTimes,
28 ext::shared_ptr<FdmMesher> mesher,
29 ext::shared_ptr<FdmInnerValueCalculator> calculator,
30 Size swingDirection,
31 Size minExercises)
32 : exerciseTimes_(std::move(exerciseTimes)), mesher_(std::move(mesher)),
33 calculator_(std::move(calculator)), minExercises_(minExercises),
34 swingDirection_(swingDirection) {}
35
37
38 const std::vector<Time>::const_iterator iter
39 = std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t);
40 const Size maxExerciseValue=mesher_->layout()->dim()[swingDirection_]-1;
41
42 if (iter != exerciseTimes_.end()) {
43 Array retVal= a;
44
45 const Size d = std::distance(iter, exerciseTimes_.end());
46
47 QL_REQUIRE(mesher_->layout()->size() == a.size(),
48 "inconsistent array dimensions");
49
50 for (const auto& iter : *mesher_->layout()) {
51
52 const std::vector<Size>& coor = iter.coordinates();
53
54 const Size exercisesUsed = coor[swingDirection_];
55
56 if (exercisesUsed < maxExerciseValue) {
57 const Real cashflow = calculator_->innerValue(iter, t);
58 const Real currentValue = a[iter.index()];
59 const Real valuePlusOneExercise
60 = a[mesher_->layout()->neighbourhood(iter, swingDirection_, 1)];
61
62 if ( currentValue < valuePlusOneExercise + cashflow
63 || exercisesUsed + d <= minExercises_) {
64 retVal[iter.index()] = valuePlusOneExercise + cashflow;
65 }
66 }
67 }
68 a = retVal;
69 }
70 }
71}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
FdmSimpleSwingCondition(std::vector< Time > exerciseTimes, ext::shared_ptr< FdmMesher > mesher, ext::shared_ptr< FdmInnerValueCalculator > calculator, Size swingDirection, Size minExercises=0)
const ext::shared_ptr< FdmMesher > mesher_
const ext::shared_ptr< FdmInnerValueCalculator > calculator_
void applyTo(Array &a, Time t) const override
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
Definition: any.hpp:35
STL namespace.