• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 14910176578

08 May 2025 03:28PM UTC coverage: 73.315% (+0.02%) from 73.3%
14910176578

Pull #2195

github

web-flow
Merge 3a61f499c into 5d972fb7b
Pull Request #2195: Added `Handle<Quote>` for spread in `OISRateHelper`

32 of 33 new or added lines in 2 files covered. (96.97%)

277 existing lines in 25 files now uncovered.

56277 of 76761 relevant lines covered (73.31%)

8687029.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.7
/ql/methods/finitedifferences/stepconditions/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 <algorithm>
22
#include <ql/math/interpolations/bilinearinterpolation.hpp>
23
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24
#include <ql/methods/finitedifferences/stepconditions/fdmsimplestoragecondition.hpp>
25
#include <utility>
26

27
namespace QuantLib {
28

29
    FdmSimpleStorageCondition::FdmSimpleStorageCondition(
1✔
30
        std::vector<Time> exerciseTimes,
31
        ext::shared_ptr<FdmMesher> mesher,
32
        ext::shared_ptr<FdmInnerValueCalculator> calculator,
33
        Real changeRate)
1✔
34
    : exerciseTimes_(std::move(exerciseTimes)), mesher_(std::move(mesher)),
1✔
35
      calculator_(std::move(calculator)), changeRate_(changeRate) {
1✔
36

37
        x_.reserve(mesher_->layout()->dim()[0]);
1✔
38
        y_.reserve(mesher_->layout()->dim()[1]);
1✔
39

40
        for (const auto& iter : *mesher_->layout()) {
1,277✔
41
            if (iter.coordinates()[1] == 0U) {
1,275✔
42
                x_.push_back(mesher_->location(iter, 0));
25✔
43
            }
44
            if (iter.coordinates()[0] == 0U) {
1,275✔
45
                y_.push_back(mesher_->location(iter, 1));
51✔
46
            }
47
        }
48
    }
1✔
49

50
    void FdmSimpleStorageCondition::applyTo(Array& a, Time t) const {
368✔
51
        const auto iter
52
            = std::find(exerciseTimes_.begin(), exerciseTimes_.end(), t);
368✔
53

54
        if (iter != exerciseTimes_.end()) {
368✔
55
            Array retVal(a.size());
366✔
56

57
            Matrix m(y_.size(), x_.size());
366✔
58
            std::copy(a.begin(), a.end(), m.begin());
366✔
59
            BilinearInterpolation interpl(x_.begin(), x_.end(),
732✔
60
                                          y_.begin(), y_.end(), m);
366✔
61

62
            QL_REQUIRE(mesher_->layout()->size() == a.size(),
366✔
63
                       "inconsistent array dimensions");
64

65
            for (const auto& iter : *mesher_->layout()) {
467,382✔
66
                const std::vector<Size>& coor = iter.coordinates();
67
                const Real x = x_[coor[0]];
466,650✔
68
                const Real y = y_[coor[1]];
466,650✔
69

70
                const Real price = calculator_->innerValue(iter, t);
466,650✔
71

72
                const Real maxWithDraw = std::min(y-y_.front(), changeRate_);
466,650✔
73
                const Real sellPrice   = interpl(x, y-maxWithDraw);
466,650✔
74

75
                const Real maxInject = std::min(y_.back()-y, changeRate_);
466,650✔
76
                const Real buyPrice  = interpl(x, y+maxInject);
466,650✔
77

78
                // bang-bang-wait strategy
79
                Real currentValue = std::max({
466,650✔
80
                        a[iter.index()],
81
                        buyPrice - price*maxInject,
466,650✔
82
                        sellPrice + price*maxWithDraw
466,650✔
83
                    });
466,650✔
84

85
                // check if intermediate grid points give a better value
86
                auto yIter = std::upper_bound(y_.begin(), y_.end(), y - maxWithDraw);
466,650✔
87

88
                while (yIter != y_.end() && *yIter < y + maxInject) {
915,000✔
89
                    if (*yIter != y) {
448,350✔
90
                        const Real change = *yIter - y;
×
91
                        const Real storagePrice(interpl(x, *yIter));
×
92

UNCOV
93
                        currentValue = std::max(currentValue,
×
UNCOV
94
                            storagePrice - change*price);
×
95
                    }
96
                    ++yIter;
97
                }
98

99
                retVal[iter.index()] = currentValue;
466,650✔
100
            }
101
            a = retVal;
366✔
102
        }
103
    }
368✔
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc