Skip to content

Commit

Permalink
Some small cleanups:
Browse files Browse the repository at this point in the history
- avoid a few copies;
- use initializer lists for readability;
- use delegating constructors to reduce duplication
  • Loading branch information
lballabio committed Aug 2, 2023
1 parent 02f5e04 commit b217876
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 77 deletions.
2 changes: 1 addition & 1 deletion ql/termstructures/yield/fittedbonddiscountcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace QuantLib {
Constraint constraint)
: constrainAtZero_(constrainAtZero), weights_(weights), l2_(std::move(l2)),
calculateWeights_(weights.empty()), optimizationMethod_(std::move(optimizationMethod)),
minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime), constraint_(constraint) {}
minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime), constraint_(std::move(constraint)) {}

void FittedBondDiscountCurve::FittingMethod::init() {
// yield conventions
Expand Down
7 changes: 3 additions & 4 deletions ql/termstructures/yield/fittedbonddiscountcurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,14 @@ namespace QuantLib {
//! return optimization method being used
ext::shared_ptr<OptimizationMethod> optimizationMethod() const;
//! return constraint of the solution being used
Constraint constraint() const;
const Constraint& constraint() const;
//! open discountFunction to public
DiscountFactor discount(const Array& x, Time t) const;
protected:
//! constructors
FittingMethod(bool constrainAtZero = true,
const Array& weights = Array(),
ext::shared_ptr<OptimizationMethod> optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
ext::shared_ptr<OptimizationMethod> optimizationMethod = {},
Array l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand Down Expand Up @@ -329,7 +328,7 @@ namespace QuantLib {
return optimizationMethod_;
}

inline Constraint
inline const Constraint&
FittedBondDiscountCurve::FittingMethod::constraint() const {
return constraint_;
}
Expand Down
95 changes: 34 additions & 61 deletions ql/termstructures/yield/nonlinearfittingmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,33 @@ namespace QuantLib {
const Size numCoeffs,
const Real fixedKappa,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
numCoeffs_(numCoeffs), fixedKappa_(fixedKappa)
{
QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required");
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)),
numCoeffs_(numCoeffs), fixedKappa_(fixedKappa) {
QL_REQUIRE(ExponentialSplinesFitting::size() > 0,
"At least 1 unconstrained coefficient required");
}

ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero,
const Array& weights,
const Array& l2, const Real minCutoffTime, const Real maxCutoffTime,
const Array& weights,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
const Size numCoeffs,
const Real fixedKappa,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime, constraint),
numCoeffs_(numCoeffs),fixedKappa_(fixedKappa)
{
QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required");
}
: ExponentialSplinesFitting(constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime,
numCoeffs, fixedKappa, std::move(constraint)) {}

ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero,
const Size numCoeffs,
const Real fixedKappa,
const Size numCoeffs,
const Real fixedKappa,
const Array& weights,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), Array(),0.0,QL_MAX_REAL, constraint),
numCoeffs_(numCoeffs), fixedKappa_(fixedKappa)
{
QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required");
}
: ExponentialSplinesFitting(constrainAtZero, weights, {}, Array(),
0.0, QL_MAX_REAL,
numCoeffs, fixedKappa, std::move(constraint)) {}

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
ExponentialSplinesFitting::clone() const {
Expand Down Expand Up @@ -113,16 +110,15 @@ namespace QuantLib {
Real minCutoffTime,
Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {}
: FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}

NelsonSiegelFitting::NelsonSiegelFitting(const Array& weights,
const Array& l2,
Real minCutoffTime,
Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(true, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime, constraint) {}
: NelsonSiegelFitting(weights, {}, l2, minCutoffTime, maxCutoffTime, std::move(constraint)) {}

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
NelsonSiegelFitting::clone() const {
Expand Down Expand Up @@ -152,16 +148,15 @@ namespace QuantLib {
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {}
: FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}

SvenssonFitting::SvenssonFitting(const Array& weights,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(true, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime, constraint) {}
: SvenssonFitting(weights, {}, l2, minCutoffTime, maxCutoffTime, std::move(constraint)) {}

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SvenssonFitting::clone() const {
Expand Down Expand Up @@ -197,8 +192,8 @@ namespace QuantLib {
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)),
splines_(3, knots.size() - 5, knots) {

QL_REQUIRE(knots.size() >= 8,
Expand All @@ -221,35 +216,14 @@ namespace QuantLib {
}

CubicBSplinesFitting::CubicBSplinesFitting(const std::vector<Time>& knots,
bool constrainAtZero,
const Array& weights,
const Array& l2,
bool constrainAtZero,
const Array& weights,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime, constraint),
splines_(3, knots.size() - 5, knots) {

QL_REQUIRE(knots.size() >= 8,
"At least 8 knots are required");
Size basisFunctions = knots.size() - 4;

if (constrainAtZero) {
size_ = basisFunctions - 1;

// Note: A small but nonzero N_th basis function at t=0 may
// lead to an ill conditioned problem
N_ = 1;

QL_REQUIRE(std::abs(splines_(N_, 0.0)) > QL_EPSILON,
"N_th cubic B-spline must be nonzero at t=0");
}
else {
size_ = basisFunctions;
N_ = 0;
}
}
: CubicBSplinesFitting(knots, constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}

Real CubicBSplinesFitting::basisFunction(Integer i, Time t) const {
return splines_(i,t);
Expand Down Expand Up @@ -303,18 +277,17 @@ namespace QuantLib {
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)),
size_(constrainAtZero ? degree : degree + 1) {}

SimplePolynomialFitting::SimplePolynomialFitting(Natural degree, bool constrainAtZero,
const Array& weights, const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights,
ext::shared_ptr<OptimizationMethod>(), l2, minCutoffTime, maxCutoffTime, constraint),
size_(constrainAtZero ? degree : degree + 1) {}
: SimplePolynomialFitting(degree, constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SimplePolynomialFitting::clone() const {
Expand Down
16 changes: 5 additions & 11 deletions ql/termstructures/yield/nonlinearfittingmethods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ namespace QuantLib {
public:
ExponentialSplinesFitting(bool constrainAtZero = true,
const Array& weights = Array(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand All @@ -73,7 +72,6 @@ namespace QuantLib {
const Array& weights = Array(),
Constraint constraint = NoConstraint{});


std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
private:
Natural numCoeffs_;
Expand All @@ -97,8 +95,7 @@ namespace QuantLib {
: public FittedBondDiscountCurve::FittingMethod {
public:
NelsonSiegelFitting(const Array& weights = Array(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand Down Expand Up @@ -131,8 +128,7 @@ namespace QuantLib {
: public FittedBondDiscountCurve::FittingMethod {
public:
SvenssonFitting(const Array& weights = Array(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand Down Expand Up @@ -174,8 +170,7 @@ namespace QuantLib {
CubicBSplinesFitting(const std::vector<Time>& knotVector,
bool constrainAtZero = true,
const Array& weights = Array(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand Down Expand Up @@ -216,8 +211,7 @@ namespace QuantLib {
SimplePolynomialFitting(Natural degree,
bool constrainAtZero = true,
const Array& weights = Array(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
ext::shared_ptr<OptimizationMethod>(),
const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Expand Down

0 comments on commit b217876

Please sign in to comment.