Skip to content

Commit

Permalink
Add constructors for YoY inflation curves taking the base date
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jan 25, 2024
1 parent 7e0554a commit 0f27598
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 44 deletions.
19 changes: 12 additions & 7 deletions ql/experimental/inflation/yoycapfloortermpricesurface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ namespace QuantLib {
bool extrapolate = true) const override {
// work in terms of maturity-of-instruments
// so ask for rate with observation lag
Period p = (obsLag == Period(-1, Days)) ? observationLag() : obsLag;
// Third parameter = force linear interpolation of yoy
return yoy_->yoyRate(d, obsLag, false, extrapolate);
return yoy_->yoyRate(d, p, false, extrapolate);
}
//@}

Expand Down Expand Up @@ -540,18 +541,22 @@ namespace QuantLib {
YYhelpers.push_back (anInstrument);
}

Date baseDate =
yoyIndex()->interpolated() ?
nominalTS_->referenceDate() - observationLag() :
inflationPeriod(nominalTS_->referenceDate() - observationLag(),
yoyIndex()->frequency()).first;
// usually this base rate is known
// however for the data to be self-consistent
// we pick this as the end of the curve
Rate baseYoYRate = atmYoYSwapRate( referenceDate() );//!

// Linear is OK because we have every year
ext::shared_ptr<PiecewiseYoYInflationCurve<Linear> > pYITS(
new PiecewiseYoYInflationCurve<Linear>(
nominalTS_->referenceDate(),
calendar(), dayCounter(), observationLag(), yoyIndex()->frequency(),
yoyIndex()->interpolated(), baseYoYRate,
YYhelpers));
auto pYITS =
ext::make_shared<PiecewiseYoYInflationCurve<Linear>>(
nominalTS_->referenceDate(), baseDate, baseYoYRate,
yoyIndex()->frequency(), yoyIndex()->interpolated(),
dayCounter(), YYhelpers);
pYITS->recalculate();
yoy_ = pYITS; // store

Expand Down
1 change: 1 addition & 0 deletions ql/termstructures/inflation/inflationtraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace QuantLib {
t->frequency()).first;
}
}

