Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/methods/finitedifferences/stepconditions/fdmbermudanstepcondition.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2010 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/fdmbermudanstepcondition.hpp>
22
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    FdmBermudanStepCondition::FdmBermudanStepCondition(
28
        const std::vector<Date>& exerciseDates,
29
        const Date& referenceDate,
30
        const DayCounter& dayCounter,
31
        ext::shared_ptr<FdmMesher> mesher,
32
        ext::shared_ptr<FdmInnerValueCalculator> calculator)
33
0
    : mesher_(std::move(mesher)), calculator_(std::move(calculator)) {
34
35
0
        exerciseTimes_.reserve(exerciseDates.size());
36
0
        for (auto exerciseDate : exerciseDates) {
37
0
            exerciseTimes_.push_back(dayCounter.yearFraction(referenceDate, exerciseDate));
38
0
        }
39
0
    }
40
41
0
    const std::vector<Time>& FdmBermudanStepCondition::exerciseTimes() const {
42
0
        return exerciseTimes_;
43
0
    }
44
    
45
0
    void FdmBermudanStepCondition::applyTo(Array& a, Time t) const {
46
0
        if (std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t) 
47
0
              != exerciseTimes_.end()) {
48
            
49
0
            QL_REQUIRE(mesher_->layout()->size() == a.size(),
50
0
                       "inconsistent array dimensions");
51
52
0
            const Size dims = mesher_->layout()->dim().size();
53
0
            Array locations(dims);
54
55
0
            for (const auto& iter : *mesher_->layout()) {
56
0
                for (Size i=0; i < dims; ++i)
57
0
                    locations[i] = mesher_->location(iter, i);
58
59
0
                const Real innerValue = calculator_->innerValue(iter, t);
60
0
                if (innerValue > a[iter.index()]) {
61
0
                    a[iter.index()] = innerValue;
62
0
                }
63
0
            }            
64
0
        }
65
0
    }
66
}