Skip to content

Commit

Permalink
Even more lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Feb 16, 2021
1 parent ccc4591 commit c1e5065
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 31 deletions.
7 changes: 2 additions & 5 deletions ql/cashflows/conundrumpricer.cpp
Expand Up @@ -257,10 +257,7 @@ namespace QuantLib {

}

Real NumericHaganPricer::integrate(Real a,
Real b, const ConundrumIntegrand& integrand) const {

using namespace ext::placeholders;
Real NumericHaganPricer::integrate(Real a, Real b, const ConundrumIntegrand& integrand) const {

Real result =.0;
//double abserr =.0;
Expand Down Expand Up @@ -292,7 +289,7 @@ namespace QuantLib {
Size k = 3;
ext::function<Real (Real)> temp = ext::cref(integrand);
VariableChange variableChange(temp, a, upperBoundary, k);
f = ext::bind(&VariableChange::value, &variableChange, _1);
f = [&](Real _x) { return variableChange.value(_x); };
result = gaussKronrodNonAdaptive(f, .0, 1.0);
} else {
f = ext::cref(integrand);
Expand Down
12 changes: 4 additions & 8 deletions ql/experimental/math/tcopulapolicy.cpp
Expand Up @@ -18,7 +18,6 @@
*/

#include <ql/experimental/math/tcopulapolicy.hpp>
#include <ql/functional.hpp>
#include <numeric>
#include <algorithm>

Expand Down Expand Up @@ -73,17 +72,14 @@ namespace QuantLib {
"Incompatible sample and latent model sizes");
#endif

using namespace ext::placeholders;

std::vector<Real> result(probs.size());
Size indexSystemic = 0;
std::transform(probs.begin(), probs.begin() + varianceFactors_.size()-1,
result.begin(),
ext::bind(&TCopulaPolicy::inverseCumulativeDensity,
this, _1, indexSystemic++));
result.begin(),
[&](Probability p) { return inverseCumulativeDensity(p, indexSystemic++); });
std::transform(probs.begin() + varianceFactors_.size()-1, probs.end(),
result.begin()+ varianceFactors_.size()-1,
ext::bind(&TCopulaPolicy::inverseCumulativeZ, this, _1));
result.begin()+ varianceFactors_.size()-1,
[&](Probability p) { return inverseCumulativeZ(p); });
return result;
}

Expand Down
9 changes: 3 additions & 6 deletions ql/math/integrals/twodimensionalintegral.hpp
Expand Up @@ -46,17 +46,14 @@ namespace QuantLib {
Real operator()(const ext::function<Real (Real, Real)>& f,
const std::pair<Real, Real>& a,
const std::pair<Real, Real>& b) const {
using namespace ext::placeholders;
return (*integratorX_)(
ext::bind(&TwoDimensionalIntegral::g, this, f, _1,
a.second, b.second), a.first, b.first);
return (*integratorX_)([&](Real x) { return g(f, x, a.second, b.second); },
a.first, b.first);
}

private:
Real g(const ext::function<Real (Real, Real)>& f,
Real x, Real a, Real b) const {
using namespace ext::placeholders;
return (*integratorY_)(ext::bind(f, x, _1), a, b);
return (*integratorY_)([&](Real y) { return f(x, y); }, a, b);
}

const ext::shared_ptr<Integrator> integratorX_, integratorY_;
Expand Down
3 changes: 1 addition & 2 deletions ql/math/optimization/levenbergmarquardt.cpp
Expand Up @@ -86,8 +86,7 @@ namespace QuantLib {
ext::bind(&LevenbergMarquardt::fcn, this, _1, _2, _3, _4, _5);
MINPACK::LmdifCostFunction lmdifJacFunction =
useCostFunctionsJacobian_
? ext::bind(&LevenbergMarquardt::jacFcn, this, _1, _2, _3,
_4, _5)
? ext::bind(&LevenbergMarquardt::jacFcn, this, _1, _2, _3, _4, _5)
: MINPACK::LmdifCostFunction();
MINPACK::lmdif(m, n, xx.get(), fvec.get(),
endCriteria.functionEpsilon(),
Expand Down
5 changes: 2 additions & 3 deletions ql/pricingengines/basket/mcamericanbasketengine.cpp
Expand Up @@ -18,7 +18,6 @@
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

#include <ql/functional.hpp>
#include <ql/math/functional.hpp>
#include <ql/methods/montecarlo/lsmbasissystem.hpp>
#include <ql/pricingengines/basket/mcamericanbasketengine.hpp>
Expand All @@ -38,7 +37,7 @@ namespace QuantLib {
|| polynomType == LsmBasisSystem::Hyperbolic
|| polynomType == LsmBasisSystem::Chebyshev2nd,
"insufficient polynom type");
using namespace ext::placeholders;

const ext::shared_ptr<BasketPayoff> basketPayoff
= ext::dynamic_pointer_cast<BasketPayoff>(payoff_);
QL_REQUIRE(basketPayoff, "payoff not a basket payoff");
Expand All @@ -50,7 +49,7 @@ namespace QuantLib {
scalingValue_/=strikePayoff->strike();
}

v_.emplace_back(ext::bind(&AmericanBasketPathPricer::payoff, this, _1));
v_.emplace_back([&](const Array& state) { return this->payoff(state); });
}

Array AmericanBasketPathPricer::state(const MultiPath& path,
Expand Down
Expand Up @@ -89,7 +89,7 @@ namespace QuantLib {

std::transform(arguments_.begin(), arguments_.end(),
localVolMatrix->begin(),
ext::bind(&Parameter::operator(), _1, 0.0));
[](const Parameter& p) { return p(0.0); });

localVol_ = ext::make_shared<FixedLocalVolSurface>(
referenceDate_,
Expand Down
Expand Up @@ -22,7 +22,6 @@
\brief Black volatility surface back by Heston model
*/

#include <ql/functional.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/pricingengines/blackformula.hpp>
Expand Down Expand Up @@ -76,7 +75,6 @@ namespace QuantLib {
}

Volatility HestonBlackVolSurface::blackVolImpl(Time t, Real strike) const {
using namespace ext::placeholders;
const ext::shared_ptr<HestonProcess> process = hestonModel_->process();

const DiscountFactor df = process->riskFreeRate()->discount(t, true);
Expand Down Expand Up @@ -117,9 +115,8 @@ namespace QuantLib {
const Volatility guess = std::sqrt(theta);
const Real accuracy = std::numeric_limits<Real>::epsilon();

const ext::function<Real(Real)> f = ext::bind(
&blackValue, payoff.optionType(), strike, fwd, t, _1, df, npv);

return solver.solve(f, accuracy, guess, 0.01);
return solver.solve([&](Volatility _v) { return blackValue(payoff.optionType(), strike, fwd,
t, _v, df, npv); },
accuracy, guess, 0.01);
}
}

0 comments on commit c1e5065

Please sign in to comment.