Skip to content

Commit

Permalink
Last lambdas.
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Feb 17, 2021
1 parent 2b6996b commit 2560d5d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 47 deletions.
13 changes: 2 additions & 11 deletions ql/methods/finitedifferences/meshers/exponentialjump1dmesher.cpp
Expand Up @@ -26,7 +26,6 @@
#include <ql/math/integrals/gausslobattointegral.hpp>
#include <ql/math/distributions/gammadistribution.hpp>
#include <ql/methods/finitedifferences/meshers/exponentialjump1dmesher.hpp>
#include <ql/functional.hpp>

namespace QuantLib {
ExponentialJump1dMesher::ExponentialJump1dMesher(
Expand Down Expand Up @@ -75,19 +74,14 @@ namespace QuantLib {
}

Real ExponentialJump1dMesher::jumpSizeDistribution(Real x, Time t) const {
using namespace ext::placeholders;
const Real xmin = std::min(x, 1.0e-100);

// the typedef is needed to disambiguate the overloaded method
typedef Real (ExponentialJump1dMesher::*M)(Real, Time) const;
return GaussLobattoIntegral(1000000, 1e-12)(
ext::bind(M(&ExponentialJump1dMesher::jumpSizeDensity),
this, _1, t),
[&](Real _x){ return jumpSizeDensity(_x, t); },
xmin, std::max(x, xmin));
}

Real ExponentialJump1dMesher::jumpSizeDistribution(Real x) const {
using namespace ext::placeholders;
const Real a = jumpIntensity_/beta_;
const Real xmin = std::min(x, QL_EPSILON);
const Real gammaValue
Expand All @@ -96,11 +90,8 @@ namespace QuantLib {
const Real lowerEps =
(std::pow(xmin, a)/a - std::pow(xmin, a+1)/(a+1))/gammaValue;

// the typedef is needed to disambiguate the overloaded method
typedef Real (ExponentialJump1dMesher::*M)(Real) const;
return lowerEps + GaussLobattoIntegral(10000, 1e-12)(
ext::bind(M(&ExponentialJump1dMesher::jumpSizeDensity),
this, _1),
[&](Real _x){ return jumpSizeDensity(_x); },
xmin/eta_, std::max(x, xmin/eta_));
}
}
7 changes: 1 addition & 6 deletions ql/methods/finitedifferences/utilities/cevrndcalculator.cpp
Expand Up @@ -20,7 +20,6 @@
/*! \file cevrndcalculator.cpp */

#include <ql/errors.hpp>
#include <ql/functional.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
Expand Down Expand Up @@ -102,17 +101,13 @@ namespace QuantLib {
}

Real CEVRNDCalculator::invcdf(Real q, Time t) const {
using namespace ext::placeholders;

if (delta_ < 2.0) {
if (f0_ < QL_EPSILON || q < massAtZero(t))
return 0.0;

const Real x = InverseCumulativeNormal()(1-q);

const ext::function<Real(Real)> cdfApprox
= ext::bind(&CEVRNDCalculator::sankaranApprox,
this, _1, t, x);
auto cdfApprox = [&](Real _c){ return sankaranApprox(_c, t, x); };

const Real y0 = X(f0_)/t;

Expand Down
7 changes: 1 addition & 6 deletions ql/methods/finitedifferences/utilities/gbsmrndcalculator.cpp
Expand Up @@ -22,7 +22,6 @@
Black-Scholes-Merton model with skew dependent volatility
*/

#include <ql/functional.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/solvers1d/brent.hpp>
Expand Down Expand Up @@ -74,8 +73,6 @@ namespace QuantLib {
}

Real GBSMRNDCalculator::invcdf(Real q, Time t) const {
using namespace ext::placeholders;

const Real fwd = process_->x0()
/ process_->riskFreeRate()->discount(t, true)
* process_->dividendYield()->discount(t, true);
Expand All @@ -100,9 +97,7 @@ namespace QuantLib {
<< cdf(lower, t) << ", " << cdf(upper, t) << ")");

return Brent().solve(
compose(subtract<Real>(q),
ext::function<Real(Real)>(
ext::bind(&GBSMRNDCalculator::cdf, this, _1, t))),
[&](Real _k){ return cdf(_k, t) - q; },
1e-10, 0.5*(lower+upper), lower, upper);
}
}
15 changes: 6 additions & 9 deletions ql/methods/finitedifferences/utilities/hestonrndcalculator.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/math/integrals/gausslobattointegral.hpp>
#include <ql/methods/finitedifferences/utilities/bsmrndcalculator.hpp>
Expand Down Expand Up @@ -101,7 +100,7 @@ namespace {
/(1.0-gamma))));
}

