Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dkimpliedyoyinflationtermstructure.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20#include <ql/time/schedule.hpp>
21
22using QuantLib::Date;
23using QuantLib::Real;
24using QuantLib::MakeSchedule;
25using QuantLib::Schedule;
26using QuantLib::Size;
27using QuantLib::Time;
28using std::map;
29using std::vector;
30
31namespace QuantExt {
32
34 const QuantLib::ext::shared_ptr<CrossAssetModel>& model, Size index, bool indexIsInterpolated)
35 : YoYInflationModelTermStructure(model, index, indexIsInterpolated) {}
36
37map<Date, Real> DkImpliedYoYInflationTermStructure::yoyRates(const vector<Date>& dts, const Period& obsLag) const {
38
39 map<Date, Real> yoys;
40 map<Date, Real> yoyswaplet;
41 map<Date, Real> yoydiscount;
42 Period useLag = obsLag == -1 * Days ? observationLag() : obsLag;
43 Calendar cal = model_->infdk(index_)->termStructure()->calendar();
44 DayCounter dc = model_->infdk(index_)->termStructure()->dayCounter();
45 Date maturity;
46
47 for (Size j = 0; j < dts.size(); j++) {
48
50 maturity = dts[j] - useLag;
51 } else {
52 maturity = inflationPeriod(dts[j] - useLag, frequency()).first;
53 }
54
55 Schedule schedule = MakeSchedule()
56 .from(baseDate())
57 .to(maturity)
58 .withTenor(1 * Years)
59 .withConvention(Unadjusted)
60 .withCalendar(cal)
61 .backwards();
62
63 Real yoyLegRate = 0.0;
64 Real fixedDiscounts = 0.0;
65 for (Size i = 1; i < schedule.dates().size(); i++) {
66 map<Date, Real>::const_iterator it = yoyswaplet.find(schedule.dates()[i]);
67 Real swapletPrice, discount;
68 if (it == yoyswaplet.end()) {
69 if (schedule.dates()[i - 1] < baseDate()) {
70 // for the first YoY swaplet, I(T_i-1) is known, obtained from a fixing. I(T_i) comes from the model
71 // directly - I(t) * Itilde(t,T).
72 Time t1 = dayCounter().yearFraction(model_->infdk(index_)->termStructure()->baseDate(),
73 schedule.dates()[i - 1]);
74 Real I1 = model_->infdkI(index_, t1, t1, state_[0], state_[1]).first;
75 Time t2 = dc.yearFraction(baseDate(), schedule.dates()[i]);
76 std::pair<Real, Real> II2 =
77 model_->infdkI(index_, relativeTime_, relativeTime_ + t2, state_[0], state_[1]);
78 Real I2 = II2.first * II2.second;
79 discount = model_->discountBond(model_->ccyIndex(model_->infdk(index_)->currency()), relativeTime_,
80 relativeTime_ + t2, state_[2]);
81 swapletPrice = discount * ((I2 / I1) - 1);
82 } else {
83 Time t1 = dc.yearFraction(baseDate(), schedule.dates()[i - 1]);
84 Time t2 = dc.yearFraction(baseDate(), schedule.dates()[i]);
85 discount = model_->discountBond(model_->ccyIndex(model_->infdk(index_)->currency()), relativeTime_,
86 relativeTime_ + t2, state_[2]);
87 swapletPrice = yoySwapletRate(t1, t2);
88 }
89 yoyswaplet[schedule.dates()[i]] = swapletPrice;
90 yoydiscount[schedule.dates()[i]] = discount;
91 } else {
92 swapletPrice = yoyswaplet[schedule.dates()[i]];
93 discount = yoydiscount[schedule.dates()[i]];
94 }
95 yoyLegRate += swapletPrice;
96 fixedDiscounts += discount;
97 }
98 Real yoyRate = (yoyLegRate / fixedDiscounts);
99
100 if (hasSeasonality()) {
101 yoyRate = seasonality()->correctYoYRate(dts[j] - useLag, yoyRate, *this);
102 }
103 yoys[dts[j]] = yoyRate;
104 }
105
106 return yoys;
107}
108
110 return model_->infdkYY(index_, relativeTime_, relativeTime_ + S,
111 relativeTime_ + T, state_[0], state_[1], state_[2]);
112}
113
115 // For DK YoY, expect the state to be three variables i.e. z_I and y_I and z_{ir}.
116 QL_REQUIRE(state_.size() == 3, "DkImpliedYoYInflationTermStructure: expected state to have " <<
117 "three elements but got " << state_.size());
118}
119
120}
std::map< QuantLib::Date, QuantLib::Real > yoyRates(const std::vector< QuantLib::Date > &dates, const QuantLib::Period &obsLag=-1 *QuantLib::Days) const override
QuantLib::Real yoySwapletRate(QuantLib::Time S, QuantLib::Time T) const
DkImpliedYoYInflationTermStructure(const QuantLib::ext::shared_ptr< CrossAssetModel > &model, QuantLib::Size index, bool indexIsInterpolated)
QuantLib::ext::shared_ptr< CrossAssetModel > model_
QuantLib::Real yoyRate(const QuantLib::Date &d, const QuantLib::Period &obsLag=-1 *QuantLib::Days, bool forceLinearInterpolation=false, bool extrapolate=false) const
year on year inflation term structure implied by a Dodgson Kainth (DK) model