35 const ext::shared_ptr<MarketModel>& inputModel,
Size startIndex,
Size endIndex) {
36 std::vector<Real> subRateTimes(inputModel->evolution().rateTimes().begin()+startIndex,
37 inputModel->evolution().rateTimes().begin()+endIndex+1);
39 std::vector<Real> subForwards(inputModel->initialRates().begin()+startIndex,inputModel->initialRates().begin()+endIndex);
45 Size factors = inputModel->numberOfFactors();
53 while (index < inputModel->evolution().numberOfSteps() && inputModel->evolution().firstAliveRate()[index] <= startIndex)
55 const Matrix& thisPseudo = inputModel->pseudoRoot(index);
57 Real thisVariance_ =0.0;
58 for (
Size j=startIndex; j < endIndex; ++j)
59 for (
Size k=startIndex; k < endIndex; ++k)
60 for (
Size f=0;
f < factors; ++
f)
61 thisVariance_+= zed[0][j-startIndex]*thisPseudo[j][
f]*thisPseudo[k][
f]*zed[0][k-startIndex];
68 Size stopIndex = index;
76 Size numberRates = inputModel->evolution().numberOfRates();
78 Matrix thisDerivative(numberRates, factors,0.0);
79 Matrix nullDerivative(numberRates, factors,0.0);
83 while (index < stopIndex)
85 const Matrix& thisPseudo = inputModel->pseudoRoot(index);
87 for (
Size rate=startIndex; rate<endIndex; ++rate)
89 Size zIndex = rate -startIndex;
90 for (
Size f =0;
f < factors; ++
f)
93 for (
Size rate2 = startIndex; rate2<endIndex; ++rate2)
95 Size zIndex2 = rate2-startIndex;
96 sum += zed[0][zIndex2] * thisPseudo[rate2][
f];
98 sum *= 2.0*zed[0][zIndex];
100 thisDerivative[rate][
f] =sum;
107 for (
Size rate=startIndex; rate<endIndex; ++rate)
108 for (
Size f =0;
f < factors; ++
f)
109 thisDerivative[rate][
f] *= scale;
116 for (; index < inputModel->evolution().numberOfSteps(); ++index)
160 QuickCap(
Real Strike,
161 const std::vector<Real>& annuities,
162 const std::vector<Real>& currentRates,
163 const std::vector<Real>& expiries,
166 Real operator()(
Real volatility)
const;
181 QuickCap::QuickCap(
Real Strike,
182 const std::vector<Real>& annuities,
183 const std::vector<Real>& currentRates,
184 const std::vector<Real>& expiries,
195 Real QuickCap::operator()(Real volatility)
const
210 Real QuickCap::vega(Real volatility)
const
232 CapPseudoDerivative::CapPseudoDerivative(
const ext::shared_ptr<MarketModel>& inputModel,
237 : firstDF_(firstDF) {
238 QL_REQUIRE(startIndex < endIndex,
"for a cap pseudo derivative the start of the cap must be before the end");
239 QL_REQUIRE( endIndex <= inputModel->numberOfRates(),
"for a cap pseudo derivative the end of the cap must before the end of the rates");
241 Size numberCaplets = endIndex-startIndex;
242 Size numberRates = inputModel->numberOfRates();
243 Size factors = inputModel->numberOfFactors();
247 const Matrix& totalCovariance(inputModel->totalCovariance(inputModel->numberOfSteps()-1));
249 std::vector<Real> displacedImpliedVols(numberCaplets);
250 std::vector<Real> annuities(numberCaplets);
251 std::vector<Real> initialRates(numberCaplets);
252 std::vector<Real> expiries(numberCaplets);
260 for (
Size j = startIndex; j < endIndex; ++j)
262 Size capletIndex = j - startIndex;
263 Time resetTime = inputModel->evolution().rateTimes()[j];
264 expiries[capletIndex] = resetTime;
266 Real sd = std::sqrt(totalCovariance[j][j]);
267 displacedImpliedVols[capletIndex] = std::sqrt(totalCovariance[j][j]/resetTime);
269 Real forward = inputModel->initialRates()[j];
270 initialRates[capletIndex] = forward;
273 annuities[capletIndex] = annuity;
275 Real displacement = inputModel->displacements()[j];
277 guess+= displacedImpliedVols[capletIndex]*(forward+displacement)/forward;
278 minVol =std::min(minVol, displacedImpliedVols[capletIndex]);
279 maxVol =std::max(maxVol, displacedImpliedVols[capletIndex]*(forward+displacement)/forward);
290 capPrice += capletPrice;
294 guess/=numberCaplets;
297 for (
Size step =0; step < inputModel->evolution().numberOfSteps(); ++step)
299 Matrix thisDerivative(numberRates,factors,0.0);
301 for (
Size rate =std::max(inputModel->evolution().firstAliveRate()[step],startIndex);
302 rate < endIndex; ++rate)
304 for (
Size f=0;
f < factors; ++
f)
306 Real expiry = inputModel->evolution().rateTimes()[rate];
307 Real volDerivative = inputModel->pseudoRoot(step)[rate][
f]
308 /(displacedImpliedVols[rate-startIndex]*expiry);
310 displacedImpliedVols[rate-startIndex]*std::sqrt(expiry),
312 annuities[rate-startIndex],
313 inputModel->displacements()[rate]);
316 thisDerivative[rate][
f] = volDerivative*capletVega;
324 QuickCap capPricer(strike, annuities, initialRates, expiries,capPrice);
326 Size maxEvaluations = 1000;
327 Real accuracy = 1E-6;
339 for (
Size step =0; step < inputModel->evolution().numberOfSteps(); ++step)
341 Matrix thisDerivative(numberRates,factors,0.0);
343 for (
Size rate =std::max(inputModel->evolution().firstAliveRate()[step],startIndex);
344 rate < endIndex; ++rate)
346 for (
Size f=0;
f < factors; ++
f)
const Matrix & priceDerivative(Size i) const
const Matrix & volatilityDerivative(Size i) const
std::vector< Matrix > volatilityDerivatives_
std::vector< Matrix > priceDerivatives_
Real impliedVolatility() const
Curve state for Libor market models
Real discountRatio(Size i, Size j) const override
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
Matrix used in linear algebra.
void setMaxEvaluations(Size evaluations)
Real solve(const F &f, Real accuracy, Real guess, Real step) const
static Matrix coterminalSwapZedMatrix(const CurveState &cs, Spread displacement)
SwaptionPseudoDerivative(const ext::shared_ptr< MarketModel > &inputModel, Size startIndex, Size endIndex)
const Matrix & varianceDerivative(Size i) const
const Matrix & volatilityDerivative(Size i) const
std::vector< Matrix > volatilityDerivatives_
std::vector< Matrix > varianceDerivatives_
Real impliedVolatility() const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Real Time
continuous quantity with 1-year units
std::size_t Size
size of a container
Real blackFormulaVolDerivative(Rate strike, Rate forward, Real stdDev, Real expiry, Real discount, Real displacement)
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
Utility functions for mapping between swap rate and forward rate.
const std::vector< Real > & currentRates_
const std::vector< Real > & expiries_
const std::vector< Real > & annuities_