Skip to content

Commit

Permalink
Pass extrapolate to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sweemer committed Aug 3, 2024
1 parent 0e83ab7 commit b22193d
Show file tree
Hide file tree
Showing 28 changed files with 215 additions and 137 deletions.
17 changes: 7 additions & 10 deletions ql/cashflows/cashflows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ namespace QuantLib {
bps_ += bps;
}
void visit(CashFlow& cf) override {
nonSensNPV_ += cf.amount() *
nonSensNPV_ += cf.amount() *
discountCurve_.discount(cf.date());
}
Real bps() const { return bps_; }
Expand Down Expand Up @@ -1157,7 +1157,10 @@ namespace QuantLib {
Date npvDate)
: leg_(leg), npv_(npv), zSpread_(new SimpleQuote(0.0)),
curve_(Handle<YieldTermStructure>(discountCurve),
Handle<Quote>(zSpread_), comp, freq, dc),
Handle<Quote>(zSpread_), comp, freq, dc,
// if the discount curve allows extrapolation,
// then the spreaded curve should too
discountCurve->allowsExtrapolation()),
includeSettlementDateFlows_(includeSettlementDateFlows),
settlementDate_(settlementDate),
npvDate_(npvDate) {
Expand All @@ -1167,11 +1170,6 @@ namespace QuantLib {

if (npvDate_ == Date())
npvDate_ = settlementDate_;

// if the discount curve allows extrapolation, let's
// the spreaded curve do too.
curve_.enableExtrapolation(
discountCurve->allowsExtrapolation());
}
Real operator()(Rate zSpread) const {
zSpread_->setValue(zSpread);
Expand Down Expand Up @@ -1216,9 +1214,8 @@ namespace QuantLib {

ZeroSpreadedTermStructure spreadedCurve(discountCurveHandle,
zSpreadQuoteHandle,
comp, freq, dc);

spreadedCurve.enableExtrapolation(discountCurveHandle->allowsExtrapolation());
comp, freq, dc,
discountCurveHandle->allowsExtrapolation());

return npv(leg, spreadedCurve,
includeSettlementDateFlows,
Expand Down
3 changes: 3 additions & 0 deletions ql/math/interpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ namespace QuantLib {
I2 yBegin_;
};

explicit Interpolation(bool extrapolate)
: Extrapolator(extrapolate) {}

Interpolation() = default;
~Interpolation() override = default;
bool empty() const { return !impl_; }
Expand Down
7 changes: 4 additions & 3 deletions ql/math/interpolations/bilinearinterpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ namespace QuantLib {
template <class I1, class I2, class M>
BilinearInterpolation(const I1& xBegin, const I1& xEnd,
const I2& yBegin, const I2& yEnd,
const M& zData) {
const M& zData, bool extrapolate = false)
: Interpolation2D(extrapolate) {
impl_ = ext::shared_ptr<Interpolation2D::Impl>(
new detail::BilinearInterpolationImpl<I1,I2,M>(xBegin, xEnd,
yBegin, yEnd,
Expand All @@ -89,8 +90,8 @@ namespace QuantLib {
template <class I1, class I2, class M>
Interpolation2D interpolate(const I1& xBegin, const I1& xEnd,
const I2& yBegin, const I2& yEnd,
const M& z) const {
return BilinearInterpolation(xBegin,xEnd,yBegin,yEnd,z);
const M& z, bool extrapolate = false) const {
return BilinearInterpolation(xBegin,xEnd,yBegin,yEnd,z,extrapolate);
}
};

Expand Down
75 changes: 50 additions & 25 deletions ql/math/interpolations/cubicinterpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace QuantLib {
Akima,

//! Kruger approximation (local, monotonic, non-linear)
Kruger,
Kruger,

//! Weighted harmonic mean approximation (local, monotonic, non-linear)
Harmonic,
Expand Down Expand Up @@ -163,7 +163,9 @@ namespace QuantLib {
CubicInterpolation::BoundaryCondition leftCond,
Real leftConditionValue,
CubicInterpolation::BoundaryCondition rightCond,
Real rightConditionValue) {
Real rightConditionValue,
bool extrapolate = false)
: Interpolation(extrapolate) {
impl_ = ext::shared_ptr<Interpolation::Impl>(new
detail::CubicInterpolationImpl<I1,I2>(xBegin, xEnd, yBegin,
da,
Expand Down Expand Up @@ -198,11 +200,13 @@ namespace QuantLib {
template <class I1, class I2>
CubicNaturalSpline(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Spline, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class MonotonicCubicNaturalSpline : public CubicInterpolation {
Expand All @@ -211,11 +215,13 @@ namespace QuantLib {
template <class I1, class I2>
MonotonicCubicNaturalSpline(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Spline, true,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class CubicSplineOvershootingMinimization1 : public CubicInterpolation {
Expand All @@ -224,11 +230,13 @@ namespace QuantLib {
template <class I1, class I2>
CubicSplineOvershootingMinimization1 (const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
SplineOM1, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class CubicSplineOvershootingMinimization2 : public CubicInterpolation {
Expand All @@ -237,11 +245,13 @@ namespace QuantLib {
template <class I1, class I2>
CubicSplineOvershootingMinimization2 (const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
SplineOM2, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class AkimaCubicInterpolation : public CubicInterpolation {
Expand All @@ -250,11 +260,13 @@ namespace QuantLib {
template <class I1, class I2>
AkimaCubicInterpolation(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Akima, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class KrugerCubic : public CubicInterpolation {
Expand All @@ -263,11 +275,13 @@ namespace QuantLib {
template <class I1, class I2>
KrugerCubic(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Kruger, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class HarmonicCubic : public CubicInterpolation {
Expand All @@ -276,11 +290,13 @@ namespace QuantLib {
template <class I1, class I2>
HarmonicCubic(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Harmonic, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class FritschButlandCubic : public CubicInterpolation {
Expand All @@ -289,11 +305,13 @@ namespace QuantLib {
template <class I1, class I2>
FritschButlandCubic(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
FritschButland, true,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class Parabolic : public CubicInterpolation {
Expand All @@ -302,11 +320,13 @@ namespace QuantLib {
template <class I1, class I2>
Parabolic(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
CubicInterpolation::Parabolic, false,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

class MonotonicParabolic : public CubicInterpolation {
Expand All @@ -315,11 +335,13 @@ namespace QuantLib {
template <class I1, class I2>
MonotonicParabolic(const I1& xBegin,
const I1& xEnd,
const I2& yBegin)
const I2& yBegin,
bool extrapolate = false)
: CubicInterpolation(xBegin, xEnd, yBegin,
Parabolic, true,
SecondDerivative, 0.0,
SecondDerivative, 0.0) {}
SecondDerivative, 0.0,
extrapolate) {}
};

//! %Cubic interpolation factory and traits
Expand All @@ -334,7 +356,8 @@ namespace QuantLib {
Real leftConditionValue = 0.0,
CubicInterpolation::BoundaryCondition rightCondition
= CubicInterpolation::SecondDerivative,
Real rightConditionValue = 0.0)
Real rightConditionValue = 0.0,
bool extrapolate = false)
: da_(da), monotonic_(monotonic),
leftType_(leftCondition), rightType_(rightCondition),
leftValue_(leftConditionValue), rightValue_(rightConditionValue) {}
Expand All @@ -345,7 +368,8 @@ namespace QuantLib {
return CubicInterpolation(xBegin, xEnd, yBegin,
da_, monotonic_,
leftType_, leftValue_,
rightType_, rightValue_);
rightType_, rightValue_,
extrapolate_);
}
static const bool global = true;
static const Size requiredPoints = 2;
Expand All @@ -354,6 +378,7 @@ namespace QuantLib {
bool monotonic_;
CubicInterpolation::BoundaryCondition leftType_, rightType_;
Real leftValue_, rightValue_;
bool extrapolate_;
};


Expand Down Expand Up @@ -385,7 +410,7 @@ namespace QuantLib {
|| rightType_ == CubicInterpolation::Lagrange) {
QL_REQUIRE((xEnd-xBegin) >= 4,
"Lagrange boundary condition requires at least "
"4 points (" << (xEnd-xBegin) << " are given)");
"4 points (" << (xEnd-xBegin) << " are given)");
}
}

Expand Down
2 changes: 2 additions & 0 deletions ql/math/interpolations/extrapolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace QuantLib {
//! base class for classes possibly allowing extrapolation
class Extrapolator {
public:
explicit Extrapolator(bool extrapolate)
: extrapolate_(extrapolate) {}
Extrapolator() = default;
virtual ~Extrapolator() = default;
//! \name modifiers
Expand Down
4 changes: 4 additions & 0 deletions ql/math/interpolations/interpolation2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ namespace QuantLib {
};

Interpolation2D() = default;

explicit Interpolation2D(bool extrapolate)
: Extrapolator(extrapolate) {}

Real operator()(Real x, Real y,
bool allowExtrapolation = false) const {
checkRange(x,y,allowExtrapolation);
Expand Down
3 changes: 2 additions & 1 deletion ql/math/interpolations/linearinterpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace QuantLib {
/*! \pre the \f$ x \f$ values must be sorted. */
template <class I1, class I2>
LinearInterpolation(const I1& xBegin, const I1& xEnd,
const I2& yBegin) {
const I2& yBegin, bool extrapolate = false)
: Interpolation(extrapolate) {
impl_ = ext::shared_ptr<Interpolation::Impl>(new
detail::LinearInterpolationImpl<I1,I2>(xBegin, xEnd,
yBegin));
Expand Down
12 changes: 6 additions & 6 deletions ql/termstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

namespace QuantLib {

TermStructure::TermStructure(DayCounter dc)
: settlementDays_(Null<Natural>()), dayCounter_(std::move(dc)) {}
TermStructure::TermStructure(DayCounter dc, bool extrapolate)
: Extrapolator(extrapolate), settlementDays_(Null<Natural>()), dayCounter_(std::move(dc)) {}

TermStructure::TermStructure(const Date& referenceDate, Calendar cal, DayCounter dc)
: calendar_(std::move(cal)), referenceDate_(referenceDate), settlementDays_(Null<Natural>()),
TermStructure::TermStructure(const Date& referenceDate, Calendar cal, DayCounter dc, bool extrapolate)
: Extrapolator(extrapolate), calendar_(std::move(cal)), referenceDate_(referenceDate), settlementDays_(Null<Natural>()),
dayCounter_(std::move(dc)) {}

TermStructure::TermStructure(Natural settlementDays, Calendar cal, DayCounter dc)
: moving_(true), updated_(false), calendar_(std::move(cal)), settlementDays_(settlementDays),
TermStructure::TermStructure(Natural settlementDays, Calendar cal, DayCounter dc, bool extrapolate)
: Extrapolator(extrapolate), moving_(true), updated_(false), calendar_(std::move(cal)), settlementDays_(settlementDays),
dayCounter_(std::move(dc)) {
registerWith(Settings::instance().evaluationDate());
}
Expand Down
11 changes: 8 additions & 3 deletions ql/termstructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ namespace QuantLib {
constructor must manage their own reference date
by overriding the referenceDate() method.
*/
explicit TermStructure(DayCounter dc = DayCounter());
explicit TermStructure(DayCounter dc = DayCounter(),
bool extrapolate = false);
//! initialize with a fixed reference date
explicit TermStructure(const Date& referenceDate,
Calendar calendar = Calendar(),
DayCounter dc = DayCounter());
DayCounter dc = DayCounter(),
bool extrapolate = false);
//! calculate the reference date based on the global evaluation date
TermStructure(Natural settlementDays, Calendar, DayCounter dc = DayCounter());
TermStructure(Natural settlementDays,
Calendar calendar,
DayCounter dc = DayCounter(),
bool extrapolate = false);
//@}
~TermStructure() override = default;
//! \name Dates and Time
Expand Down
Loading

0 comments on commit b22193d

Please sign in to comment.