Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
inflationindexwrapper.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
19#include <ql/termstructures/inflationtermstructure.hpp>
20#include <ql/time/calendars/nullcalendar.hpp>
22
23namespace QuantExt {
24
25QL_DEPRECATED_DISABLE_WARNING
26ZeroInflationIndexWrapper::ZeroInflationIndexWrapper(const QuantLib::ext::shared_ptr<ZeroInflationIndex> source)
27 : ZeroInflationIndex(source->familyName(), source->region(), source->revised(), source->frequency(), source->availabilityLag(), source->currency(),
28 source->zeroInflationTermStructure()),
29 source_(source), interpolation_(QuantLib::CPI::InterpolationType::Flat) {}
30
31ZeroInflationIndexWrapper::ZeroInflationIndexWrapper(const QuantLib::ext::shared_ptr<ZeroInflationIndex> source,
32 const CPI::InterpolationType interpolation)
33 : ZeroInflationIndex(source->familyName(), source->region(), source->revised(), source->interpolated(),
34 source->frequency(), source->availabilityLag(), source->currency(),
35 source->zeroInflationTermStructure()),
36 source_(source), interpolation_(interpolation) {}
37QL_DEPRECATED_ENABLE_WARNING
38
39Rate ZeroInflationIndexWrapper::fixing(const Date& fixingDate, bool /*forecastTodaysFixing*/) const {
40
41 // duplicated logic from CPICashFlow::amount()
42
43 // what interpolation do we use? Index / flat / linear
44 QL_DEPRECATED_DISABLE_WARNING
45 if (interpolation_ == CPI::AsIndex) {
46 return source_->fixing(fixingDate);
47 } else {
48 std::pair<Date, Date> dd = inflationPeriod(fixingDate, frequency());
49 Real indexStart = source_->fixing(dd.first);
50 if (interpolation_ == CPI::Linear) {
51 Real indexEnd = source_->fixing(dd.second + Period(1, Days));
52 // linear interpolation
53 return indexStart + (indexEnd - indexStart) * (fixingDate - dd.first) /
54 ((dd.second + Period(1, Days)) -
55 dd.first); // can't get to next period's value within current period
56 } else {
57 // no interpolation, i.e. flat = constant, so use start-of-period value
58 return indexStart;
59 }
60 }
61 QL_DEPRECATED_ENABLE_WARNING
62}
63
64YoYInflationIndexWrapper::YoYInflationIndexWrapper(const QuantLib::ext::shared_ptr<ZeroInflationIndex> zeroIndex,
65 const bool interpolated, const Handle<YoYInflationTermStructure>& ts)
66 : YoYInflationIndex(zeroIndex, interpolated, ts),
67 zeroIndex_(zeroIndex) {
68 registerWith(zeroIndex_);
69}
70
71Rate YoYInflationIndexWrapper::fixing(const Date& fixingDate, bool /*forecastTodaysFixing*/) const {
72
73 // duplicated logic from YoYInflationIndex, this would not be necessary, if forecastFixing
74 // was defined virtual in InflationIndex
75 if (needsForecast(fixingDate)) {
76 return forecastFixing(fixingDate);
77 }
78 // historical fixing
79 return YoYInflationIndex::fixing(fixingDate);
80}
81
82Real YoYInflationIndexWrapper::forecastFixing(const Date& fixingDate) const {
83 if (!yoyInflationTermStructure().empty())
84 return YoYInflationIndex::fixing(fixingDate);
85 auto interpolation = YoYInflationIndex::interpolated() ? CPI::Linear : CPI::Flat;
86 Real f1 = CPI::laggedFixing(zeroIndex_, fixingDate, 0 * Days, interpolation);
87 Real f0 = CPI::laggedFixing(zeroIndex_, fixingDate - 1 * Years, 0 * Days, interpolation);
88 return (f1 - f0) / f0;
89}
90
91} // namespace QuantExt
const QuantLib::ext::shared_ptr< ZeroInflationIndex > zeroIndex_
Rate forecastFixing(const Date &fixingDate) const
Rate fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
YoYInflationIndexWrapper(const QuantLib::ext::shared_ptr< ZeroInflationIndex > zeroIndex, const bool interpolated, const Handle< YoYInflationTermStructure > &ts=Handle< YoYInflationTermStructure >())
ZeroInflationIndexWrapper(const QuantLib::ext::shared_ptr< ZeroInflationIndex > source)
const QuantLib::ext::shared_ptr< ZeroInflationIndex > source_
QL_DEPRECATED const CPI::InterpolationType interpolation_
Rate fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
wrapper classes for inflation yoy and interpolation