Skip to content

Commit

Permalink
add constraint to nonlinearfittingmethods
Browse files Browse the repository at this point in the history
  • Loading branch information
Cay Oest committed Jul 18, 2023
1 parent 9601be9 commit ffbc855
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
66 changes: 42 additions & 24 deletions ql/termstructures/yield/nonlinearfittingmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ namespace QuantLib {
const Real minCutoffTime,
const Real maxCutoffTime,
const Size numCoeffs,
const Real fixedKappa)
const Real fixedKappa,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime),
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
numCoeffs_(numCoeffs), fixedKappa_(fixedKappa)
{
QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required");
Expand All @@ -44,9 +45,11 @@ namespace QuantLib {
ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero,
const Array& weights,
const Array& l2, const Real minCutoffTime, const Real maxCutoffTime,
const Size numCoeffs, const Real fixedKappa)
const Size numCoeffs,
const Real fixedKappa,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime),
minCutoffTime, maxCutoffTime, constraint),
numCoeffs_(numCoeffs),fixedKappa_(fixedKappa)
{
QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required");
Expand All @@ -55,8 +58,9 @@ namespace QuantLib {
ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero,
const Size numCoeffs,
const Real fixedKappa,
const Array& weights )
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), Array(),0.0,QL_MAX_REAL),
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");
Expand Down Expand Up @@ -106,16 +110,19 @@ namespace QuantLib {
const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime)
Real minCutoffTime,
Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime) {}
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {}

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

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
NelsonSiegelFitting::clone() const {
Expand Down Expand Up @@ -143,14 +150,18 @@ namespace QuantLib {
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime)
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime) {}
true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {}

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

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SvenssonFitting::clone() const {
Expand Down Expand Up @@ -184,9 +195,10 @@ namespace QuantLib {
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime)
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime),
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
splines_(3, knots.size() - 5, knots) {

QL_REQUIRE(knots.size() >= 8,
Expand All @@ -212,9 +224,11 @@ namespace QuantLib {
bool constrainAtZero,
const Array& weights,
const Array& l2,
const Real minCutoffTime, const Real maxCutoffTime)
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr<OptimizationMethod>(), l2,
minCutoffTime, maxCutoffTime),
minCutoffTime, maxCutoffTime, constraint),
splines_(3, knots.size() - 5, knots) {

QL_REQUIRE(knots.size() >= 8,
Expand Down Expand Up @@ -287,16 +301,19 @@ namespace QuantLib {
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime)
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime),
constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint),
size_(constrainAtZero ? degree : degree + 1) {}

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

std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
Expand Down Expand Up @@ -327,7 +344,8 @@ namespace QuantLib {
SpreadFittingMethod::SpreadFittingMethod(const ext::shared_ptr<FittingMethod>& method,
Handle<YieldTermStructure> discountCurve,
const Real minCutoffTime,
const Real maxCutoffTime)
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(
method != nullptr ? method->constrainAtZero() : true,
method != nullptr ? method->weights() : Array(),
Expand Down
36 changes: 24 additions & 12 deletions ql/termstructures/yield/nonlinearfittingmethods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,21 @@ namespace QuantLib {
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Size numCoeffs = 9,
Real fixedKappa = Null<Real>());
Real fixedKappa = Null<Real>(),
Constraint constraint = NoConstraint{});
ExponentialSplinesFitting(bool constrainAtZero,
const Array& weights,
const Array& l2,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL,
Size numCoeffs = 9,
Real fixedKappa = Null<Real>());
Real fixedKappa = Null<Real>(),
Constraint constraint = NoConstraint{});
ExponentialSplinesFitting(bool constrainAtZero,
Size numCoeffs,
Real fixedKappa,
const Array& weights = Array() );
const Array& weights = Array(),
Constraint constraint = NoConstraint{});


std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
Expand Down Expand Up @@ -98,11 +101,13 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod>(),
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
NelsonSiegelFitting(const Array& weights,
const Array& l2,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
private:
Size size() const override;
Expand Down Expand Up @@ -130,11 +135,13 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod>(),
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
SvenssonFitting(const Array& weights,
const Array& l2,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
private:
Size size() const override;
Expand Down Expand Up @@ -171,13 +178,15 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod>(),
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
CubicBSplinesFitting(const std::vector<Time>& knotVector,
bool constrainAtZero,
const Array& weights,
const Array& l2,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
//! cubic B-spline basis functions
Real basisFunction(Integer i, Time t) const;
std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
Expand Down Expand Up @@ -211,13 +220,15 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod>(),
const Array& l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
SimplePolynomialFitting(Natural degree,
bool constrainAtZero,
const Array& weights,
const Array& l2,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
private:
Size size() const override;
Expand All @@ -235,7 +246,8 @@ namespace QuantLib {
SpreadFittingMethod(const ext::shared_ptr<FittingMethod>& method,
Handle<YieldTermStructure> discountCurve,
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
protected:
void init() override;
Expand Down

0 comments on commit ffbc855

Please sign in to comment.