QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
utilities.cpp
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
27namespace QuantLib {
28
29 void mergeTimes(const std::vector<std::vector<Time> >& times,
30 std::vector<Time>& mergedTimes,
31 std::vector<std::valarray<bool> >& isPresent) {
32
33 std::vector<Time> allTimes;
34 for (const auto& time : times) {
35 allTimes.insert(allTimes.end(), time.begin(), time.end());
36 }
37
38 // ...sort and compact the vector mergedTimes...
39 std::sort(allTimes.begin(), allTimes.end());
40 auto end = std::unique(allTimes.begin(), allTimes.end());
41 //mergedTimes.clear(); // shouldn't be cleared?
42 mergedTimes.insert(mergedTimes.end(),
43 allTimes.begin(), end);
44
45 isPresent.resize(times.size());
46 for (Size i=0; i<times.size(); i++) {
47 isPresent[i].resize(allTimes.size());
48 for (Size j=0; j<allTimes.size(); j++) {
49 isPresent[i][j] = std::binary_search(times[i].begin(),
50 times[i].end(),
51 allTimes[j]);
52 }
53 }
54 }
55
56 std::valarray<bool> isInSubset(const std::vector<Time>& set,
57 const std::vector<Time>& subset) {
58
59 std::valarray<bool> result(false,set.size());
60 Size dimsubSet = subset.size();
61 if (dimsubSet==0)
62 return result;
63 Size dimSet = set.size();
64 Time setElement, subsetElement;
65
66 QL_REQUIRE(dimSet >= dimsubSet,
67 "set is required to be larger or equal than subset");
68
69 for (Size i=0; i<dimSet; ++i) { // loop in set
70 Size j=0;
71 setElement = set[i];
72 for (;;) { // loop in subset
73 subsetElement = subset[j];
74 result[i] = false;
75 // if smaller no hope, leave false and go to next i
76 if (setElement < subsetElement)
77 break;
78 // if match, set result[i] to true and go to next i
79 if (setElement == subsetElement) {
80 result[i] = true;
81 break;
82 }
83 // if larger, leave false if at the end or go to next j
84 if (j == dimsubSet-1)
85 break;
86 ++j;
87 }
88 }
89 return result;
90 }
91
92 void checkIncreasingTimes(const std::vector<Time>& times) {
93 Size nTimes = times.size();
94 QL_REQUIRE(nTimes>0,
95 "at least one time is required");
96 QL_REQUIRE(times[0]>0.0,
97 "first time (" << times[0] <<
98 ") must be greater than zero");
99 for (Size i=0; i<nTimes-1; ++i)
100 QL_REQUIRE(times[i+1]-times[i]>0,
101 "non increasing rate times: "
102 "times[" << i << "]=" << times[i] << ", "
103 "times[" << i+1 << "]=" << times[i+1]);
104 }
105
106 void checkIncreasingTimesAndCalculateTaus(const std::vector<Time>& times,
107 std::vector<Time>& taus) {
108 Size nTimes = times.size();
109 QL_REQUIRE(nTimes>1,
110 "at least two times are required, " << nTimes << " provided");
111 QL_REQUIRE(times[0]>0.0,
112 "first time (" << times[0] <<
113 ") must be greater than zero");
114 if (taus.size()!=nTimes-1)
115 taus.resize(nTimes-1);
116 for (Size i=0; i<nTimes-1; ++i) {
117 taus[i]=times[i+1]-times[i];
118 QL_REQUIRE(taus[i]>0,
119 "non increasing rate times: "
120 "times[" << i << "]=" << times[i] << ", "
121 "times[" << i+1 << "]=" << times[i+1]);
122 }
123 }
124
125
126}
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::valarray< bool > isInSubset(const std::vector< Time > &set, const std::vector< Time > &subset)
Definition: utilities.cpp:56
void mergeTimes(const std::vector< std::vector< Time > > &times, std::vector< Time > &mergedTimes, std::vector< std::valarray< bool > > &isPresent)
Definition: utilities.cpp:29
void checkIncreasingTimesAndCalculateTaus(const std::vector< Time > &times, std::vector< Time > &taus)
Definition: utilities.cpp:106
void checkIncreasingTimes(const std::vector< Time > &times)
check for strictly increasing times, first time greater than zero
Definition: utilities.cpp:92