QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
vegabumpcluster.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Mark Joshi
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/errors.hpp>
23#include <utility>
24#include <valarray>
25
26namespace QuantLib {
27
28
29
31 Size factorEnd,
32 Size rateBegin,
33 Size rateEnd,
34 Size stepBegin,
35 Size stepEnd)
36 :
37 factorBegin_(factorBegin),
38 factorEnd_(factorEnd),
39 rateBegin_(rateBegin),
40 rateEnd_(rateEnd),
41 stepBegin_(stepBegin),
42 stepEnd_(stepEnd)
43 {
44 QL_REQUIRE(factorBegin_<factorEnd_, "must have factorBegin_ < factorEnd_ in VegaBumpCluster ");
45 QL_REQUIRE(rateBegin_<rateEnd_, "must have rateBegin_ < rateEnd_ in VegaBumpCluster ");
46 QL_REQUIRE(stepBegin_<stepEnd_, "must have stepBegin_ < stepEnd_ in VegaBumpCluster ");
47 }
48
50 {
51 if (factorEnd_ <= comparee.factorBegin_)
52 return false;
53
54 if (rateEnd_ <= comparee.rateBegin_)
55 return false;
56
57 if (stepEnd_ <= comparee.stepBegin_)
58 return false;
59
60
61 if (comparee.factorEnd_ <= factorBegin_)
62 return false;
63
64 if (comparee.rateEnd_ <= rateBegin_)
65 return false;
66
67 if (comparee.stepEnd_ <= stepBegin_)
68 return false;
69
70 return true;
71
72
73 }
74
75
76 bool VegaBumpCluster::isCompatible(const ext::shared_ptr<MarketModel>& volStructure) const
77 {
78 if (rateEnd_ > volStructure->numberOfRates())
79 return false;
80
81 if (stepEnd_ > volStructure->numberOfSteps())
82 return false;
83
84 if (factorEnd_ > volStructure->numberOfFactors())
85 return false;
86
87 Size firstAliveRate = volStructure->evolution().firstAliveRate()[stepEnd_-1];
88
89 return rateBegin_ >= firstAliveRate; // if the rate has reset after the beginning of the last step of the bump
90 }
91
92
93
94 VegaBumpCollection::VegaBumpCollection(const ext::shared_ptr<MarketModel>& volStructure,
95 bool factorwiseBumping)
96 : associatedVolStructure_(volStructure)
97 {
98 Size steps = volStructure->numberOfSteps();
99 Size rates = volStructure->numberOfRates();
100 Size factors = volStructure->numberOfFactors();
101
102 for (Size s=0; s < steps; ++s)
103 for (Size r=volStructure->evolution().firstAliveRate()[s]; r < rates; ++r)
104 {
105 if (factorwiseBumping)
106 {
107 for (Size f=0; f < factors; ++f)
108 {
109 VegaBumpCluster thisCluster(f,f+1,r,r+1,s,s+1);
110 allBumps_.push_back(thisCluster);
111
112 }
113 }
114 else
115 {
116 VegaBumpCluster thisCluster(0,factors,r,r+1,s,s+1);
117 allBumps_.push_back(thisCluster);
118
119 }
120 }
121
122 checked_=true;
123 full_=true;
124 nonOverlapped_=true;
125
126
127 }
128
129
130 VegaBumpCollection::VegaBumpCollection(std::vector<VegaBumpCluster> allBumps,
131 ext::shared_ptr<MarketModel> volStructure)
132 : allBumps_(std::move(allBumps)), associatedVolStructure_(std::move(volStructure)),
133 checked_(false) {
134 for (auto& allBump : allBumps_)
135 QL_REQUIRE(allBump.isCompatible(associatedVolStructure_),
136 "incompatible bumps passed to VegaBumpCollection");
137 }
138
139
140 const std::vector<VegaBumpCluster>& VegaBumpCollection::allBumps() const
141 {
142 return allBumps_;
143 }
144
145 bool VegaBumpCollection::isFull() const // i.e. is every alive pseudo-root element bumped at least once
146 {
147 if (checked_)
148 return full_;
149 std::vector<std::vector<std::valarray<bool> > > v;
150
151 Size factors = associatedVolStructure_->numberOfFactors();
152
153 std::valarray<bool> model(false,factors);
154 // std::fill(model.begin(), model.end(), false);
155
156 std::vector<std::valarray<bool> > modelTwo;
157 for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
158 modelTwo.push_back(model);
159
160 for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
161 v.push_back(modelTwo);
162
163 for (const auto& allBump : allBumps_) {
164 for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
165 for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
166 for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s)
167 v[s][r][f] = true;
168 }
169
170 Size numberFailures =0;
171 for (Size s =0; s < associatedVolStructure_->numberOfSteps(); ++s)
172 for (Size f=0; f < associatedVolStructure_->numberOfFactors(); ++f)
173 for (Size r=associatedVolStructure_->evolution().firstAliveRate()[s]; r < associatedVolStructure_->numberOfRates(); ++r)
174 if (!v[s][r][f])
175 ++numberFailures;
176
177 return numberFailures>0;
178
179 }
180
181 bool VegaBumpCollection::isNonOverlapping() const // i.e. is every alive pseudo-root element bumped at most once
182 {
183
184 if (checked_)
185 return nonOverlapped_;
186
187 std::vector<std::vector<std::valarray<bool> > > v;
188
189 Size factors = associatedVolStructure_->numberOfFactors();
190
191
192 std::valarray<bool> model(false,factors);
193 //std::fill(model.begin(), model.end(), false);
194
195 std::vector<std::valarray<bool> > modelTwo;
196 for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
197 modelTwo.push_back(model);
198
199 for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
200 v.push_back(modelTwo);
201
202 Size numberFailures=0;
203
204 for (const auto& allBump : allBumps_) {
205 for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
206 for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
207 for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s) {
208 if (v[s][r][f])
209 ++numberFailures;
210 v[s][r][f] = true;
211 }
212 }
213
214 return numberFailures>0;
215
216 }
217
218 bool VegaBumpCollection::isSensible() const // i.e. is every alive pseudo-root element bumped precisely once
219 {
220 if (checked_)
221 return true;
222
223 return isNonOverlapping() && isFull();
224 }
225
226
228 {
229 return allBumps_.size();
230 }
231
232}
bool doesIntersect(const VegaBumpCluster &comparee) const
bool isCompatible(const ext::shared_ptr< MarketModel > &volStructure) const
VegaBumpCluster(Size factorBegin, Size factorEnd, Size rateBegin, Size rateEnd, Size stepBegin, Size stepEnd)
VegaBumpCollection(const ext::shared_ptr< MarketModel > &volStructure, bool allowFactorwiseBumping=true)
ext::shared_ptr< MarketModel > associatedVolStructure_
const std::vector< VegaBumpCluster > & allBumps() const
std::vector< VegaBumpCluster > allBumps_
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v