File: vegabumpcluster.cpp

package info (click to toggle)
quantlib 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,480 kB
  • sloc: cpp: 400,885; makefile: 6,547; python: 214; sh: 150; lisp: 86
file content (236 lines) | stat: -rw-r--r-- 7,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2008 Mark Joshi

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/errors.hpp>
#include <ql/models/marketmodels/evolutiondescription.hpp>
#include <ql/models/marketmodels/pathwisegreeks/vegabumpcluster.hpp>
#include <utility>
#include <valarray>

namespace QuantLib {



    VegaBumpCluster::VegaBumpCluster(Size factorBegin,
        Size factorEnd,
        Size rateBegin,
        Size rateEnd,
        Size stepBegin,
        Size stepEnd)
        :
    factorBegin_(factorBegin),
        factorEnd_(factorEnd),
        rateBegin_(rateBegin),
        rateEnd_(rateEnd),
        stepBegin_(stepBegin),
        stepEnd_(stepEnd)
    {
        QL_REQUIRE(factorBegin_<factorEnd_, "must have factorBegin_ < factorEnd_ in VegaBumpCluster ");
        QL_REQUIRE(rateBegin_<rateEnd_, "must have rateBegin_ < rateEnd_ in VegaBumpCluster ");
        QL_REQUIRE(stepBegin_<stepEnd_, "must have stepBegin_ < stepEnd_ in VegaBumpCluster ");
    }

    bool VegaBumpCluster::doesIntersect(const VegaBumpCluster& comparee) const
    {
        if (factorEnd_ <= comparee.factorBegin_)
            return false;

        if (rateEnd_ <= comparee.rateBegin_)
            return false;

        if (stepEnd_ <= comparee.stepBegin_)
            return false;


        if (comparee.factorEnd_ <= factorBegin_)
            return false;

        if (comparee.rateEnd_ <= rateBegin_)
            return false;

        if (comparee.stepEnd_ <= stepBegin_)
            return false;

        return true;


    }


    bool VegaBumpCluster::isCompatible(const ext::shared_ptr<MarketModel>& volStructure) const
    {
        if (rateEnd_ > volStructure->numberOfRates())
            return false;

        if (stepEnd_ > volStructure->numberOfSteps())
            return false;

        if (factorEnd_ > volStructure->numberOfFactors())
            return false;

        Size firstAliveRate = volStructure->evolution().firstAliveRate()[stepEnd_-1];

        return rateBegin_ >= firstAliveRate; // if the rate has reset after the beginning of the last step of the bump
    }



    VegaBumpCollection::VegaBumpCollection(const ext::shared_ptr<MarketModel>& volStructure,
                           bool factorwiseBumping)
                            : associatedVolStructure_(volStructure)
    {
        Size steps = volStructure->numberOfSteps();
        Size rates = volStructure->numberOfRates();
        Size factors = volStructure->numberOfFactors();

        for (Size s=0; s < steps; ++s)
            for (Size r=volStructure->evolution().firstAliveRate()[s]; r < rates; ++r)
            {
                if (factorwiseBumping)
                {
                    for (Size f=0; f < factors; ++f)
                    {
                        VegaBumpCluster thisCluster(f,f+1,r,r+1,s,s+1);
                        allBumps_.push_back(thisCluster);

                    }
                }
                else
                {
                     VegaBumpCluster thisCluster(0,factors,r,r+1,s,s+1);
                     allBumps_.push_back(thisCluster);

                }
            }

        checked_=true;
        full_=true;
        nonOverlapped_=true;


    }


    VegaBumpCollection::VegaBumpCollection(std::vector<VegaBumpCluster> allBumps,
                                           ext::shared_ptr<MarketModel> volStructure)
    : allBumps_(std::move(allBumps)), associatedVolStructure_(std::move(volStructure)),
      checked_(false) {
        for (auto& allBump : allBumps_)
            QL_REQUIRE(allBump.isCompatible(associatedVolStructure_),
                       "incompatible bumps passed to VegaBumpCollection");
    }


    const std::vector<VegaBumpCluster>& VegaBumpCollection::allBumps() const
    {
        return allBumps_;
    }

    bool VegaBumpCollection::isFull() const // i.e. is every alive pseudo-root element bumped at least once
    {
        if (checked_)
            return full_;
        std::vector<std::vector<std::valarray<bool> > > v;

        Size factors = associatedVolStructure_->numberOfFactors();

        std::valarray<bool> model(false,factors);
    //    std::fill(model.begin(), model.end(), false);

        std::vector<std::valarray<bool> > modelTwo;
        modelTwo.reserve(associatedVolStructure_->numberOfRates());
        for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
            modelTwo.push_back(model);

        v.reserve(associatedVolStructure_->numberOfSteps());
        for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
            v.push_back(modelTwo);

        for (const auto& allBump : allBumps_) {
            for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
                for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
                    for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s)
                        v[s][r][f] = true;
        }

        Size numberFailures =0;
        for (Size s =0; s < associatedVolStructure_->numberOfSteps(); ++s)
            for (Size f=0; f < associatedVolStructure_->numberOfFactors(); ++f)
                for (Size r=associatedVolStructure_->evolution().firstAliveRate()[s]; r <  associatedVolStructure_->numberOfRates(); ++r)
                    if (!v[s][r][f])
                        ++numberFailures;

        return numberFailures>0;

    }

    bool VegaBumpCollection::isNonOverlapping() const // i.e. is every alive pseudo-root element bumped at most once
    {

        if (checked_)
            return nonOverlapped_;

        std::vector<std::vector<std::valarray<bool> > > v;

        Size factors = associatedVolStructure_->numberOfFactors();


        std::valarray<bool> model(false,factors);
        //std::fill(model.begin(), model.end(), false);

        std::vector<std::valarray<bool> > modelTwo;
        modelTwo.reserve(associatedVolStructure_->numberOfRates());
        for (Size i=0; i < associatedVolStructure_->numberOfRates(); ++i)
            modelTwo.push_back(model);

        v.reserve(associatedVolStructure_->numberOfSteps());
        for (Size j=0; j < associatedVolStructure_->numberOfSteps(); ++j)
            v.push_back(modelTwo);

        Size numberFailures=0;

        for (const auto& allBump : allBumps_) {
            for (Size f = allBump.factorBegin(); f < allBump.factorEnd(); ++f)
                for (Size r = allBump.rateBegin(); r < allBump.rateEnd(); ++r)
                    for (Size s = allBump.stepBegin(); s < allBump.stepEnd(); ++s) {
                        if (v[s][r][f])
                            ++numberFailures;
                        v[s][r][f] = true;
                    }
        }

        return numberFailures>0;

    }

    bool VegaBumpCollection::isSensible() const // i.e. is every alive pseudo-root element bumped precisely once
    {
        if (checked_)
            return true;

        return isNonOverlapping() && isFull();
    }


    Size VegaBumpCollection::numberBumps() const
    {
        return allBumps_.size();
    }

}