Skip to content

Commit

Permalink
Merge pull request #908.
Browse files Browse the repository at this point in the history
fix to have option of using std::tuple
  • Loading branch information
lballabio committed Oct 13, 2020
2 parents b36a021 + 8a807c5 commit d90d543
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 123 deletions.
2 changes: 1 addition & 1 deletion Examples/GlobalOptimizer/GlobalOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <ql/functional.hpp>


#include <boost/tuple/tuple.hpp>
#include <ql/tuple.hpp>
#include <iostream>
#include <iomanip>

Expand Down
23 changes: 22 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,30 @@ if test "$ql_use_std_function" = "yes" ; then
fi
AC_MSG_RESULT([$ql_use_std_function])


AC_MSG_CHECKING([whether to enable std::tuple])
AC_ARG_ENABLE([std-tuple],
AC_HELP_STRING([--enable-std-tuple],
[If enabled, std::tuple and related
functions will be used instead
of boost::tuple; this requires you to set
your compiler's standard to at least C++11.
If disabled (the default) the Boost facilities
are used.]),
[ql_use_std_tuple=$enableval],
[ql_use_std_tuple=no])
if test "$ql_use_std_tuple" = "yes" ; then
AC_DEFINE([QL_USE_STD_TUPLE],[1],
[Define this if you want to use std::tuple.])
fi
AC_MSG_RESULT([$ql_use_std_pointers])

AC_MSG_CHECKING([whether to enable available std classes])
AC_ARG_ENABLE([std-classes],
AC_HELP_STRING([--enable-std-classes],
[This is a shortcut for --enable-std-pointers
--enable-std-unique-ptr --enable-std-function.
--enable-std-unique-ptr --enable-std-function
--enable-std-tuple.
If enabled, this supersedes any --disable
option passed for the above.]),
[ql_use_std_classes=$enableval],
Expand All @@ -428,6 +447,8 @@ if test "$ql_use_std_classes" = "yes" ; then
[Define this if you want to replace std::auto_ptr with std::unique_ptr.])
AC_DEFINE([QL_USE_STD_FUNCTION],[1],
[Define this if you want to use std::function and std::bind.])
AC_DEFINE([QL_USE_STD_TUPLE],[1],
[Define this if you want to use std::tuple.])
fi
AC_MSG_RESULT([$ql_use_std_classes])

Expand Down
1 change: 1 addition & 0 deletions ql/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ this_include_HEADERS = \
termstructure.hpp \
timegrid.hpp \
timeseries.hpp \
tuple.hpp \
types.hpp \
version.hpp \
volatilitymodel.hpp
Expand Down
9 changes: 5 additions & 4 deletions ql/experimental/credit/randomdefaultlatentmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef quantlib_randomdefault_latent_model_hpp
#define quantlib_randomdefault_latent_model_hpp

