Skip to content

Commit

Permalink
Simplify Null<T> implementation (#2082)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored Oct 3, 2024
2 parents e366a83 + ef12f99 commit 8bd5703
Show file tree
Hide file tree
Showing 44 changed files with 120 additions and 195 deletions.
10 changes: 5 additions & 5 deletions ql/cashflows/cpicoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace QuantLib {
const Date& refPeriodStart,
const Date& refPeriodEnd,
const Date& exCouponDate)
: CPICoupon(baseCPI, Null<Date>(), paymentDate, nominal, startDate, endDate,
: CPICoupon(baseCPI, Date(), paymentDate, nominal, startDate, endDate,
index, observationLag, observationInterpolation, dayCounter,
fixedRate, refPeriodStart, refPeriodEnd, exCouponDate) {}

Expand Down Expand Up @@ -84,7 +84,7 @@ namespace QuantLib {
observationInterpolation_(observationInterpolation), baseDate_(baseDate) {

QL_REQUIRE(index_, "no index provided");
QL_REQUIRE(baseCPI_ != Null<Rate>() || baseDate != Null<Date>(),
QL_REQUIRE(baseCPI_ != Null<Rate>() || baseDate != Date(),
"baseCPI and baseDate can not be both null, provide a valid baseCPI or baseDate");
QL_REQUIRE(baseCPI_ == Null<Rate>() || std::fabs(baseCPI_) > 1e-16,
"|baseCPI_| < 1e-16, future divide-by-zero problem");
Expand Down Expand Up @@ -150,7 +150,7 @@ namespace QuantLib {
interpolation_(interpolation), frequency_(index ? index->frequency() : NoFrequency) {
QL_REQUIRE(index, "no index provided");
QL_REQUIRE(
baseFixing_ != Null<Rate>() || baseDate != Null<Date>(),
baseFixing_ != Null<Rate>() || baseDate != Date(),
"baseCPI and baseDate can not be both null, provide a valid baseCPI or baseDate");
QL_REQUIRE(baseFixing_ == Null<Rate>() || std::fabs(baseFixing_) > 1e-16,
"|baseCPI_| < 1e-16, future divide-by-zero problem");
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace QuantLib {
const Period& observationLag)
: schedule_(std::move(schedule)), index_(std::move(index)), baseCPI_(baseCPI),
observationLag_(observationLag), paymentDayCounter_(Thirty360(Thirty360::BondBasis)),
paymentCalendar_(schedule_.calendar()), baseDate_(Null<Date>()) {}
paymentCalendar_(schedule_.calendar()) {}


CPILeg& CPILeg::withObservationInterpolation(CPI::InterpolationType interp) {
Expand Down Expand Up @@ -283,7 +283,7 @@ namespace QuantLib {
if (n>0) {
QL_REQUIRE(!fixedRates_.empty(), "no fixedRates given");

if (baseDate_ == Null<Date>() && baseCPI_ == Null<Real>()) {
if (baseDate_ == Date() && baseCPI_ == Null<Real>()) {
baseDate = schedule_.date(0) - observationLag_;
}

Expand Down
2 changes: 1 addition & 1 deletion ql/cashflows/cpicoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace QuantLib {
Calendar exCouponCalendar_;
BusinessDayConvention exCouponAdjustment_ = Following;
bool exCouponEndOfMonth_ = false;
Date baseDate_ = Null<Date>();
Date baseDate_ = Date();
};


Expand Down
4 changes: 2 additions & 2 deletions ql/cashflows/timebasket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace QuantLib {
for (auto j : *this) {
Date date = j.first;
Real value = j.second;
Date pDate = Null<Date>(), nDate = Null<Date>();
Date pDate = Date(), nDate = Date();

auto bi =
std::lower_bound(sbuckets.begin(), sbuckets.end(), date);
Expand All @@ -60,7 +60,7 @@ namespace QuantLib {
if (bi != sbuckets.begin() && bi != sbuckets.end())
nDate = *(bi-1);

if (pDate == date || nDate == Null<Date>()) {
if (pDate == date || nDate == Date()) {
result[pDate] += value;
} else {
Real pDays = Real(pDate-date);
Expand Down
4 changes: 2 additions & 2 deletions ql/experimental/credit/defaultevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace QuantLib {
: bondsCurrency_(std::move(curr)), defaultDate_(creditEventDate), eventType_(atomicEvType),
bondsSeniority_(bondsSen),
defSettlement_(settleDate, recoveryRates.empty() ? makeIsdaConvMap() : recoveryRates) {
if(settleDate != Null<Date>()) {// has settled
if (settleDate != Date()) {// has settled
QL_REQUIRE(settleDate >= creditEventDate,
"Settlement date should be after default date.");
QL_REQUIRE(recoveryRates.find(bondsSen) != recoveryRates.end(),
Expand All @@ -114,7 +114,7 @@ namespace QuantLib {
Real recoveryRate)
: bondsCurrency_(std::move(curr)), defaultDate_(creditEventDate), eventType_(atomicEvType),
bondsSeniority_(bondsSen), defSettlement_(settleDate, bondsSen, recoveryRate) {
if(settleDate != Null<Date>()) {
if (settleDate != Date()) {
QL_REQUIRE(settleDate >= creditEventDate,
"Settlement date should be after default date.");
}
Expand Down
4 changes: 2 additions & 2 deletions ql/experimental/credit/defaultevent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace QuantLib {
Currency curr,
Seniority bondsSen,
// Settlement information:
const Date& settleDate = Null<Date>(),
const Date& settleDate = Date(),
const std::map<Seniority, Real>& recoveryRates = rate_map());
/*! Use NoSeniority to settle to all seniorities with that
recovery. In that case the event is assumed to have
Expand All @@ -114,7 +114,7 @@ namespace QuantLib {
bool isRestructuring() const { return eventType_.isRestructuring(); }
bool isDefault() const { return !isRestructuring();}
bool hasSettled() const {
return defSettlement_.date() != Null<Date>();
return defSettlement_.date() != Date();
}
const DefaultSettlement& settlement() const {
return defSettlement_;
Expand Down
5 changes: 2 additions & 3 deletions ql/experimental/exoticoptions/partialtimebarrieroption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ namespace QuantLib {
PartialTimeBarrierOption::arguments::arguments()
: barrierType(PartialBarrier::Type(-1)),
barrierRange(PartialBarrier::Range(-1)),
barrier(Null<Real>()), rebate(Null<Real>()),
coverEventDate(Null<Date>()) {}
barrier(Null<Real>()), rebate(Null<Real>()) {}

void PartialTimeBarrierOption::arguments::validate() const {
OneAssetOption::arguments::validate();
Expand All @@ -78,7 +77,7 @@ namespace QuantLib {

QL_REQUIRE(barrier != Null<Real>(), "no barrier given");
QL_REQUIRE(rebate != Null<Real>(), "no rebate given");
QL_REQUIRE(coverEventDate != Null<Date>(), "no cover event date given");
QL_REQUIRE(coverEventDate != Date(), "no cover event date given");
QL_REQUIRE(coverEventDate < exercise->lastDate(),
"cover event date equal or later than exercise date");
}
Expand Down
2 changes: 1 addition & 1 deletion ql/instruments/bond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace QuantLib {
}

Date Bond::maturityDate() const {
if (maturityDate_!=Null<Date>())
if (maturityDate_ != Date())
return maturityDate_;
else
return BondFunctions::maturityDate(*this);
Expand Down
12 changes: 5 additions & 7 deletions ql/instruments/creditdefaultswap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace QuantLib {
: side_(side), notional_(notional), upfront_(ext::nullopt), runningSpread_(spread),
settlesAccrual_(settlesAccrual), paysAtDefaultTime_(paysAtDefaultTime),
claim_(std::move(claim)),
protectionStart_(protectionStart == Null<Date>() ? schedule[0] : protectionStart),
protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays) {

init(schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual);
Expand All @@ -78,7 +78,7 @@ namespace QuantLib {
: side_(side), notional_(notional), upfront_(upfront), runningSpread_(runningSpread),
settlesAccrual_(settlesAccrual), paysAtDefaultTime_(paysAtDefaultTime),
claim_(std::move(claim)),
protectionStart_(protectionStart == Null<Date>() ? schedule[0] : protectionStart),
protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays) {

init(schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual, upfrontDate);
Expand Down Expand Up @@ -460,10 +460,8 @@ namespace QuantLib {
QL_REQUIRE(!leg.empty(), "coupons not set");
QL_REQUIRE(upfrontPayment, "upfront payment not set");
QL_REQUIRE(claim, "claim not set");
QL_REQUIRE(protectionStart != Null<Date>(),
"protection start date not set");
QL_REQUIRE(maturity != Null<Date>(),
"maturity date not set");
QL_REQUIRE(protectionStart != Date(), "protection start date not set");
QL_REQUIRE(maturity != Date(), "maturity date not set");
}

void CreditDefaultSwap::results::reset() {
Expand Down Expand Up @@ -494,7 +492,7 @@ namespace QuantLib {
if (rule == DateGeneration::CDS2015 && (anchorDate == Date(20, Dec, anchorDate.year()) ||
anchorDate == Date(20, Jun, anchorDate.year()))) {
if (tenor.length() == 0) {
return Null<Date>();
return Date();
} else {
anchorDate -= 3 * Months;
}
Expand Down
2 changes: 1 addition & 1 deletion ql/instruments/creditdefaultswap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ namespace QuantLib {

/*! Return the CDS maturity date given the CDS trade date, \p tradeDate, the CDS \p tenor and a CDS \p rule.
A \c Null<Date>() is returned when a \p rule of \c CDS2015 and a \p tenor length of zero fail to yield a valid
A null date is returned when a \p rule of \c CDS2015 and a \p tenor length of zero fail to yield a valid
CDS maturity date.
\warning An exception will be thrown if the \p rule is not \c CDS2015, \c CDS or \c OldCDS.
Expand Down
5 changes: 2 additions & 3 deletions ql/instruments/forwardvanillaoption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ namespace QuantLib {
template <class ArgumentsType>
class ForwardOptionArguments : public ArgumentsType {
public:
ForwardOptionArguments() : moneyness(Null<Real>()),
resetDate(Null<Date>()) {}
ForwardOptionArguments() : moneyness(Null<Real>()) {}
void validate() const override;
Real moneyness;
Date resetDate;
Expand Down Expand Up @@ -72,7 +71,7 @@ namespace QuantLib {
QL_REQUIRE(moneyness != Null<Real>(), "null moneyness given");
QL_REQUIRE(moneyness > 0.0, "negative or zero moneyness given");

QL_REQUIRE(resetDate != Null<Date>(), "null reset date given");
QL_REQUIRE(resetDate != Date(), "null reset date given");
QL_REQUIRE(resetDate >= Settings::instance().evaluationDate(),
"reset date in the past");
QL_REQUIRE(this->exercise->lastDate() > resetDate,
Expand Down
2 changes: 1 addition & 1 deletion ql/instruments/makecds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace QuantLib {

MakeCreditDefaultSwap::operator ext::shared_ptr<CreditDefaultSwap>() const {

Date tradeDate = (tradeDate_ != Null<Date>()) ? tradeDate_ : Settings::instance().evaluationDate();
Date tradeDate = (tradeDate_ != Date()) ? tradeDate_ : Settings::instance().evaluationDate();
Date upfrontDate = WeekendsOnly().advance(tradeDate, cashSettlementDays_, Days);

Date protectionStart;
Expand Down
6 changes: 3 additions & 3 deletions ql/instruments/makeswaption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace QuantLib {
Rate strike)
: swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical),
settlementMethod_(Settlement::PhysicalOTC), optionTenor_(optionTenor),
optionConvention_(ModifiedFollowing), fixingDate_(Null<Date>()), strike_(strike),
optionConvention_(ModifiedFollowing), strike_(strike),
underlyingType_(Swap::Payer), nominal_(1.0) {}

MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
Expand All @@ -58,10 +58,10 @@ namespace QuantLib {
// if the evaluation date is not a business day
// then move to the next business day
refDate = fixingCalendar.adjust(refDate);
if (fixingDate_ == Null<Date>())
if (fixingDate_ == Date())
fixingDate_ = fixingCalendar.advance(refDate, optionTenor_,
optionConvention_);
if (exerciseDate_ == Null<Date>()) {
if (exerciseDate_ == Date()) {
exercise_ = ext::shared_ptr<Exercise>(new
EuropeanExercise(fixingDate_));
} else {
Expand Down
2 changes: 1 addition & 1 deletion ql/instruments/simplechooseroption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace QuantLib {
//! Extra %arguments for single chooser option
class SimpleChooserOption::arguments : public OneAssetOption::arguments {
public:
arguments() : choosingDate(Null<Date>()) {}
arguments() = default;
void validate() const override;
Date choosingDate;
};
Expand Down
6 changes: 3 additions & 3 deletions ql/legacy/libormarketmodels/lfmcovarparam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ namespace QuantLib {
Size size() const { return size_; }
Size factors() const { return factors_; }

virtual Matrix diffusion(Time t, const Array& x = Null<Array>()) const = 0;
virtual Matrix covariance(Time t, const Array& x = Null<Array>()) const;
virtual Matrix integratedCovariance(Time t, const Array& x = Null<Array>()) const;
virtual Matrix diffusion(Time t, const Array& x = {}) const = 0;
virtual Matrix covariance(Time t, const Array& x = {}) const;
virtual Matrix integratedCovariance(Time t, const Array& x = {}) const;

protected:
const Size size_;
Expand Down
7 changes: 3 additions & 4 deletions ql/legacy/libormarketmodels/lfmcovarproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ namespace QuantLib {
ext::shared_ptr<LmVolatilityModel> volatilityModel() const;
ext::shared_ptr<LmCorrelationModel> correlationModel() const;

Matrix diffusion(Time t, const Array& x = Null<Array>()) const override;
Matrix covariance(Time t, const Array& x = Null<Array>()) const override;
Matrix diffusion(Time t, const Array& x = {}) const override;
Matrix covariance(Time t, const Array& x = {}) const override;

using LfmCovarianceParameterization::integratedCovariance;
virtual Real integratedCovariance(
Size i, Size j, Time t, const Array& x = Null<Array>()) const;
virtual Real integratedCovariance(Size i, Size j, Time t, const Array& x = {}) const;

protected:
const ext::shared_ptr<LmVolatilityModel> volaModel_;
Expand Down
6 changes: 3 additions & 3 deletions ql/legacy/libormarketmodels/lfmhullwhiteparam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ namespace QuantLib {
const ext::shared_ptr<OptionletVolatilityStructure> & capletVol,
const Matrix& correlation = Matrix(), Size factors = 1);

Matrix diffusion(Time t, const Array& x = Null<Array>()) const override;
Matrix covariance(Time t, const Array& x = Null<Array>()) const override;
Matrix integratedCovariance(Time t, const Array& x = Null<Array>()) const override;
Matrix diffusion(Time t, const Array& x = {}) const override;
Matrix covariance(Time t, const Array& x = {}) const override;
Matrix integratedCovariance(Time t, const Array& x = {}) const override;

protected:
Size nextIndexReset(Time t) const;
Expand Down
6 changes: 3 additions & 3 deletions ql/legacy/libormarketmodels/lmconstwrappercorrmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ namespace QuantLib {

Size factors() const override { return corrModel_->factors(); }

Matrix correlation(Time t, const Array& x = Null<Array>()) const override {
Matrix correlation(Time t, const Array& x = {}) const override {
return corrModel_->correlation(t, x);
}
Matrix pseudoSqrt(Time t, const Array& x = Null<Array>()) const override {
Matrix pseudoSqrt(Time t, const Array& x = {}) const override {
return corrModel_->pseudoSqrt(t, x);
}
Real correlation(Size i, Size j, Time t, const Array& x = Null<Array>()) const override {
Real correlation(Size i, Size j, Time t, const Array& x = {}) const override {
return corrModel_->correlation(i, j, t, x);
}
bool isTimeIndependent() const override { return corrModel_->isTimeIndependent(); }
Expand Down
7 changes: 3 additions & 4 deletions ql/legacy/libormarketmodels/lmconstwrappervolmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ namespace QuantLib {
volaModel_(volaModel) {
}

Array volatility(Time t, const Array& x = Null<Array>()) const override {
Array volatility(Time t, const Array& x = {}) const override {
return volaModel_->volatility(t, x);
}
Volatility volatility(
Size i, Time t, const Array& x = Null<Array>()) {
Size i, Time t, const Array& x = {}) {
return volaModel_->volatility(i, t, x);
}
Real
integratedVariance(Size i, Size j, Time u, const Array& x = Null<Array>()) const override {
Real integratedVariance(Size i, Size j, Time u, const Array& x = {}) const override {
return volaModel_->integratedVariance(i, j, u, x);
}

Expand Down
6 changes: 3 additions & 3 deletions ql/legacy/libormarketmodels/lmcorrmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ namespace QuantLib {
std::vector<Parameter>& params();
void setParams(const std::vector<Parameter> & arguments);

virtual Matrix correlation(Time t, const Array& x = Null<Array>()) const = 0;
virtual Matrix pseudoSqrt(Time t, const Array& x = Null<Array>()) const;
virtual Real correlation(Size i, Size j, Time t, const Array& x = Null<Array>()) const;
virtual Matrix correlation(Time t, const Array& x = {}) const = 0;
virtual Matrix pseudoSqrt(Time t, const Array& x = {}) const;
virtual Real correlation(Size i, Size j, Time t, const Array& x = {}) const;
virtual bool isTimeIndependent() const;

protected:
Expand Down
4 changes: 2 additions & 2 deletions ql/legacy/libormarketmodels/lmexpcorrmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace QuantLib {
public:
LmExponentialCorrelationModel(Size size, Real rho);

Matrix correlation(Time t, const Array& x = Null<Array>()) const override;
Matrix pseudoSqrt(Time t, const Array& x = Null<Array>()) const override;
Matrix correlation(Time t, const Array& x = {}) const override;
Matrix pseudoSqrt(Time t, const Array& x = {}) const override;
Real correlation(Size i, Size j, Time t, const Array& x) const override;
bool isTimeIndependent() const override;

Expand Down
7 changes: 3 additions & 4 deletions ql/legacy/libormarketmodels/lmextlinexpvolmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ namespace QuantLib {
LmExtLinearExponentialVolModel(const std::vector<Time>& fixingTimes,
Real a, Real b, Real c, Real d);

Array volatility(Time t, const Array& x = Null<Array>()) const override;
Volatility volatility(Size i, Time t, const Array& x = Null<Array>()) const override;
Array volatility(Time t, const Array& x = {}) const override;
Volatility volatility(Size i, Time t, const Array& x = {}) const override;

Real
integratedVariance(Size i, Size j, Time u, const Array& x = Null<Array>()) const override;
Real integratedVariance(Size i, Size j, Time u, const Array& x = {}) const override;
};

}
Expand Down
2 changes: 1 addition & 1 deletion ql/legacy/libormarketmodels/lmfixedvolmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace QuantLib {
public:
LmFixedVolatilityModel(Array volatilities, const std::vector<Time>& startTimes);

Array volatility(Time t, const Array& x = Null<Array>()) const override;
Array volatility(Time t, const Array& x = {}) const override;
Volatility volatility(Size i, Time t, const Array& x) const override;

private:
Expand Down
4 changes: 2 additions & 2 deletions ql/legacy/libormarketmodels/lmlinexpcorrmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace QuantLib {
LmLinearExponentialCorrelationModel(Size size, Real rho, Real beta,
Size factors = Null<Size>());

Matrix correlation(Time t, const Array& x = Null<Array>()) const override;
Matrix pseudoSqrt(Time t, const Array& x = Null<Array>()) const override;
Matrix correlation(Time t, const Array& x = {}) const override;
Matrix pseudoSqrt(Time t, const Array& x = {}) const override;
Real correlation(Size i, Size j, Time t, const Array& x) const override;

Size factors() const override;
Expand Down
Loading

0 comments on commit 8bd5703

Please sign in to comment.