Skip to content

Commit

Permalink
More lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Feb 16, 2021
1 parent e88cde2 commit ccc4591
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 80 deletions.
17 changes: 6 additions & 11 deletions test-suite/basketoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include <ql/termstructures/volatility/equityfx/hestonblackvolsurface.hpp>
#include <ql/pricingengines/basket/fd2dblackscholesvanillaengine.hpp>
#include <ql/utilities/dataformatters.hpp>
#include <ql/functional.hpp>
#include <boost/preprocessor/iteration/local.hpp>

using namespace QuantLib;
using namespace boost::unit_test_framework;
Expand Down Expand Up @@ -1066,15 +1064,12 @@ test_suite* BasketOptionTest::suite(SpeedLevel speed) {
suite->add(QUANTLIB_TEST_CASE(&BasketOptionTest::test2DPDEGreeks));

if (speed <= Fast) {
#define N_TEST_CASES 5
#define BOOST_PP_LOCAL_MACRO(n) \
suite->add(QUANTLIB_TEST_CASE( \
ext::bind(&BasketOptionTest::testOneDAmericanValues, \
(n *LENGTH(oneDataValues))/N_TEST_CASES, \
((n+1)*LENGTH(oneDataValues))/N_TEST_CASES)));

#define BOOST_PP_LOCAL_LIMITS (0, N_TEST_CASES-1)
#include BOOST_PP_LOCAL_ITERATE()
// unrolled to get different test names
suite->add(QUANTLIB_TEST_CASE([=](){ BasketOptionTest::testOneDAmericanValues( 0, 5); }));
suite->add(QUANTLIB_TEST_CASE([=](){ BasketOptionTest::testOneDAmericanValues( 5, 11); }));
suite->add(QUANTLIB_TEST_CASE([=](){ BasketOptionTest::testOneDAmericanValues(11, 17); }));
suite->add(QUANTLIB_TEST_CASE([=](){ BasketOptionTest::testOneDAmericanValues(17, 23); }));
suite->add(QUANTLIB_TEST_CASE([=](){ BasketOptionTest::testOneDAmericanValues(23, 29); }));
}

if (speed == Slow) {
Expand Down
16 changes: 8 additions & 8 deletions test-suite/cdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/functional.hpp>
#include <boost/preprocessor/iteration/local.hpp>
#include <iomanip>
#include <iostream>

Expand Down Expand Up @@ -369,13 +367,15 @@ void CdoTest::testHW(unsigned dataSet) {

test_suite* CdoTest::suite(SpeedLevel speed) {
auto* suite = BOOST_TEST_SUITE("CDO tests");
#ifndef QL_PATCH_SOLARIS
if (speed == Slow) {
#define BOOST_PP_LOCAL_MACRO(n) \
suite->add(QUANTLIB_TEST_CASE(ext::bind(&CdoTest::testHW, n)));

#define BOOST_PP_LOCAL_LIMITS (0, 4)
#include BOOST_PP_LOCAL_ITERATE()
#ifndef QL_PATCH_SOLARIS
if (speed == Slow) {
// unrolled to get different test names
suite->add(QUANTLIB_TEST_CASE([=](){ CdoTest::testHW(0); }));
suite->add(QUANTLIB_TEST_CASE([=](){ CdoTest::testHW(1); }));
suite->add(QUANTLIB_TEST_CASE([=](){ CdoTest::testHW(2); }));
suite->add(QUANTLIB_TEST_CASE([=](){ CdoTest::testHW(3); }));
suite->add(QUANTLIB_TEST_CASE([=](){ CdoTest::testHW(4); }));
}
#endif
return suite;
Expand Down
20 changes: 8 additions & 12 deletions test-suite/fdmlinearop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1284,19 +1284,17 @@ void FdmLinearOpTest::testBiCGstab() {
BOOST_TEST_MESSAGE(
"Testing bi-conjugated gradient stabilized algorithm...");

using namespace ext::placeholders;

const Size n=41, m=21;
const Real theta = 1.0;
const boost::numeric::ublas::compressed_matrix<Real> a
= createTestMatrix(n, m, theta);

const ext::function<Disposable<Array>(const Array&)> matmult(
ext::bind(&axpy, a, _1));
const ext::function<Disposable<Array>(const Array&)> matmult
= [&](const Array& _x) { return axpy(a, _x); };

SparseILUPreconditioner ilu(a, 4);
ext::function<Disposable<Array>(const Array&)> precond(
ext::bind(&SparseILUPreconditioner::apply, &ilu, _1));
ext::function<Disposable<Array>(const Array&)> precond
= [&](const Array& _x) { return ilu.apply(_x); };

Array b(n*m);
MersenneTwisterUniformRng rng(1234);
Expand Down Expand Up @@ -1324,19 +1322,17 @@ void FdmLinearOpTest::testGMRES() {
#if !defined(QL_NO_UBLAS_SUPPORT)
BOOST_TEST_MESSAGE("Testing GMRES algorithm...");

using namespace ext::placeholders;

const Size n=41, m=21;
const Real theta = 1.0;
const boost::numeric::ublas::compressed_matrix<Real> a
= createTestMatrix(n, m, theta);

const ext::function<Disposable<Array>(const Array&)> matmult(
ext::bind(&axpy, a, _1));
const ext::function<Disposable<Array>(const Array&)> matmult
= [&](const Array& _x) { return axpy(a, _x); };

SparseILUPreconditioner ilu(a, 4);
ext::function<Disposable<Array>(const Array&)> precond(
ext::bind(&SparseILUPreconditioner::apply, &ilu, _1));
ext::function<Disposable<Array>(const Array&)> precond
= [&](const Array& _x) { return ilu.apply(_x); };

Array b(n*m);
MersenneTwisterUniformRng rng(1234);
Expand Down
6 changes: 1 addition & 5 deletions test-suite/hybridhestonhullwhiteprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#include <ql/pricingengines/vanilla/analytichestonhullwhiteengine.hpp>
#include <ql/pricingengines/vanilla/fdhestonvanillaengine.hpp>
#include <ql/pricingengines/vanilla/fdhestonhullwhitevanillaengine.hpp>
#include <ql/functional.hpp>
#include <cmath>

using namespace QuantLib;
Expand Down Expand Up @@ -628,8 +627,6 @@ void HybridHestonHullWhiteProcessTest::testAnalyticHestonHullWhitePricing() {
void HybridHestonHullWhiteProcessTest::testCallableEquityPricing() {
BOOST_TEST_MESSAGE("Testing the pricing of a callable equity product...");

using namespace ext::placeholders;

SavedSettings backup;

/*
Expand Down Expand Up @@ -671,8 +668,7 @@ void HybridHestonHullWhiteProcessTest::testCallableEquityPricing() {

std::vector<Time> times(maturity+1);
std::transform(schedule.begin(), schedule.end(), times.begin(),
ext::bind(&Actual365Fixed::yearFraction,
dc, today, _1, Date(), Date()));
[&](const Date& d) { return dc.yearFraction(today, d); });

for (Size i=0; i<=maturity; ++i)
times[i] = static_cast<Time>(i);
Expand Down
39 changes: 20 additions & 19 deletions test-suite/marketmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ FOR A PARTICULAR PURPOSE. See the license for more details.
#include <ql/models/marketmodels/products/multistep/multisteppathwisewrapper.hpp>

#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/preprocessor/iteration/local.hpp>
#include <ql/functional.hpp>
#include <sstream>

using namespace QuantLib;
Expand Down Expand Up @@ -4749,26 +4747,29 @@ test_suite* MarketModelTest::suite(SpeedLevel speed) {

setup();

MarketModelType marketModels[] = {
ExponentialCorrelationFlatVolatility,
ExponentialCorrelationAbcdVolatility
};

Size testedFactors[] = { 4, 8, todaysForwards.size() };
#define BOOST_PP_LOCAL_MACRO(n) \
suite->add(QUANTLIB_TEST_CASE( \
ext::bind(&MarketModelTest::testCallableSwapAnderson, \
marketModels[n/LENGTH(testedFactors)], \
testedFactors[n%LENGTH(testedFactors)])));

#define BOOST_PP_LOCAL_LIMITS (0, 5)
#include BOOST_PP_LOCAL_ITERATE()
#include <utility>
// unrolled to get different test names
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationFlatVolatility, 4);
}));
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationFlatVolatility, 8);
}));
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationFlatVolatility, todaysForwards.size());
}));
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationAbcdVolatility, 4);
}));
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationAbcdVolatility, 8);
}));
suite->add(QUANTLIB_TEST_CASE([=](){
MarketModelTest::testCallableSwapAnderson(ExponentialCorrelationAbcdVolatility, todaysForwards.size());
}));
}

if (speed == Slow) {
suite->add(QUANTLIB_TEST_CASE(
&MarketModelTest::testAllMultiStepProducts));
suite->add(QUANTLIB_TEST_CASE(&MarketModelTest::testAllMultiStepProducts));
suite->add(QUANTLIB_TEST_CASE(&MarketModelTest::testCallableSwapNaif));
suite->add(QUANTLIB_TEST_CASE(&MarketModelTest::testCallableSwapLS));
}
Expand Down
5 changes: 2 additions & 3 deletions test-suite/normalclvmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ namespace normal_clv_model_test {
void NormalCLVModelTest::testMonteCarloBSOptionPricing() {
BOOST_TEST_MESSAGE("Testing Monte Carlo BS option pricing...");

using namespace ext::placeholders;
using namespace normal_clv_model_test;

SavedSettings backup;
Expand Down Expand Up @@ -362,8 +361,8 @@ void NormalCLVModelTest::testMonteCarloBSOptionPricing() {
}

VanillaOption fdmOption(
ext::make_shared<CLVModelPayoff>(
payoff->optionType(), payoff->strike(), ext::bind(g, t, _1)),
ext::make_shared<CLVModelPayoff>(payoff->optionType(), payoff->strike(),
[&](Real _x) { return g(t, _x); }),
exercise);

fdmOption.setPricingEngine(
Expand Down
13 changes: 3 additions & 10 deletions test-suite/nthorderderivativeop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "nthorderderivativeop.hpp"
#include "utilities.hpp"
#include <ql/functional.hpp>
#include <ql/math/comparison.hpp>
#include <ql/math/initializers.hpp>
#include <ql/math/integrals/gausslobattointegral.hpp>
Expand Down Expand Up @@ -418,19 +417,13 @@ namespace {

Disposable<Array> solve_splitting(Size direction, const Array& r, Real dt) const override {

using namespace ext::placeholders;
if (direction == direction_) {
BiCGStabResult result =
QuantLib::BiCGstab(
ext::function<Disposable<Array>(const Array&)>(
ext::bind(
&FdmHeatEquationOp::solve_apply,
this, _1, -dt)),
[&](const Array& a){ return solve_apply(a, -dt); },
std::max(Size(10), r.size()), 1e-14,
ext::function<Disposable<Array>(const Array&)>(
ext::bind(&FdmLinearOpComposite::preconditioner,
this, _1, dt))
).solve(r, r);
[&](const Array& a){ return preconditioner(a, dt); })
.solve(r, r);

return result.x;
}
Expand Down
6 changes: 2 additions & 4 deletions test-suite/paralleltestrunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include <ql/types.hpp>
#include <ql/errors.hpp>
#include <ql/functional.hpp>

#ifdef VERSION
/* This comes from ./configure, and for some reason it interferes with
Expand Down Expand Up @@ -287,7 +286,7 @@ int main( int argc, char* argv[] )
// fork worker processes
boost::thread_group threadGroup;
for (unsigned i=0; i < nProc; ++i) {
threadGroup.create_thread(QuantLib::ext::bind(worker, cmd.str()));
threadGroup.create_thread([&]() { worker(cmd.str()); });
}

struct mutex_remove {
Expand Down Expand Up @@ -416,8 +415,7 @@ int main( int argc, char* argv[] )
#if BOOST_VERSION < 106200
BOOST_TEST_FOREACH( test_observer*, to,
framework::impl::s_frk_state().m_observers )
framework::impl::s_frk_state().m_aux_em.vexecute(
ext::bind( &test_observer::test_start, to, 1 ) );
framework::impl::s_frk_state().m_aux_em.vexecute([&](){ to->test_start(1); });

framework::impl::s_frk_state().execute_test_tree( id.id );

Expand Down
7 changes: 2 additions & 5 deletions test-suite/riskneutraldensitycalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "riskneutraldensitycalculator.hpp"
#include "utilities.hpp"
#include <ql/functional.hpp>
#include <ql/instruments/vanillaoption.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/math/functional.hpp>
Expand Down Expand Up @@ -493,8 +492,7 @@ void RiskNeutralDensityCalculatorTest::testSquareRootProcessRND() {

const Real cdfCalculated = rndCalculator.cdf(v, t);
const Real cdfExpected = GaussLobattoIntegral(10000, 0.01*tol)(
ext::bind(&SquareRootProcessRNDCalculator::pdf,
&rndCalculator, ext::placeholders::_1, t), 0, v);
[&](Real _x) { return rndCalculator.pdf(_x, t); }, 0, v);

if (std::fabs(cdfCalculated - cdfExpected) > tol) {
BOOST_FAIL("failed to calculate cdf"
Expand Down Expand Up @@ -741,8 +739,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, ext::placeholders::_1, t),
std::max(QL_EPSILON, f0-ax), f0+ax) +
[&](Real _x) { return calculator->pdf(_x, t); }, std::max(QL_EPSILON, f0-ax), f0+ax) +
calculator->massAtZero(t);

if (std::fabs(calculated - 1.0) > tol) {
Expand Down
4 changes: 1 addition & 3 deletions test-suite/squarerootclvmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ void SquareRootCLVModelTest::testSquareRootCLVVanillaPricing() {
BOOST_TEST_MESSAGE(
"Testing vanilla option pricing with square-root kernel process...");

using namespace ext::placeholders;
using namespace square_root_clv_model;

SavedSettings backup;
Expand Down Expand Up @@ -249,8 +248,7 @@ void SquareRootCLVModelTest::testSquareRootCLVMappingFunction() {
std::sqrt(sabrVol->blackVariance(m, strike)),
rTS->discount(m)).value();

const CLVModelPayoff clvModelPayoff(
optionType, strike, ext::bind(g, t, ext::placeholders::_1));
const CLVModelPayoff clvModelPayoff(optionType, strike, [&](Real x) { return g(t, x); });

const ext::function<Real(Real)> f = [&](Real xi) {
return clvModelPayoff(xi) * boost::math::pdf(dist, xi);
Expand Down

0 comments on commit ccc4591

Please sign in to comment.