Skip to content

Commit

Permalink
Merge pull request #1068.
Browse files Browse the repository at this point in the history
ZeroInflationCashFlow used by ZeroCouponInflationSwap
  • Loading branch information
lballabio committed Jul 1, 2021
2 parents abac3c4 + 267f44e commit 454d343
Show file tree
Hide file tree
Showing 26 changed files with 846 additions and 200 deletions.
1 change: 1 addition & 0 deletions LICENSE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ QuantLib is
Copyright (C) 2020, 2021 Marcin Rybacki

Copyright (C) 2021 Magnus Mencke
Copyright (C) 2021 Ralf Konrad Eckel

QuantLib includes code taken from Peter Jäckel's book "Monte Carlo
Methods in Finance".
Expand Down
2 changes: 2 additions & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
<ClInclude Include="ql\cashflows\subperiodcoupon.hpp" />
<ClInclude Include="ql\cashflows\timebasket.hpp" />
<ClInclude Include="ql\cashflows\yoyinflationcoupon.hpp" />
<ClInclude Include="ql\cashflows\zeroinflationcashflow.hpp" />
<ClInclude Include="ql\currencies\africa.hpp" />
<ClInclude Include="ql\currencies\all.hpp" />
<ClInclude Include="ql\currencies\america.hpp" />
Expand Down Expand Up @@ -1905,6 +1906,7 @@
<ClCompile Include="ql\cashflows\subperiodcoupon.cpp" />
<ClCompile Include="ql\cashflows\timebasket.cpp" />
<ClCompile Include="ql\cashflows\yoyinflationcoupon.cpp" />
<ClCompile Include="ql\cashflows\zeroinflationcashflow.cpp" />
<ClCompile Include="ql\currencies\africa.cpp" />
<ClCompile Include="ql\currencies\america.cpp" />
<ClCompile Include="ql\currencies\asia.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@
<ClInclude Include="ql\cashflows\yoyinflationcoupon.hpp">
<Filter>cashflows</Filter>
</ClInclude>
<ClInclude Include="ql\cashflows\zeroinflationcashflow.hpp">
<Filter>cashflows</Filter>
</ClInclude>
<ClInclude Include="ql\indexes\all.hpp">
<Filter>indexes</Filter>
</ClInclude>
Expand Down Expand Up @@ -4492,6 +4495,9 @@
<ClCompile Include="ql\cashflows\yoyinflationcoupon.cpp">
<Filter>cashflows</Filter>
</ClCompile>
<ClCompile Include="ql\cashflows\zeroinflationcashflow.cpp">
<Filter>cashflows</Filter>
</ClCompile>
<ClCompile Include="ql\indexes\bmaindex.cpp">
<Filter>indexes</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(QuantLib_SRC
cashflows/timebasket.cpp
cashflows/subperiodcoupon.cpp
cashflows/yoyinflationcoupon.cpp
cashflows/zeroinflationcashflow.cpp
currencies/africa.cpp
currencies/america.cpp
currencies/asia.cpp
Expand Down Expand Up @@ -955,6 +956,7 @@ set(QuantLib_HDR
cashflows/subperiodcoupon.hpp
cashflows/timebasket.hpp
cashflows/yoyinflationcoupon.hpp
cashflows/zeroinflationcashflow.hpp
compounding.hpp
config.ansi.hpp
config.mingw.hpp
Expand Down
6 changes: 4 additions & 2 deletions ql/cashflows/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ this_include_HEADERS = \
simplecashflow.hpp \
subperiodcoupon.hpp \
timebasket.hpp \
yoyinflationcoupon.hpp
yoyinflationcoupon.hpp \
zeroinflationcashflow.hpp

cpp_files = \
averagebmacoupon.cpp \
Expand Down Expand Up @@ -66,7 +67,8 @@ cpp_files = \
simplecashflow.cpp \
subperiodcoupon.cpp \
timebasket.cpp \
yoyinflationcoupon.cpp
yoyinflationcoupon.cpp \
zeroinflationcashflow.cpp

if UNITY_BUILD

Expand Down
1 change: 1 addition & 0 deletions ql/cashflows/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
#include <ql/cashflows/subperiodcoupon.hpp>
#include <ql/cashflows/timebasket.hpp>
#include <ql/cashflows/yoyinflationcoupon.hpp>
#include <ql/cashflows/zeroinflationcashflow.hpp>

9 changes: 0 additions & 9 deletions ql/cashflows/cpicoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@

namespace QuantLib {

struct CPI {
//! when you observe an index, how do you interpolate between fixings?
enum InterpolationType {
AsIndex, //!< same interpolation as index
Flat, //!< flat from previous fixing
Linear //!< linearly between bracketing fixings
};
};


class CPICouponPricer;

Expand Down
103 changes: 103 additions & 0 deletions ql/cashflows/zeroinflationcashflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Ralf Konrad Eckel
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

#include <ql/cashflows/zeroinflationcashflow.hpp>
#include <ql/indexes/inflationindex.hpp>
#include <ql/termstructures/inflationtermstructure.hpp>
#include <ql/time/calendars/nullcalendar.hpp>

namespace QuantLib {

ZeroInflationCashFlow::ZeroInflationCashFlow(Real notional,
const ext::shared_ptr<ZeroInflationIndex>& index,
CPI::InterpolationType observationInterpolation,
const Date& startDate,
const Date& endDate,
const Period& observationLag,
const Date& paymentDate,
bool growthOnly)
: ZeroInflationCashFlow(notional, index, observationInterpolation,
startDate, endDate, observationLag,
NullCalendar(), Unadjusted, paymentDate, growthOnly) {}

ZeroInflationCashFlow::ZeroInflationCashFlow(Real notional,
const ext::shared_ptr<ZeroInflationIndex>& index,
CPI::InterpolationType observationInterpolation,
const Date& startDate,
const Date& endDate,
const Period& observationLag,
const Calendar& calendar,
BusinessDayConvention convention,
const Date& paymentDate,
bool growthOnly)
: IndexedCashFlow(notional, index,
calendar.adjust(startDate - observationLag, convention),
calendar.adjust(endDate - observationLag, convention),
paymentDate, growthOnly),
zeroInflationIndex_(index), observationInterpolation_(observationInterpolation),
startDate_(startDate), endDate_(endDate), observationLag_(observationLag) {}

Real ZeroInflationCashFlow::amount() const {

auto baseDatePeriod = inflationPeriod(baseDate(), zeroInflationIndex()->frequency());
auto fixingDatePeriod = inflationPeriod(fixingDate(), zeroInflationIndex()->frequency());

Real I0, I1;

if (observationInterpolation_ == CPI::AsIndex) {
I0 = zeroInflationIndex_->fixing(baseDate());
I1 = zeroInflationIndex_->fixing(fixingDate());
} else if (observationInterpolation_ == CPI::Linear) {
auto getInterpolatedFixing = [this](const std::pair<Date, Date>& fixingPeriod,
const Date& date) -> Real {
auto oneDay = Period(1, Days);
auto startIndex = zeroInflationIndex_->fixing(fixingPeriod.first);
auto endIndex = zeroInflationIndex_->fixing(fixingPeriod.second + oneDay);

auto interpolationPeriod = inflationPeriod(date, zeroInflationIndex()->frequency());

return startIndex + (endIndex - startIndex) * (date - interpolationPeriod.first) /
(Real)((interpolationPeriod.second + oneDay) - interpolationPeriod.first);
};

I0 = getInterpolatedFixing(baseDatePeriod, startDate_);
I1 = getInterpolatedFixing(fixingDatePeriod, endDate_);
} else if (observationInterpolation_ == CPI::Flat) {
I0 = zeroInflationIndex_->fixing(baseDatePeriod.first);
I1 = zeroInflationIndex_->fixing(fixingDatePeriod.first);
} else {
// We should not end up here...
QL_FAIL("Unknown ZeroInflationInterpolationType.");
}

if (growthOnly())
return notional() * (I1 / I0 - 1.0);
else
return notional() * (I1 / I0);
}

void ZeroInflationCashFlow::accept(AcyclicVisitor& v) {
auto* v1 = dynamic_cast<Visitor<ZeroInflationCashFlow>*>(&v);
if (v1 != nullptr)
v1->visit(*this);
else
IndexedCashFlow::accept(v);
}

}
94 changes: 94 additions & 0 deletions ql/cashflows/zeroinflationcashflow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Ralf Konrad Eckel
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

/*! \file zeroinflationcashflow.hpp
\brief Cash flow dependent on an inflation index ratio (NOT a coupon, i.e. no accruals).
*/

#ifndef quantlib_inflation_cash_flow_hpp
#define quantlib_inflation_cash_flow_hpp

#include <ql/cashflows/indexedcashflow.hpp>
#include <ql/indexes/inflationindex.hpp>

namespace QuantLib {

//! Cash flow dependent on a zero inflation index ratio.
/*! The ratio is taken between fixings observed at the start date
and the end date minus the observation lag; that is, if the start
and end dates are, e.g., in June and the observation lag is three
months, the ratio will be taken between March fixings.
*/
class ZeroInflationCashFlow : public IndexedCashFlow {
public:
/*! The fixings dates for the index are `startDate - observationLag` and
`endDate - observationLag`.
*/
ZeroInflationCashFlow(Real notional,
const ext::shared_ptr<ZeroInflationIndex>& index,
CPI::InterpolationType observationInterpolation,
const Date& startDate,
const Date& endDate,
const Period& observationLag,
const Date& paymentDate,
bool growthOnly = false);

/*! The fixings dates for the index are `startDate - observationLag` and
`endDate - observationLag`, adjusted on the passed calendar.
*/
ZeroInflationCashFlow(Real notional,
const ext::shared_ptr<ZeroInflationIndex>& index,
CPI::InterpolationType observationInterpolation,
const Date& startDate,
const Date& endDate,
const Period& observationLag,
const Calendar& calendar,
BusinessDayConvention convention,
const Date& paymentDate,
bool growthOnly = false);

//! \name ZeroInflationCashFlow interface
//@{
ext::shared_ptr<ZeroInflationIndex> zeroInflationIndex() const {
return zeroInflationIndex_;
}
CPI::InterpolationType observationInterpolation() const {
return observationInterpolation_;
}
//@}

//! \name CashFlow interface
//@{
Real amount() const override;
//@}
//! \name Visitability
//@{
void accept(AcyclicVisitor&) override;
//@}

private:
ext::shared_ptr<ZeroInflationIndex> zeroInflationIndex_;
CPI::InterpolationType observationInterpolation_;
Date startDate_, endDate_;
Period observationLag_;
};

}

#endif
15 changes: 13 additions & 2 deletions ql/indexes/inflationindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/*
Copyright (C) 2007 Chris Kenyon
Copyright (C) 2021 Ralf Konrad Eckel
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace QuantLib {
bool forceOverwrite) {

std::pair<Date,Date> lim = inflationPeriod(fixingDate, frequency_);
Size n = lim.second - lim.first + 1;
Size n = static_cast<QuantLib::Size>(lim.second - lim.first) + 1;
std::vector<Date> dates(n);
std::vector<Rate> rates(n);
for (Size i=0; i<n; ++i) {
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace QuantLib {
// the term structure is relative to the fixing value at the base date.
Date baseDate = zeroInflation_->baseDate();
QL_REQUIRE(!needsForecast(baseDate),
name() << " index fixing at base date is not available");
name() << " index fixing at base date " << baseDate << " is not available");
Real baseFixing = fixing(baseDate);
Date effectiveFixingDate;
if (interpolated()) {
Expand Down Expand Up @@ -337,4 +338,14 @@ namespace QuantLib {
availabilityLag_, currency_, h);
}


CPI::InterpolationType
detail::CPI::effectiveInterpolationType(const ext::shared_ptr<ZeroInflationIndex>& index,
const QuantLib::CPI::InterpolationType& type) {
if (type == QuantLib::CPI::AsIndex) {
return index->interpolated() ? QuantLib::CPI::Linear : QuantLib::CPI::Flat;
} else {
return type;
}
}
}
Loading

0 comments on commit 454d343

Please sign in to comment.