const HestonParams& p_;
const HestonParams p_;
const Time t_;
const Real x_, c_inf_;
};
Expand All @@ -127,17 +126,15 @@ namespace {
CpxPv_Helper(getHestonParams(hestonProcess_), x_t(x, t), t),
0.0, 1.0)/M_TWOPI;
}

Real HestonRNDCalculator::cdf(Real x, Time t) const {
using namespace ext::placeholders;
CpxPv_Helper helper(getHestonParams(hestonProcess_), x_t(x, t), t);

return GaussLobattoIntegral(
maxIntegrationIterations_, 0.1*integrationEps_)(
ext::bind(&CpxPv_Helper::p0,
CpxPv_Helper(getHestonParams(hestonProcess_), x_t(x,t),t),_1),
return GaussLobattoIntegral(maxIntegrationIterations_, 0.1*integrationEps_)(
[&](Real p_x){ return helper.p0(p_x); },
0.0, 1.0)/M_TWOPI + 0.5;

}

Real HestonRNDCalculator::invcdf(Real p, Time t) const {
const Real v0 = hestonProcess_->v0();
const Real kappa = hestonProcess_->kappa();
Expand Down
Expand Up @@ -22,7 +22,6 @@
\brief local volatility risk neutral terminal density calculation
*/

#include <ql/functional.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/math/integrals/discreteintegrals.hpp>
#include <ql/math/integrals/gausslobattointegral.hpp>
Expand Down Expand Up @@ -126,8 +125,6 @@ namespace QuantLib {
}

Real LocalVolRNDCalculator::cdf(Real x, Time t) const {
using namespace ext::placeholders;

calculate();

// get the left side of the integral
Expand All @@ -146,14 +143,15 @@ namespace QuantLib {
// left or right hand integral
if (x > 0.5*(xr+xl)) {
while (pdf(xr, t) > 0.01*localVolProbEps_) xr*=1.1;

return 1.0-GaussLobattoIntegral(maxIter_, 0.1*localVolProbEps_)(
ext::bind(&LocalVolRNDCalculator::pdf, this, _1, t), x, xr);
[&](Real _x){ return pdf(_x, t); }, x, xr);
}
else {
while (pdf(xl, t) > 0.01*localVolProbEps_) xl*=0.9;

return GaussLobattoIntegral(maxIter_, 0.1*localVolProbEps_)(
ext::bind(&LocalVolRNDCalculator::pdf, this, _1, t), xl, x);
[&](Real _x){ return pdf(_x, t); }, xl, x);
}
}

Expand Down
Expand Up @@ -21,9 +21,9 @@
#include <ql/math/functional.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/methods/finitedifferences/utilities/riskneutraldensitycalculator.hpp>
#include <ql/functional.hpp>

namespace QuantLib {

RiskNeutralDensityCalculator::InvCDFHelper::InvCDFHelper(
const RiskNeutralDensityCalculator* calculator,
Real guess, Real accuracy, Size maxEvaluations,
Expand All @@ -34,17 +34,11 @@ namespace QuantLib {
maxEvaluations_(maxEvaluations),
stepSize_(stepSize) { }

Real RiskNeutralDensityCalculator::InvCDFHelper::inverseCDF(Real p, Time t)
const {
using namespace ext::placeholders;

const ext::function<Real(Real)> cdf
= ext::bind(&RiskNeutralDensityCalculator::cdf,
calculator_, _1, t);

Real RiskNeutralDensityCalculator::InvCDFHelper::inverseCDF(Real p, Time t) const {
Brent solver;
solver.setMaxEvaluations(maxEvaluations_);
return solver.solve(compose(subtract<Real>(p), cdf),
return solver.solve([&](Real _x){ return calculator_->cdf(_x, t) - p; },
accuracy_, guess_, stepSize_);
}

}

0 comments on commit 2560d5d

Please sign in to comment.