Skip to content

Commit

Permalink
Merge pull request #784.
Browse files Browse the repository at this point in the history
Import global boost placeholders into ext::placeholders namespace.
  • Loading branch information
lballabio committed Mar 17, 2020
2 parents d37b6d8 + b9b94e2 commit c3ad506
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 51 deletions.
10 changes: 4 additions & 6 deletions ql/experimental/credit/defaultprobabilitylatentmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ namespace QuantLib {
Trivial method for testing
*/
Probability probOfDefault(Size iName, const Date& d) const {
using namespace ext::placeholders;
QL_REQUIRE(basket_, "No portfolio basket set.");
const ext::shared_ptr<Pool>& pool = basket_->pool();
// avoid repeating this in the integration:
Expand All @@ -224,7 +223,7 @@ namespace QuantLib {
this,
inverseCumulativeY(pUncond, iName),
iName,
_1)
ext::placeholders::_1)
));
}
/*! Pearsons' default probability correlation.
Expand All @@ -239,15 +238,14 @@ namespace QuantLib {
defaults in the basket portfolio at a given time.
*/
Probability probAtLeastNEvents(Size n, const Date& date) const {
using namespace ext::placeholders;
return integratedExpectedValue(
ext::function<Real (const std::vector<Real>& v1)>(
ext::bind(
&DefaultLatentModel<copulaPolicy>::conditionalProbAtLeastNEvents,
this,
n,
ext::cref(date),
_1)
ext::placeholders::_1)
));
}
};
Expand All @@ -259,7 +257,6 @@ namespace QuantLib {
Real DefaultLatentModel<CP>::defaultCorrelation(const Date& d,
Size iNamei, Size iNamej) const
{
using namespace ext::placeholders;
QL_REQUIRE(basket_, "No portfolio basket set.");

const ext::shared_ptr<Pool>& pool = basket_->pool();
Expand All @@ -280,7 +277,8 @@ namespace QuantLib {
ext::function<Real (const std::vector<Real>& v1)>(
ext::bind(
&DefaultLatentModel<CP>::condProbProduct,
this, invPi, invPj, iNamei, iNamej, _1) ));
this, invPi, invPj, iNamei, iNamej,
ext::placeholders::_1) ));
}else{
E1i1j = pi;
}
Expand Down
14 changes: 8 additions & 6 deletions ql/experimental/math/latentmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,16 @@ namespace QuantLib {
*/
Real integratedExpectedValue(
const ext::function<Real(const std::vector<Real>& v1)>& f) const {
using namespace ext::placeholders;

// function composition: composes the integrand with the density
// through a product.
return
integration()->integrate(
ext::bind(std::multiplies<Real>(),
ext::bind(&copulaPolicyImpl::density, copula_, _1),
ext::bind(ext::cref(f), _1)));
ext::bind(&copulaPolicyImpl::density, copula_,
ext::placeholders::_1),
ext::bind(ext::cref(f),
ext::placeholders::_1)));
}
/*! Integrates an arbitrary vector function over the density domain(i.e.
computes its expected value).
Expand All @@ -607,13 +609,13 @@ namespace QuantLib {
// const ext::function<std::vector<Real>(
const ext::function<Disposable<std::vector<Real> >(
const std::vector<Real>& v1)>& f ) const {
using namespace ext::placeholders;
return
integration()->integrateV(//see note in LMIntegrators base class
ext::bind<Disposable<std::vector<Real> > >(
detail::multiplyV(),
ext::bind(&copulaPolicyImpl::density, copula_, _1),
ext::bind(ext::cref(f), _1)));
ext::bind(&copulaPolicyImpl::density, copula_,
ext::placeholders::_1),
ext::bind(ext::cref(f), ext::placeholders::_1)));
}
protected:
// Integrable models must provide their integrator.
Expand Down
10 changes: 4 additions & 6 deletions ql/experimental/math/multidimintegrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ namespace QuantLib {

template<>
void inline MultidimIntegral::spawnFcts<1>() const {
using namespace ext::placeholders;
integrationLevelEntries_[0] =
ext::bind(&MultidimIntegral::integrate<0>, this, _1, _2, _3);
ext::bind(&MultidimIntegral::integrate<0>, this,
ext::placeholders::_1, ext::placeholders::_2, ext::placeholders::_3);
}

template<int nT>
Expand All @@ -136,11 +136,10 @@ namespace QuantLib {
const std::vector<Real>& a,
const std::vector<Real>& b) const
{
using namespace ext::placeholders;
return
(*integrators_[nT])(
ext::bind(&MultidimIntegral::vectorBinder<nT>, this, f,
_1, ext::cref(a), ext::cref(b)), a[nT], b[nT]);
ext::placeholders::_1, ext::cref(a), ext::cref(b)), a[nT], b[nT]);
}

template<int T_N>
Expand All @@ -156,10 +155,9 @@ namespace QuantLib {

template<Size depth>
void MultidimIntegral::spawnFcts() const {
using namespace ext::placeholders;
integrationLevelEntries_[depth-1] =
ext::bind(&MultidimIntegral::integrate<depth-1>, this,
_1, _2, _3);
ext::placeholders::_1, ext::placeholders::_2, ext::placeholders::_3);
spawnFcts<depth-1>();
}

Expand Down
31 changes: 12 additions & 19 deletions ql/experimental/math/multidimquadrature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ namespace QuantLib {

template <class F> // todo: fix copies.
detail::DispArray operator()(const F& f) const {
using namespace ext::placeholders;
//first one, we do not know the size of the vector returned by f
Integer i = order()-1;
std::vector<Real> term = f(x_[i]);// potential copy! @#$%^!!!
Expand All @@ -74,8 +73,9 @@ namespace QuantLib {
// sum[j] += term[j] * w_[i];
std::transform(term.begin(), term.end(), sum.begin(),
sum.begin(),
ext::bind(std::plus<Real>(), _2,
ext::bind(std::multiplies<Real>(), w_[i], _1)));
ext::bind(std::plus<Real>(), ext::placeholders::_2,
ext::bind(std::multiplies<Real>(), w_[i],
ext::placeholders::_1)));
}
return sum;
}
Expand Down Expand Up @@ -136,15 +136,14 @@ namespace QuantLib {
// class construction time) handles to the integration entry points
template<Size levelSpawn>
void spawnFcts() const {
using namespace ext::placeholders;
integrationEntries_[levelSpawn-1] =
ext::bind(
&GaussianQuadMultidimIntegrator::scalarIntegrator<levelSpawn>,
this, _1, _2);
this, ext::placeholders::_1, ext::placeholders::_2);
integrationEntriesVR_[levelSpawn-1] =
ext::bind(
&GaussianQuadMultidimIntegrator::vectorIntegratorVR<levelSpawn>,
this, _1, _2);
this, ext::placeholders::_1, ext::placeholders::_2);
spawnFcts<levelSpawn-1>();
}
//@}
Expand All @@ -156,13 +155,12 @@ namespace QuantLib {
ext::function<Real (const std::vector<Real>& arg1)> f,
const Real mFctr) const
{
using namespace ext::placeholders;
varBuffer_[intgDepth-1] = mFctr;
return integral_(ext::bind(
&GaussianQuadMultidimIntegrator::scalarIntegrator<intgDepth-1>,
this,
f,
_1)
ext::placeholders::_1)
);
}

Expand All @@ -171,14 +169,13 @@ namespace QuantLib {
const ext::function<detail::DispArray(const std::vector<Real>& arg1)>& f,
const Real mFctr) const
{
using namespace ext::placeholders;
varBuffer_[intgDepth-1] = mFctr;
return
integralV_(ext::bind(
&GaussianQuadMultidimIntegrator::vectorIntegratorVR<intgDepth-1>,
this,
f,
_1)
ext::placeholders::_1)
);
}
private:
Expand Down Expand Up @@ -212,12 +209,11 @@ namespace QuantLib {
inline Real GaussianQuadMultidimIntegrator::operator()(
const ext::function<Real (const std::vector<Real>& v1)>& f) const
{
using namespace ext::placeholders;
return integral_(ext::bind(
// integration entry level is selected now
integrationEntries_[dimension_-1],
ext::cref(f),
_1)
ext::placeholders::_1)
);
}

Expand All @@ -226,7 +222,6 @@ namespace QuantLib {
inline Real GaussianQuadMultidimIntegrator::integrate<Real>(
const ext::function<Real (const std::vector<Real>& v1)>& f) const
{
using namespace ext::placeholders;
// integration variables
// call vector quadrature integration with the function and start
// values, kicks in recursion over the dimensions of the integration
Expand All @@ -235,7 +230,7 @@ namespace QuantLib {
// integration entry level is selected now
integrationEntries_[dimension_-1],
ext::cref(f),
_1)
ext::placeholders::_1)
);
}

Expand All @@ -244,11 +239,10 @@ namespace QuantLib {
inline detail::DispArray GaussianQuadMultidimIntegrator::integrate<detail::DispArray>(
const ext::function<detail::DispArray (const std::vector<Real>& v1)>& f) const
{
using namespace ext::placeholders;
return integralV_(ext::bind(
ext::cref(integrationEntriesVR_[dimension_-1]),
ext::cref(f),
_1)
ext::placeholders::_1)
);
}

Expand Down Expand Up @@ -276,13 +270,12 @@ namespace QuantLib {
//! Terminal level:
template<>
inline void GaussianQuadMultidimIntegrator::spawnFcts<1>() const {
using namespace ext::placeholders;
integrationEntries_[0] =
ext::bind(&GaussianQuadMultidimIntegrator::scalarIntegrator<1>,
this, _1, _2);
this, ext::placeholders::_1, ext::placeholders::_2);
integrationEntriesVR_[0] =
ext::bind(&GaussianQuadMultidimIntegrator::vectorIntegratorVR<1>,
this, _1, _2);
this, ext::placeholders::_1, ext::placeholders::_2);
}

}
Expand Down
12 changes: 11 additions & 1 deletion ql/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ namespace QuantLib {
using boost::bind;
using boost::ref;
using boost::cref;
namespace placeholders {}
namespace placeholders {
using ::_1;
using ::_2;
using ::_3;
using ::_4;
using ::_5;
using ::_6;
using ::_7;
using ::_8;
using ::_9;
}
#endif

}
Expand Down
17 changes: 12 additions & 5 deletions test-suite/linearleastsquaresregression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,16 @@ void LinearLeastSquaresRegressionTest::testRegression() {
}
}

namespace {
Real f(const Array& a, Size i) {
return a[i];
}
namespace linear_least_square_regression_test {

struct get_item {
Size i;
explicit get_item(Size i) : i(i) {}
Real operator()(const Array& a) const {
return a[i];
}
};

}

void LinearLeastSquaresRegressionTest::testMultiDimRegression() {
Expand All @@ -121,6 +127,7 @@ void LinearLeastSquaresRegressionTest::testMultiDimRegression() {
"Testing multi-dimensional linear least-squares regression...");

using namespace ext::placeholders;
using namespace linear_least_square_regression_test;

SavedSettings backup;

Expand All @@ -132,7 +139,7 @@ void LinearLeastSquaresRegressionTest::testMultiDimRegression() {
std::vector<ext::function<Real(Array)> > v;
v.push_back(constant<Array, Real>(1.0));
for (Size i=0; i < dims; ++i) {
v.push_back(ext::bind(f, _1, i));
v.push_back(get_item(i));
}

Array coeff(v.size());
Expand Down
8 changes: 2 additions & 6 deletions test-suite/riskneutraldensitycalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,6 @@ void RiskNeutralDensityCalculatorTest::testLocalVolatilityRND() {
void RiskNeutralDensityCalculatorTest::testSquareRootProcessRND() {
BOOST_TEST_MESSAGE("Testing probability density for a square root process...");

using namespace ext::placeholders;

struct SquareRootProcessParams {
const Real v0, kappa, theta, sigma;
};
Expand All @@ -506,7 +504,7 @@ void RiskNeutralDensityCalculatorTest::testSquareRootProcessRND() {
const Real cdfCalculated = rndCalculator.cdf(v, t);
const Real cdfExpected = GaussLobattoIntegral(10000, 0.01*tol)(
ext::bind(&SquareRootProcessRNDCalculator::pdf,
&rndCalculator, _1, t), 0, v);
&rndCalculator, ext::placeholders::_1, t), 0, v);

if (std::fabs(cdfCalculated - cdfExpected) > tol) {
BOOST_FAIL("failed to calculate cdf"
Expand Down Expand Up @@ -735,8 +733,6 @@ void RiskNeutralDensityCalculatorTest::testMassAtZeroCEVProcessRND() {
BOOST_TEST_MESSAGE("Testing the mass at zero for a "
"constant elasticity of variance (CEV) process...");

using namespace ext::placeholders;

const Real f0 = 100.0;
const Time t = 2.75;

Expand All @@ -759,7 +755,7 @@ void RiskNeutralDensityCalculatorTest::testMassAtZeroCEVProcessRND() {
const Real ax = 15.0*std::sqrt(t)*alpha*std::pow(f0, beta);

const Real calculated = GaussLobattoIntegral(1000, 1e-8)(
ext::bind(&CEVRNDCalculator::pdf, calculator, _1, t),
ext::bind(&CEVRNDCalculator::pdf, calculator, ext::placeholders::_1, t),
std::max(QL_EPSILON, f0-ax), f0+ax) +
calculator->massAtZero(t);

Expand Down
4 changes: 2 additions & 2 deletions test-suite/squarerootclvmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ void SquareRootCLVModelTest::testSquareRootCLVMappingFunction() {
BOOST_TEST_MESSAGE(
"Testing mapping function of the square root kernel process...");

using namespace ext::placeholders;
using namespace square_root_clv_model;

SavedSettings backup;
Expand Down Expand Up @@ -269,7 +268,8 @@ void SquareRootCLVModelTest::testSquareRootCLVMappingFunction() {
rTS->discount(m)).value();

const CLVModelPayoff clvModelPayoff(
optionType, strike, ext::bind(g, t, _1));
optionType, strike,
ext::bind(g, t, ext::placeholders::_1));

const ext::function<Real(Real)> f = integrand(clvModelPayoff, dist);

Expand Down

0 comments on commit c3ad506

Please sign in to comment.