Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/models/marketmodels/utilities.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) 2007 Ferdinando Ametrano
5
 Copyright (C) 2006 Marco Bianchetti
6
 Copyright (C) 2006 Mark Joshi
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <http://quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/models/marketmodels/utilities.hpp>
23
#include <ql/errors.hpp>
24
#include <algorithm>
25
#include <valarray>
26
27
namespace QuantLib {
28
29
    void mergeTimes(const std::vector<std::vector<Time> >& times,
30
                    std::vector<Time>& mergedTimes,
31
0
                    std::vector<std::valarray<bool> >& isPresent) {
32
33
0
        std::vector<Time> allTimes;
34
0
        for (const auto& time : times) {
35
0
            allTimes.insert(allTimes.end(), time.begin(), time.end());
36
0
        }
37
38
        // ...sort and compact the vector mergedTimes...
39
0
        std::sort(allTimes.begin(), allTimes.end());
40
0
        auto end = std::unique(allTimes.begin(), allTimes.end());
41
        //mergedTimes.clear(); // shouldn't be cleared?
42
0
        mergedTimes.insert(mergedTimes.end(),
43
0
                           allTimes.begin(), end);
44
45
0
        isPresent.resize(times.size());
46
0
        for (Size i=0; i<times.size(); i++) {
47
0
            isPresent[i].resize(allTimes.size());
48
0
            for (Size j=0; j<allTimes.size(); j++) {
49
0
                isPresent[i][j] = std::binary_search(times[i].begin(),
50
0
                                                     times[i].end(),
51
0
                                                     allTimes[j]);
52
0
            }
53
0
        }
54
0
    }
55
56
    std::valarray<bool> isInSubset(const std::vector<Time>& set,
57
0
                                   const std::vector<Time>& subset) {
58
59
0
        std::valarray<bool> result(false,set.size());
60
0
        Size dimsubSet = subset.size();
61
0
        if (dimsubSet==0)
62
0
            return result;
63
0
        Size dimSet = set.size();
64
0
        Time setElement, subsetElement;
65
66
0
        QL_REQUIRE(dimSet >= dimsubSet,
67
0
                   "set is required to be larger or equal than subset");
68
69
0
        for (Size i=0; i<dimSet; ++i) {  // loop in set
70
0
            Size j=0;
71
0
            setElement = set[i];
72
0
            for (;;) {              // loop in subset
73
0
                subsetElement = subset[j];
74
0
                result[i] = false;
75
                // if smaller no hope, leave false and go to next i
76
0
                if (setElement < subsetElement)
77
0
                    break;
78
                // if match, set result[i] to true and go to next i
79
0
                if (setElement == subsetElement) {
80
0
                    result[i] = true;
81
0
                    break;
82
0
                }
83
                // if larger, leave false if at the end or go to next j
84
0
                if (j == dimsubSet-1)
85
0
                    break;
86
0
                ++j;
87
0
            }
88
0
        }
89
0
        return result;
90
0
    }
91
92
0
    void checkIncreasingTimes(const std::vector<Time>& times) {
93
0
        Size nTimes = times.size();
94
0
        QL_REQUIRE(nTimes>0,
95
0
                   "at least one time is required");
96
0
        QL_REQUIRE(times[0]>0.0,
97
0
                   "first time (" << times[0] <<
98
0
                   ") must be greater than zero");
99
0
        for (Size i=0; i<nTimes-1; ++i)
100
0
            QL_REQUIRE(times[i+1]-times[i]>0,
101
0
                       "non increasing rate times: "
102
0
                       "times[" << i   << "]=" << times[i] << ", "
103
0
                       "times[" << i+1 << "]=" << times[i+1]);
104
0
    }
105
106
    void checkIncreasingTimesAndCalculateTaus(const std::vector<Time>& times,
107
0
                                              std::vector<Time>& taus) {
108
0
        Size nTimes = times.size();
109
0
        QL_REQUIRE(nTimes>1,
110
0
                   "at least two times are required, " << nTimes << " provided");
111
0
        QL_REQUIRE(times[0]>0.0,
112
0
                   "first time (" << times[0] <<
113
0
                   ") must be greater than zero");
114
0
        if (taus.size()!=nTimes-1)
115
0
            taus.resize(nTimes-1);
116
0
        for (Size i=0; i<nTimes-1; ++i) {
117
0
            taus[i]=times[i+1]-times[i];
118
0
            QL_REQUIRE(taus[i]>0,
119
0
                       "non increasing rate times: "
120
0
                       "times[" << i   << "]=" << times[i] << ", "
121
0
                       "times[" << i+1 << "]=" << times[i+1]);
122
0
        }
123
0
    }
124
125
126
}