// value at reference date
static Rate initialValue(const YoYInflationTermStructure* t) {
return t->baseRate();
Expand Down
90 changes: 86 additions & 4 deletions ql/termstructures/inflation/interpolatedyoyinflationcurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ namespace QuantLib {
: public YoYInflationTermStructure,
protected InterpolatedCurve<Interpolator> {
public:
InterpolatedYoYInflationCurve(const Date& referenceDate,
std::vector<Date> dates,
const std::vector<Rate>& rates,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality>& seasonality = {},
const Interpolator& interpolator = Interpolator());

/*! \deprecated Use the other overload and pass the base date directly
as the first date in the vector instead of using a lag.
Deprecated in version 1.34.
*/
QL_DEPRECATED
InterpolatedYoYInflationCurve(const Date& referenceDate,
const Calendar& calendar,
const DayCounter& dayCounter,
Expand Down Expand Up @@ -80,15 +94,28 @@ namespace QuantLib {
(or can't) provide the points for interpolation on
construction.
*/
InterpolatedYoYInflationCurve(const Date& referenceDate,
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality>& seasonality = {},
const Interpolator& interpolator = Interpolator());

/*! \deprecated Use the other overload and pass the base date directly
instead of using a lag.
Deprecated in version 1.34.
*/
QL_DEPRECATED
InterpolatedYoYInflationCurve(const Date& referenceDate,
const Calendar& calendar,
const DayCounter& dayCounter,
Rate baseYoYRate,
const Period& lag,
Frequency frequency,
bool indexIsInterpolated,
const Interpolator& interpolator
= Interpolator());
const Interpolator& interpolator = Interpolator());
};

typedef InterpolatedYoYInflationCurve<Linear> YoYInflationCurve;
Expand All @@ -97,6 +124,56 @@ namespace QuantLib {

// template definitions

template <class Interpolator>
InterpolatedYoYInflationCurve<Interpolator>::InterpolatedYoYInflationCurve(
const Date& referenceDate,
std::vector<Date> dates,
const std::vector<Rate>& rates,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality>& seasonality,
const Interpolator& interpolator)
: YoYInflationTermStructure(referenceDate, dates.at(0), rates[0], frequency,
indexIsInterpolated, dayCounter, seasonality),
InterpolatedCurve<Interpolator>(std::vector<Time>(), rates, interpolator),
dates_(std::move(dates)) {

QL_REQUIRE(dates_.size()>1, "too few dates: " << dates_.size());

QL_REQUIRE(this->data_.size() == dates_.size(),
"indices/dates count mismatch: "
<< this->data_.size() << " vs " << dates_.size());

for (Size i = 1; i < dates_.size(); i++) {
// YoY inflation data may be positive or negative
// but must be greater than -1
QL_REQUIRE(this->data_[i] > -1.0,
"year-on-year inflation data < -100 %");
}

this->setupTimes(dates_, referenceDate, dayCounter);
this->setupInterpolation();
this->interpolation_.update();
}

template <class Interpolator>
InterpolatedYoYInflationCurve<Interpolator>::
InterpolatedYoYInflationCurve(const Date& referenceDate,
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality>& seasonality,
const Interpolator& interpolator)
: YoYInflationTermStructure(referenceDate, baseDate, baseYoYRate, frequency,
indexIsInterpolated, dayCounter, seasonality),
InterpolatedCurve<Interpolator>(interpolator) {}


QL_DEPRECATED_DISABLE_WARNING

template <class Interpolator>
InterpolatedYoYInflationCurve<Interpolator>::InterpolatedYoYInflationCurve(
const Date& referenceDate,
Expand Down Expand Up @@ -154,10 +231,15 @@ namespace QuantLib {
lag, frequency, indexIsInterpolated),
InterpolatedCurve<Interpolator>(interpolator) {}

QL_DEPRECATED_ENABLE_WARNING


template <class T>
Date InterpolatedYoYInflationCurve<T>::baseDate() const{
return dates_.front();
Date InterpolatedYoYInflationCurve<T>::baseDate() const {
if (hasExplicitBaseDate())
return YoYInflationTermStructure::baseDate();
else
return dates_.front();
}

template <class T>
Expand Down
36 changes: 35 additions & 1 deletion ql/termstructures/inflation/piecewiseyoyinflationcurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ namespace QuantLib {
typedef Interpolator interpolator_type;
//! \name Constructors
//@{
PiecewiseYoYInflationCurve(
const Date& referenceDate,
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
std::vector<ext::shared_ptr<typename Traits::helper> > instruments,
const ext::shared_ptr<Seasonality>& seasonality = {},
Real accuracy = 1.0e-12,
const Interpolator& i = Interpolator())
: base_curve(referenceDate,
baseDate,
baseYoYRate,
frequency,
indexIsInterpolated,
dayCounter,
seasonality,
i),
instruments_(std::move(instruments)), accuracy_(accuracy) {
bootstrap_.setup(this);
}


QL_DEPRECATED_DISABLE_WARNING

/*! \deprecated Use the other overload and pass the base date directly
instead of using a lag.
Deprecated in version 1.34.
*/
QL_DEPRECATED
PiecewiseYoYInflationCurve(
const Date& referenceDate,
const Calendar& calendar,
Expand All @@ -71,6 +102,8 @@ namespace QuantLib {
instruments_(std::move(instruments)), accuracy_(accuracy) {
bootstrap_.setup(this);
}

QL_DEPRECATED_ENABLE_WARNING
//@}

//! \name Inflation interface
Expand Down Expand Up @@ -106,7 +139,8 @@ namespace QuantLib {

template <class I, template <class> class B, class T>
inline Date PiecewiseYoYInflationCurve<I,B,T>::baseDate() const {
this->calculate();
if (!this->hasExplicitBaseDate())
this->calculate();
return base_curve::baseDate();
}

Expand Down
35 changes: 35 additions & 0 deletions ql/termstructures/inflationtermstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,41 @@ namespace QuantLib {
}


YoYInflationTermStructure::YoYInflationTermStructure(
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality> &seasonality)
: InflationTermStructure(baseDate, frequency, dayCounter, seasonality, baseYoYRate),
indexIsInterpolated_(indexIsInterpolated) {}

YoYInflationTermStructure::YoYInflationTermStructure(
const Date& referenceDate,
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality> &seasonality)
: InflationTermStructure(referenceDate, baseDate, frequency,
dayCounter, seasonality, baseYoYRate),
indexIsInterpolated_(indexIsInterpolated) {}

YoYInflationTermStructure::YoYInflationTermStructure(
Natural settlementDays,
const Calendar& calendar,
Date baseDate,
Rate baseYoYRate,
Frequency frequency,
bool indexIsInterpolated,
const DayCounter& dayCounter,
const ext::shared_ptr<Seasonality> &seasonality)
: InflationTermStructure(settlementDays, calendar, baseDate, frequency,
dayCounter, seasonality, baseYoYRate),
indexIsInterpolated_(indexIsInterpolated) {}

QL_DEPRECATED_DISABLE_WARNING

YoYInflationTermStructure::YoYInflationTermStructure(
Expand Down
Loading

0 comments on commit 0f27598

Please sign in to comment.