QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
volatilityinterpolationspecifierabcd.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
5Copyright (C) 2007 Mark Joshi
6
7This file is part of QuantLib, a free-software/open-source library
8for financial quantitative analysts and developers - http://quantlib.org/
9
10QuantLib is free software: you can redistribute it and/or modify it
11under the terms of the QuantLib license. You should have received a
12copy of the license along with this program; if not, please email
13<quantlib-dev@lists.sf.net>. The license is also available online at
14<http://quantlib.org/license.shtml>.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20/*!
21implementation specifies how to decide volatility structure for additional
22synthetic rates which are interleaved
23
24here we work with abcd curves and interpolate the a, b, c and d
25
26*/
27
29#include <ql/types.hpp>
30#include <ql/errors.hpp>
31#include <ql/shared_ptr.hpp>
32#include <vector>
33
34namespace QuantLib
35{
36
38 Size offset,
39 const std::vector< PiecewiseConstantAbcdVariance>& originalVariances, // these should be associated with the long rates
40 const std::vector<Time>& timesForSmallRates, // these should be associated with the shorter rates
41 Real lastCapletVol
42 )
43 :
44 period_(period),
45 offset_(offset),
46 interpolatedVariances_(timesForSmallRates.size()-1),
47 originalVariances_(originalVariances.size()),
48 originalABCDVariances_(originalVariances),
49 originalABCDVariancesScaled_(originalVariances),
50 lastCapletVol_(lastCapletVol),
51 timesForSmallRates_(timesForSmallRates),
52 scalingFactors_(originalVariances.size(),1.0),
53 noBigRates_(originalVariances.size()),
54 noSmallRates_(timesForSmallRates.size()-1)
55 {
56 QL_REQUIRE( (noSmallRates_ - offset) /period == noBigRates_, "size mismatch in VolatilityInterpolationSpecifierabcd");
57
58 for (Size i=0; i < noBigRates_; ++i)
59 for (Size j=0; j < originalVariances[i].rateTimes().size(); ++j)
60 QL_REQUIRE( originalVariances[i].rateTimes()[j] == timesForSmallRates[offset+j*period],"rate times in variances passed in don't match small times in VolatilityInterpolationSpecifierabcd");
61
62 if (lastCapletVol_ == 0.0)
64
65 // change type of array to PiecewiseConstantVariance for client, from PiecewiseConstantAbcdVariance
66 for (Size i=0; i < noBigRates_; ++i)
67 originalVariances_[i] = ext::shared_ptr<PiecewiseConstantVariance>(new PiecewiseConstantAbcdVariance(originalVariances[i]));
68
69 recompute();
70
71 }
72
73 void VolatilityInterpolationSpecifierabcd::setScalingFactors(const std::vector<Real>& scales)
74 {
75 QL_REQUIRE(scalingFactors_.size() == scales.size(), "inappropriate number of scales passed in to VolatilityInterpolationSpecifierabcd::setScalingFactors ");
76 scalingFactors_= scales;
77 recompute();
78 }
79
81 {
82 lastCapletVol_ = vol;
83 recompute();
84 }
85
86
87 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& VolatilityInterpolationSpecifierabcd::interpolatedVariances() const
88 {
90 }
91
92 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& VolatilityInterpolationSpecifierabcd::originalVariances() const
93 {
94 return originalVariances_;
95 }
96
98 {
99 return period_;
100 }
101
103 {
104 return offset_;
105 }
106
108 {
109 return noBigRates_;
110 }
112 {
113 return noSmallRates_;
114 }
115
116
118 {
119 // PiecewiseConstantAbcdVariance(Real a, Real b, Real c, Real d,
120 // Size resetIndex,
121 // const std::vector<Time>& rateTimes);
122
123
124 for (Size i=0; i < noBigRates_; ++i)
125 {
126 Real a,b,c,d;
127 originalABCDVariances_[i].getABCD(a,b,c,d);
128 a*=scalingFactors_[i];
129 b*=scalingFactors_[i];
130 // c is not scaled
131 d*=scalingFactors_[i];
132
134
135 }
136
137 // three cases:
138 //before offset,
139 // between offset and last big rate,
140 // and after last big rate
141
142 // before offset
143
144 {
145 Real a,b,c,d;
146 originalABCDVariancesScaled_[0].getABCD(a,b,c,d);
147
148 for (Size i=0; i < offset_; ++i)
149 interpolatedVariances_[i] = ext::shared_ptr<PiecewiseConstantVariance>(
151 }
152
153
154 // in between rates
155
156 for (Size j=0; j < noBigRates_-1; ++j)
157 {
158 Real a,b,c,d;
159 Real a0,b0,c0,d0;
160 Real a1,b1,c1,d1;
161 originalABCDVariancesScaled_[j].getABCD(a0,b0,c0,d0);
162 originalABCDVariancesScaled_[j+1].getABCD(a1,b1,c1,d1);
163 a= 0.5*(a0+a1);
164 b= 0.5*(b0+b1);
165 c= 0.5*(c0+c1);
166 d= 0.5*(d0+d1);
167
168 for (Size i=0; i < period_; ++i)
169 interpolatedVariances_[i+j*period_+offset_] = ext::shared_ptr<PiecewiseConstantVariance>(
171
172 }
173
174
175 {
176 Real a,b,c,d;
178
179 for (Size i=offset_+(noBigRates_-1)*period_; i < noSmallRates_; ++i)
180 interpolatedVariances_[i] = ext::shared_ptr<PiecewiseConstantVariance>(
182
183 // very last rate is special as we must match the caplet vol
184 Real vol = interpolatedVariances_[noSmallRates_-1]->totalVolatility(noSmallRates_-1);
185
186 Real scale = lastCapletVol_/vol;
187 a*=scale;
188 b*=scale;
189 d*=scale;
190 interpolatedVariances_[noSmallRates_-1] = ext::shared_ptr<PiecewiseConstantVariance>(
192
193 }
194 }
195
196}
std::vector< PiecewiseConstantAbcdVariance > originalABCDVariances_
VolatilityInterpolationSpecifierabcd(Size period, Size offset, const std::vector< PiecewiseConstantAbcdVariance > &originalVariances, const std::vector< Time > &timesForSmallRates, Real lastCapletVol=0.0)
void setScalingFactors(const std::vector< Real > &scales) override
std::vector< PiecewiseConstantAbcdVariance > originalABCDVariancesScaled_
std::vector< ext::shared_ptr< PiecewiseConstantVariance > > interpolatedVariances_
const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > & interpolatedVariances() const override
const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > & originalVariances() const override
std::vector< ext::shared_ptr< PiecewiseConstantVariance > > originalVariances_
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
Date d
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Maps shared_ptr to either the boost or std implementation.
Custom types.