Skip to content

Commit

Permalink
Avoid syntax deprecated in C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Nov 6, 2021
1 parent 57beb0b commit 89ead9e
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 75 deletions.
2 changes: 1 addition & 1 deletion ql/cashflows/cashflows.cpp
Expand Up @@ -710,7 +710,7 @@ namespace QuantLib {
QL_REQUIRE(y.compounding() == Compounded,
"compounded rate required");

return (1.0+y.rate()/y.frequency()) *
return (1.0+y.rate()/Integer(y.frequency())) *
modifiedDuration(leg, y,
includeSettlementDateFlows,
settlementDate, npvDate);
Expand Down
17 changes: 9 additions & 8 deletions ql/cashflows/conundrumpricer.cpp
Expand Up @@ -340,7 +340,7 @@ namespace QuantLib {

// v. HAGAN, Conundrums..., formule 2.17a, 2.18a
return coupon_->accrualPeriod() * (discount_/annuity_) *
((1 + dFdK) * swaptionPrice + optionType*integralValue);
((1 + dFdK) * swaptionPrice + Integer(optionType) * integralValue);
}

Real NumericHaganPricer::swapletPrice() const {
Expand Down Expand Up @@ -475,13 +475,14 @@ namespace QuantLib {
const Real dminus12 = (lnRoverK-.5*variance)/sqrtSigma2T;

CumulativeNormalDistribution cumulativeOfNormal;
const Real N32 = cumulativeOfNormal(optionType*d32);
const Real N12 = cumulativeOfNormal(optionType*d12);
const Real Nminus12 = cumulativeOfNormal(optionType*dminus12);

price += optionType * firstDerivativeOfGAtForwardValue * annuity_ *
swapRateValue_ * (swapRateValue_ * std::exp(variance) * N32-
(swapRateValue_+strike) * N12 + strike * Nminus12);
Integer sign = Integer(optionType);
const Real N32 = cumulativeOfNormal(sign * d32);
const Real N12 = cumulativeOfNormal(sign * d12);
const Real Nminus12 = cumulativeOfNormal(sign * dminus12);

price += sign * firstDerivativeOfGAtForwardValue * annuity_ *
swapRateValue_ * (swapRateValue_ * std::exp(variance) * N32 -
(swapRateValue_ + strike) * N12 + strike * Nminus12);
price *= coupon_->accrualPeriod();
return price;
}
Expand Down
2 changes: 1 addition & 1 deletion ql/experimental/credit/blackcdsoptionengine.cpp
Expand Up @@ -71,7 +71,7 @@ namespace QuantLib {
// if a non knock-out payer option, add front end protection value
if (arguments_.side == Protection::Buyer && !arguments_.knocksOut) {
Real frontEndProtection =
callPut * arguments_.swap->notional()
Integer(callPut) * arguments_.swap->notional()
* (1.-recoveryRate_)
* probability_->defaultProbability(exerciseDate)
* termStructure_->discount(exerciseDate);
Expand Down
3 changes: 2 additions & 1 deletion ql/experimental/swaptions/haganirregularswaptionengine.cpp
Expand Up @@ -178,7 +178,8 @@ namespace QuantLib {

Real defect = -targetNPV_;

for(Size i=0; i< weights.size();++i) defect -= swap_->type()*lambda*weights[i]*annuities_[i];
for (Size i=0; i< weights.size();++i)
defect -= Integer(swap_->type()) * lambda*weights[i] * annuities_[i];

return defect;
}
Expand Down
58 changes: 29 additions & 29 deletions ql/math/interpolations/multicubicspline.hpp
Expand Up @@ -46,15 +46,15 @@ namespace QuantLib {
struct EmptyDim {}; // size_t termination marker

template<class X> struct DataTable {
DataTable<X>(const std::vector<Size>::const_iterator &i) {
DataTable(const std::vector<Size>::const_iterator &i) {
std::vector<X> temp(*i, X(i + 1));
data_table_.swap(temp);
}
DataTable<X>(const SplineGrid::const_iterator &i) {
DataTable(const SplineGrid::const_iterator &i) {
std::vector<X> temp(i->size(), X(i + 1));
data_table_.swap(temp);
}
template<class U> DataTable<X>(const std::vector<U> &v) {
template<class U> DataTable(const std::vector<U> &v) {
DataTable temp(v.begin());
data_table_.swap(temp.data_table_);
}
Expand All @@ -67,12 +67,12 @@ namespace QuantLib {
};

template<> struct DataTable<Real> {
DataTable<Real>(Size n) : data_table_(n) {}
DataTable<Real>(const std::vector<Size>::const_iterator& i)
DataTable(Size n) : data_table_(n) {}
DataTable(const std::vector<Size>::const_iterator& i)
: data_table_(*i) {}
DataTable<Real>(const SplineGrid::const_iterator &i)
DataTable(const SplineGrid::const_iterator &i)
: data_table_(i->size()) {}
template<class U> DataTable<Real>(const std::vector<U> &v) {
template<class U> DataTable(const std::vector<U> &v) {
DataTable temp(v.begin());
data_table_.swap(temp.data_table_);
}
Expand All @@ -87,11 +87,11 @@ namespace QuantLib {
typedef DataTable<Real> base_data_table;

template<class X, class Y> struct Data {
Data<X, Y>()
Data()
: first(X()), second(Y()) {}
Data<X, Y>(const SplineGrid::const_iterator &i)
Data(const SplineGrid::const_iterator &i)
: first(*i), second(i + 1) {}
Data<X, Y>(const SplineGrid &v)
Data(const SplineGrid &v)
: first(v[0]), second(v.begin()+1) {}
void swap(Data<X, Y> &d) {
first.swap(d.first);
Expand All @@ -102,13 +102,13 @@ namespace QuantLib {
};

template<> struct Data<std::vector<Real>, EmptyArg> {
Data<std::vector<Real>, EmptyArg>()
Data()
: first(std::vector<Real>()) {}
Data<std::vector<Real>, EmptyArg>(const SplineGrid::const_iterator &i)
Data(const SplineGrid::const_iterator &i)
: first(*i) {}
Data<std::vector<Real>, EmptyArg>(const SplineGrid &v)
Data(const SplineGrid &v)
: first(v[0]) {}
Data<std::vector<Real>, EmptyArg>(std::vector<Real> v) : first(std::move(v)) {}
Data(std::vector<Real> v) : first(std::move(v)) {}
void swap(Data<std::vector<Real>, EmptyArg> &d) {
first.swap(d.first);
}
Expand All @@ -122,15 +122,15 @@ namespace QuantLib {

template<class X, class Y> struct Point {
typedef X data_type;
Point<X, Y>()
Point()
: first(data_type()), second(Y()) {}
Point<X, Y>(const std::vector<Real>::const_iterator &i)
Point(const std::vector<Real>::const_iterator &i)
: first(*i), second(i + 1) {}
Point<X, Y>(const std::vector<Real> &v)
Point(const std::vector<Real> &v)
: first(v[0]), second(v.begin()+1) {}
Point<X, Y>(const SplineGrid::const_iterator &i)
Point(const SplineGrid::const_iterator &i)
: first(i->size()), second(i + 1) {}
Point<X, Y>(const SplineGrid &grid)
Point(const SplineGrid &grid)
: first(grid[0].size()), second(grid.begin()+1) {}
operator data_type() const {
return first;
Expand All @@ -143,11 +143,11 @@ namespace QuantLib {

template<> struct Point<Real, EmptyArg> {
typedef Real data_type;
Point<Real, EmptyArg>(data_type s)
Point(data_type s)
: first(s) {}
Point<Real, EmptyArg>(const std::vector<Real>::const_iterator &i)
Point(const std::vector<Real>::const_iterator &i)
: first(*i) {}
Point<Real, EmptyArg>(const std::vector<Real> &v)
Point(const std::vector<Real> &v)
: first(v[0]) {}
operator data_type() const {return first;}
data_type operator[](Size n) const {
Expand All @@ -166,9 +166,9 @@ namespace QuantLib {

template<> struct Point<Real, EmptyRes> {
typedef Real data_type;
Point<Real, EmptyRes>()
Point()
: first(data_type()) {}
Point<Real, EmptyRes>(data_type s)
Point(data_type s)
: first(s) {}
operator data_type() const {return first;}
const data_type &operator[](Size n) const {
Expand All @@ -187,9 +187,9 @@ namespace QuantLib {

template<> struct Point<Size, EmptyDim> {
typedef Size data_type;
Point<Size, EmptyDim>()
Point()
: first(data_type()) {}
Point<Size, EmptyDim>(data_type s)
Point(data_type s)
: first(s) {}
operator data_type() const {return first;}
data_type operator[](Size n) const {
Expand All @@ -208,10 +208,10 @@ namespace QuantLib {

template<> struct Point<base_data_table, EmptyRes> {
typedef base_data_table data_type;
Point<base_data_table, EmptyRes>(data_type s) : first(std::move(s)) {}
Point<base_data_table, EmptyRes>(const SplineGrid::const_iterator &i)
Point(data_type s) : first(std::move(s)) {}
Point(const SplineGrid::const_iterator &i)
: first(i->size()) {}
Point<base_data_table, EmptyRes>(const SplineGrid &grid)
Point(const SplineGrid &grid)
: first(grid[0].size()) {}
Real operator[](Size n) const {return first[n];}
Real& operator[](Size n) {return first[n];}
Expand Down
71 changes: 42 additions & 29 deletions ql/pricingengines/blackformula.cpp
Expand Up @@ -76,8 +76,10 @@ namespace QuantLib {
QL_REQUIRE(discount>0.0,
"discount (" << discount << ") must be positive");

if (stdDev==0.0)
return std::max((forward-strike)*optionType, Real(0.0))*discount;
Integer sign = Integer(optionType);

if (stdDev == 0.0)
return std::max((forward-strike) * sign, Real(0.0)) * discount;

forward = forward + displacement;
strike = strike + displacement;
Expand All @@ -90,9 +92,9 @@ namespace QuantLib {
Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev;
Real d2 = d1 - stdDev;
CumulativeNormalDistribution phi;
Real nd1 = phi(optionType*d1);
Real nd2 = phi(optionType*d2);
Real result = discount * optionType * (forward*nd1 - strike*nd2);
Real nd1 = phi(sign * d1);
Real nd2 = phi(sign * d2);
Real result = discount * sign * (forward*nd1 - strike*nd2);
QL_ENSURE(result>=0.0,
"negative value (" << result << ") for " <<
stdDev << " stdDev, " <<
Expand Down Expand Up @@ -123,19 +125,21 @@ namespace QuantLib {
"stdDev (" << stdDev << ") must be non-negative");
QL_REQUIRE(discount>0.0,
"discount (" << discount << ") must be positive");

if (stdDev==0.0)
return optionType * std::max(1.0 * boost::math::sign((forward - strike) * optionType), 0.0) * discount;

Integer sign = Integer(optionType);

if (stdDev == 0.0)
return sign * std::max(1.0 * boost::math::sign((forward - strike) * sign), 0.0) * discount;

forward = forward + displacement;
strike = strike + displacement;

if (strike==0.0)
return (optionType==Option::Call ? discount : 0.0);
if (strike == 0.0)
return (optionType == Option::Call ? discount : 0.0);

Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev;
CumulativeNormalDistribution phi;
return optionType * phi(optionType * d1) * discount;
return sign * phi(sign * d1) * discount;
}

Real blackFormulaForwardDerivative(const ext::shared_ptr<PlainVanillaPayoff>& payoff,
Expand Down Expand Up @@ -169,7 +173,7 @@ namespace QuantLib {
stdDev = blackPrice/discount*std::sqrt(2.0 * M_PI)/forward;
else {
// Corrado and Miller extended moneyness approximation
Real moneynessDelta = optionType*(forward-strike);
Real moneynessDelta = Integer(optionType) * (forward-strike);
Real moneynessDelta_2 = moneynessDelta/2.0;
Real temp = blackPrice/discount - moneynessDelta_2;
Real moneynessDelta_PI = moneynessDelta*moneynessDelta/M_PI;
Expand Down Expand Up @@ -337,16 +341,18 @@ namespace QuantLib {
Real forward,
Real undiscountedBlackPrice,
Real displacement = 0.0)
: halfOptionType_(0.5*optionType), signedStrike_(optionType*(strike+displacement)),
signedForward_(optionType*(forward+displacement)),
: halfOptionType_(0.5 * Integer(optionType)),
signedStrike_(Integer(optionType) * (strike+displacement)),
signedForward_(Integer(optionType) * (forward+displacement)),
undiscountedBlackPrice_(undiscountedBlackPrice)
{
checkParameters(strike, forward, displacement);
QL_REQUIRE(undiscountedBlackPrice>=0.0,
"undiscounted Black price (" <<
undiscountedBlackPrice << ") must be non-negative");
signedMoneyness_ = optionType*std::log((forward+displacement)/(strike+displacement));
signedMoneyness_ = Integer(optionType) * std::log((forward+displacement)/(strike+displacement));
}

Real operator()(Real stdDev) const {
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(stdDev>=0.0,
Expand Down Expand Up @@ -398,7 +404,7 @@ namespace QuantLib {
QL_REQUIRE(blackPrice>=0.0,
"option price (" << blackPrice << ") must be non-negative");
// check the price of the "other" option implied by put-call paity
Real otherOptionPrice = blackPrice - optionType*(forward-strike)*discount;
Real otherOptionPrice = blackPrice - Integer(optionType) * (forward-strike)*discount;
QL_REQUIRE(otherOptionPrice>=0.0,
"negative " << Option::Type(-1*optionType) <<
" price (" << otherOptionPrice <<
Expand Down Expand Up @@ -567,16 +573,19 @@ namespace QuantLib {
Real stdDev,
Real displacement) {
checkParameters(strike, forward, displacement);

Integer sign = Integer(optionType);

if (stdDev==0.0)
return (forward*optionType > strike*optionType ? 1.0 : 0.0);
return (forward * sign > strike * sign ? 1.0 : 0.0);

forward = forward + displacement;
strike = strike + displacement;
if (strike==0.0)
return (optionType==Option::Call ? 1.0 : 0.0);
return (optionType == Option::Call ? 1.0 : 0.0);
Real d2 = std::log(forward/strike)/stdDev - 0.5*stdDev;
CumulativeNormalDistribution phi;
return phi(optionType*d2);
return phi(sign * d2);
}

Real blackFormulaCashItmProbability(
Expand All @@ -595,16 +604,19 @@ namespace QuantLib {
Real stdDev,
Real displacement) {
checkParameters(strike, forward, displacement);

Integer sign = Integer(optionType);

if (stdDev==0.0)
return (forward*optionType < strike*optionType ? 1.0 : 0.0);
return (forward * sign < strike * sign ? 1.0 : 0.0);

forward = forward + displacement;
strike = strike + displacement;
if (strike==0.0)
return (optionType==Option::Call ? 1.0 : 0.0);
if (strike == 0.0)
return (optionType == Option::Call ? 1.0 : 0.0);
Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev;
CumulativeNormalDistribution phi;
return phi(optionType*d1);
return phi(sign * d1);
}

Real blackFormulaAssetItmProbability(
Expand Down Expand Up @@ -707,7 +719,7 @@ namespace QuantLib {
"stdDev (" << stdDev << ") must be non-negative");
QL_REQUIRE(discount>0.0,
"discount (" << discount << ") must be positive");
Real d = (forward-strike)*optionType, h = d/stdDev;
Real d = (forward-strike) * Integer(optionType), h = d / stdDev;
if (stdDev==0.0)
return discount*std::max(d, 0.0);
CumulativeNormalDistribution phi;
Expand Down Expand Up @@ -737,11 +749,12 @@ namespace QuantLib {
"stdDev (" << stdDev << ") must be non-negative");
QL_REQUIRE(discount>0.0,
"discount (" << discount << ") must be positive");
if (stdDev==0.0)
return optionType * std::max(1.0 * boost::math::sign((forward - strike) * optionType), 0.0) * discount;
Real d = (forward-strike)*optionType, h = d/stdDev;
Integer sign = Integer(optionType);
if (stdDev == 0.0)
return sign * std::max(1.0 * boost::math::sign((forward - strike) * sign), 0.0) * discount;
Real d = (forward - strike) * sign, h = d / stdDev;
CumulativeNormalDistribution phi;
return optionType * phi(h) * discount;
return sign * phi(h) * discount;
}

Real bachelierBlackFormulaForwardDerivative(
Expand Down Expand Up @@ -863,7 +876,7 @@ namespace QuantLib {
Real stdDev) {
QL_REQUIRE(stdDev>=0.0,
"stdDev (" << stdDev << ") must be non-negative");
Real d = (forward-strike)*optionType, h = d/stdDev;
Real d = (forward - strike) * Integer(optionType), h = d / stdDev;
if (stdDev==0.0)
return std::max(d, 0.0);
CumulativeNormalDistribution phi;
Expand Down
2 changes: 1 addition & 1 deletion ql/pricingengines/capfloor/bacheliercapfloorengine.cpp
Expand Up @@ -115,7 +115,7 @@ namespace QuantLib {
strike));
floorletVega = bachelierBlackFormulaStdDevDerivative(strike,
forward, stdDevs[i], discountedAccrual) * sqrtTime;
floorletDelta = Option::Put * bachelierBlackFormulaAssetItmProbability(
floorletDelta = Integer(Option::Put) * bachelierBlackFormulaAssetItmProbability(
Option::Put, strike, forward,
stdDevs[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion ql/pricingengines/capfloor/blackcapfloorengine.cpp
Expand Up @@ -132,7 +132,7 @@ namespace QuantLib {
floorletVega = blackFormulaStdDevDerivative(strike,
forward, stdDevs[i], discountedAccrual, displacement_)
* sqrtTime;
floorletDelta = Option::Put * blackFormulaAssetItmProbability(
floorletDelta = Integer(Option::Put) * blackFormulaAssetItmProbability(
Option::Put, strike, forward,
stdDevs[i], displacement_);
}
Expand Down

0 comments on commit 89ead9e

Please sign in to comment.