QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 modelTwo.reserve(associatedVolStructure_->numberOfRates());
158 for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
159 modelTwo.push_back(model);
160
161 v.reserve(associatedVolStructure_->numberOfSteps());
162 for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
163 v.push_back(modelTwo);
164
165 for (const auto& allBump : allBumps_) {
166 for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
167 for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
168 for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s)
169 v[s][r][f] = true;
170 }
171
172 Size numberFailures =0;
173 for (Size s =0; s < associatedVolStructure_->numberOfSteps(); ++s)
174 for (Size f=0; f < associatedVolStructure_->numberOfFactors(); ++f)
175 for (Size r=associatedVolStructure_->evolution().firstAliveRate()[s]; r < associatedVolStructure_->numberOfRates(); ++r)
176 if (!v[s][r][f])
177 ++numberFailures;
178
179 return numberFailures>0;
180
181 }
182
183 bool VegaBumpCollection::isNonOverlapping() const // i.e. is every alive pseudo-root element bumped at most once
184 {
185
186 if (checked_)
187 return nonOverlapped_;
188
189 std::vector<std::vector<std::valarray<bool> > > v;
190
191 Size factors = associatedVolStructure_->numberOfFactors();
192
193
194 std::valarray<bool> model(false,factors);
195 //std::fill(model.begin(), model.end(), false);
196
197 std::vector<std::valarray<bool> > modelTwo;
198 modelTwo.reserve(associatedVolStructure_->numberOfRates());
199 for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
200 modelTwo.push_back(model);
201
202 v.reserve(associatedVolStructure_->numberOfSteps());
203 for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
204 v.push_back(modelTwo);
205
206 Size numberFailures=0;
207
208 for (const auto& allBump : allBumps_) {
209 for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
210 for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
211 for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s) {
212 if (v[s][r][f])
213 ++numberFailures;
214 v[s][r][f] = true;
215 }
216 }
217
218 return numberFailures>0;
219
220 }
221
222 bool VegaBumpCollection::isSensible() const // i.e. is every alive pseudo-root element bumped precisely once
223 {
224 if (checked_)
225 return true;
226
227 return isNonOverlapping() && isFull();
228 }
229
230
232 {
233 return allBumps_.size();
234 }
235
236}
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:37
STL namespace.
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v