#include <ql/tuple.hpp>
#include <ql/math/beta.hpp>
#include <ql/math/statistics/histogram.hpp>
#include <ql/math/statistics/riskstatistics.hpp>
Expand Down Expand Up @@ -189,7 +190,7 @@ namespace QuantLib {
virtual Real percentile(const Date& d, Real percentile) const;
/*! Returns the VaR value for a given percentile and the 95 confidence
interval of that value. */
virtual boost::tuples::tuple<Real, Real, Real> percentileAndInterval(
virtual ext::tuple<Real, Real, Real> percentileAndInterval(
const Date& d, Real percentile) const;
/*! Distributes the total VaR amount along the portfolio counterparties.
The passed loss amount is in loss units.
Expand Down Expand Up @@ -534,7 +535,7 @@ namespace QuantLib {
template<template <class, class> class D, class C, class URNG>
Real RandomLM<D, C, URNG>::percentile(const Date& d, Real perc) const {
// need to specify return type in tuples' get is parametric
return percentileAndInterval(d, perc).template get<0>();
return ext::get<0>(percentileAndInterval(d, perc));
}


Expand All @@ -545,7 +546,7 @@ namespace QuantLib {
of the stimator just computed. See the reference for a discussion.
*/
template<template <class, class> class D, class C, class URNG>
boost::tuples::tuple<Real, Real, Real> // disposable?
ext::tuple<Real, Real, Real> // disposable?
RandomLM<D, C, URNG>::percentileAndInterval(const Date& d,
Real percentile) const {

Expand Down Expand Up @@ -624,7 +625,7 @@ namespace QuantLib {
lowerPercentile = rankLosses[r];
upperPercentile = rankLosses[s];

return boost::tuples::tuple<Real, Real, Real>(quantileValue,
return ext::tuple<Real, Real, Real>(quantileValue,
lowerPercentile, upperPercentile);
}

Expand Down
45 changes: 23 additions & 22 deletions ql/experimental/credit/saddlepointlossmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef quantlib_saddle_point_lossmodel_hpp
#define quantlib_saddle_point_lossmodel_hpp

#include <ql/tuple.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/math/solvers1d/newton.hpp>
#include <ql/math/functional.hpp>
Expand Down Expand Up @@ -152,11 +153,11 @@ namespace QuantLib {
Included for optimization, most methods work on expansion of these
terms.
Alternatively use a local private buffer member? */
boost::tuples::tuple<Real, Real, Real, Real> CumGen0234DerivCond(
ext::tuple<Real, Real, Real, Real> CumGen0234DerivCond(
const std::vector<Real>& invUncondProbs,
Real saddle,
const std::vector<Real>& mktFactor) const;
boost::tuples::tuple<Real, Real> CumGen02DerivCond(
ext::tuple<Real, Real> CumGen02DerivCond(
const std::vector<Real>& invUncondProbs,
Real saddle,
const std::vector<Real>& mktFactor) const;
Expand Down Expand Up @@ -775,7 +776,7 @@ namespace QuantLib {
}

template<class CP>
boost::tuples::tuple<Real, Real, Real, Real> /// DISPOSABLE????
ext::tuple<Real, Real, Real, Real> /// DISPOSABLE????
SaddlePointLossModel<CP>::CumGen0234DerivCond(
const std::vector<Real>& invUncondProbs,
Real saddle,
Expand Down Expand Up @@ -814,12 +815,12 @@ namespace QuantLib {
(12.*suma1*suma1*suma2 -
6.*std::pow(suma1,4.)/suma0)/suma0)/suma0)/suma0;
}
return boost::tuples::tuple<Real, Real, Real, Real>(deriv0, deriv2,
return ext::tuple<Real, Real, Real, Real>(deriv0, deriv2,
deriv3, deriv4);
}

template<class CP>
boost::tuples::tuple<Real, Real> /// DISPOSABLE????
ext::tuple<Real, Real> /// DISPOSABLE????
SaddlePointLossModel<CP>::CumGen02DerivCond(
const std::vector<Real>& invUncondProbs,
Real saddle,
Expand Down Expand Up @@ -849,7 +850,7 @@ namespace QuantLib {
//deriv1 += suma1 / suma0;
deriv2 += suma2 / suma0 - std::pow(suma1 / suma0 , 2.);
}
return boost::tuples::tuple<Real, Real>(deriv0, deriv2);
return ext::tuple<Real, Real>(deriv0, deriv2);
}

// ----- Saddle point search ----------------------------------------------
Expand Down Expand Up @@ -1029,13 +1030,13 @@ namespace QuantLib {
Real saddlePt = findSaddle(invUncondProbs,
relativeLoss, mktFactor);

boost::tuples::tuple<Real, Real, Real, Real> cumulants =
ext::tuple<Real, Real, Real, Real> cumulants =
CumGen0234DerivCond(invUncondProbs,
saddlePt, mktFactor);
Real baseVal = cumulants.get<0>();
Real secondVal = cumulants.get<1>();
Real K3Saddle = cumulants.get<2>();
Real K4Saddle = cumulants.get<3>();
Real baseVal = ext::get<0>(cumulants);
Real secondVal = ext::get<1>(cumulants);
Real K3Saddle = ext::get<2>(cumulants);
Real K4Saddle = ext::get<3>(cumulants);

Real saddleTo2 = saddlePt * saddlePt;
Real saddleTo3 = saddleTo2 * saddlePt;
Expand Down Expand Up @@ -1118,11 +1119,11 @@ namespace QuantLib {
Real saddlePt = findSaddle(invUncondPs,
relativeLoss, mktFactor);

boost::tuples::tuple<Real, Real> cumulants =
ext::tuple<Real, Real> cumulants =
CumGen02DerivCond(invUncondPs,
saddlePt, mktFactor);
Real baseVal = cumulants.get<0>();
Real secondVal = cumulants.get<1>();
Real baseVal = ext::get<0>(cumulants);
Real secondVal = ext::get<1>(cumulants);

Real saddleTo2 = saddlePt * saddlePt;

Expand Down Expand Up @@ -1173,14 +1174,14 @@ namespace QuantLib {
Real saddlePt = findSaddle(invUncondPs,
relativeLoss, mktFactor);

boost::tuples::tuple<Real, Real, Real, Real> cumulants =
ext::tuple<Real, Real, Real, Real> cumulants =
CumGen0234DerivCond(invUncondPs,
saddlePt, mktFactor);
/// access them directly rather than through this copy
Real K0Saddle = cumulants.get<0>();
Real K2Saddle = cumulants.get<1>();
Real K3Saddle = cumulants.get<2>();
Real K4Saddle = cumulants.get<3>();
Real K0Saddle = ext::get<0>(cumulants);
Real K2Saddle = ext::get<1>(cumulants);
Real K3Saddle = ext::get<2>(cumulants);
Real K4Saddle = ext::get<3>(cumulants);
/* see, for instance R.Martin "he saddle point method and portfolio
optionalities." in Risk December 2006 p.93 */
//\todo the exponentials below are dangerous and agressive, tame them.
Expand Down Expand Up @@ -1384,11 +1385,11 @@ namespace QuantLib {
// Broda and Paolella:
Real elCondRatio = elCond / remainingNotional_;
boost::tuples::tuple<Real, Real, Real, Real> cumulants =
ext::tuple<Real, Real, Real, Real> cumulants =
CumGen0234DerivCond(uncondProbs,
saddlePt, mktFactor);
Real K0Saddle = cumulants.get<0>();///USE THEM DIRECTLY
Real K2Saddle = cumulants.get<1>();
Real K0Saddle = ext::get<0>(cumulants);///USE THEM DIRECTLY
Real K2Saddle = ext::get<1>(cumulants);
Real wq = std::sqrt(2. * saddlePt * lossPercRatio - 2. * K0Saddle);
//std::sqrt(-2. * saddlePt * lossPerc + 2. * K0Saddle);????
Expand Down
20 changes: 10 additions & 10 deletions ql/experimental/models/hestonslvfdmmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace QuantLib {
Time t0, Time t1, Size vGrid,
Real v0, const HestonSLVFokkerPlanckFdmParams& params) {

std::vector<boost::tuple<Real, Real, bool> > cPoints;
std::vector<ext::tuple<Real, Real, bool> > cPoints;

const Real v0Density = params.v0Density;
const Real upperBoundDensity = params.vUpperBoundDensity;
Expand All @@ -85,9 +85,9 @@ namespace QuantLib {
const Real v0Center = std::log(v0);

cPoints +=
boost::make_tuple(lowerBound, lowerBoundDensity, false),
boost::make_tuple(v0Center, v0Density, true),
boost::make_tuple(upperBound, upperBoundDensity, false);
ext::make_tuple(lowerBound, lowerBoundDensity, false),
ext::make_tuple(v0Center, v0Density, true),
ext::make_tuple(upperBound, upperBoundDensity, false);

return ext::make_shared<Concentrating1dMesher>(
lowerBound, upperBound, vGrid, cPoints, 1e-8);
Expand All @@ -98,9 +98,9 @@ namespace QuantLib {
const Real v0Center = v0;

cPoints +=
boost::make_tuple(lowerBound, lowerBoundDensity, false),
boost::make_tuple(v0Center, v0Density, true),
boost::make_tuple(upperBound, upperBoundDensity, false);
ext::make_tuple(lowerBound, lowerBoundDensity, false),
ext::make_tuple(v0Center, v0Density, true),
ext::make_tuple(upperBound, upperBoundDensity, false);

return ext::make_shared<Concentrating1dMesher>(
lowerBound, upperBound, vGrid, cPoints, 1e-8);
Expand All @@ -111,9 +111,9 @@ namespace QuantLib {
const Real v0Center = v0;

cPoints +=
boost::make_tuple(lowerBound, lowerBoundDensity, false),
boost::make_tuple(v0Center, v0Density, true),
boost::make_tuple(upperBound, upperBoundDensity, false);
ext::make_tuple(lowerBound, lowerBoundDensity, false),
ext::make_tuple(v0Center, v0Density, true),
ext::make_tuple(upperBound, upperBoundDensity, false);

return ext::make_shared<Concentrating1dMesher>(
lowerBound, upperBound, vGrid, cPoints, 1e-8);
Expand Down
10 changes: 5 additions & 5 deletions ql/methods/finitedifferences/meshers/concentrating1dmesher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ namespace QuantLib {

Concentrating1dMesher::Concentrating1dMesher(
Real start, Real end, Size size,
const std::vector<boost::tuple<Real, Real, bool> >& cPoints,
const std::vector<ext::tuple<Real, Real, bool> >& cPoints,
Real tol)
: Fdm1dMesher(size) {
using namespace ext::placeholders;

QL_REQUIRE(end > start, "end must be larger than start");

std::vector<Real> points, betas;
for (std::vector<boost::tuple<Real, Real, bool> >::const_iterator
for (std::vector<ext::tuple<Real, Real, bool> >::const_iterator
iter = cPoints.begin(); iter != cPoints.end(); ++iter) {
points.push_back(iter->get<0>());
betas.push_back(square<Real>()(iter->get<1>()*(end-start)));
points.push_back(ext::get<0>(*iter));
betas.push_back(square<Real>()(ext::get<1>(*iter)*(end-start)));
}

// get scaling factor a so that y(1) = end
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace QuantLib {
std::vector<std::pair<Real, Real> > w(1, std::make_pair(0.0, 0.0));

for (Size i=0; i < points.size(); ++i) {
if (cPoints[i].get<2>() && points[i] > start && points[i] < end) {
if (ext::get<2>(cPoints[i]) && points[i] > start && points[i] < end) {

const Size j = std::distance(y.begin(),
std::lower_bound(y.begin(), y.end(), points[i]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <ql/methods/finitedifferences/meshers/fdm1dmesher.hpp>
#include <ql/utilities/null.hpp>

#include <boost/tuple/tuple.hpp>
#include <ql/tuple.hpp>

#include <utility>
#include <vector>
Expand All @@ -47,7 +47,7 @@ namespace QuantLib {

Concentrating1dMesher(
Real start, Real end, Size size,
const std::vector<boost::tuple<Real, Real, bool> >& cPoints,
const std::vector<ext::tuple<Real, Real, bool> >& cPoints,
Real tol = 1e-8);
};
}
Expand Down
1 change: 1 addition & 0 deletions ql/quantlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <ql/termstructure.hpp>
#include <ql/timegrid.hpp>
#include <ql/timeseries.hpp>
#include <ql/tuple.hpp>
#include <ql/types.hpp>
#include <ql/volatilitymodel.hpp>

Expand Down
Loading

0 comments on commit d90d543

Please sign in to comment.