Skip to content

Commit

Permalink
add constraint to fittingmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Cay Oest committed Jul 18, 2023
1 parent d9e8af7 commit 9c5321e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 4 additions & 5 deletions ql/termstructures/yield/fittedbonddiscountcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

#include <ql/cashflows/cashflows.hpp>
#include <ql/math/optimization/constraint.hpp>
#include <ql/math/optimization/costfunction.hpp>
#include <ql/math/optimization/simplex.hpp>
#include <ql/pricingengines/bond/bondfunctions.hpp>
Expand Down Expand Up @@ -123,10 +122,11 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod> optimizationMethod,
Array l2,
const Real minCutoffTime,
const Real maxCutoffTime)
const Real maxCutoffTime,
Constraint constraint)
: constrainAtZero_(constrainAtZero), weights_(weights), l2_(std::move(l2)),
calculateWeights_(weights.empty()), optimizationMethod_(std::move(optimizationMethod)),
minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime) {}
minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime), constraint_(constraint) {}

void FittedBondDiscountCurve::FittingMethod::init() {
// yield conventions
Expand Down Expand Up @@ -178,7 +178,6 @@ namespace QuantLib {
void FittedBondDiscountCurve::FittingMethod::calculate() {

FittingCost& costFunction = *costFunction_;
Constraint constraint = NoConstraint();

// start with the guess solution, if it exists
Array x(size(), 0.0);
Expand Down Expand Up @@ -210,7 +209,7 @@ namespace QuantLib {
if(!optimization){
optimization = ext::make_shared<Simplex>(curve_->simplexLambda_);
}
Problem problem(costFunction, constraint, x);
Problem problem(costFunction, constraint_, x);

Real rootEpsilon = curve_->accuracy_;
Real functionEpsilon = curve_->accuracy_;
Expand Down
13 changes: 12 additions & 1 deletion ql/termstructures/yield/fittedbonddiscountcurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <ql/termstructures/yield/bondhelpers.hpp>
#include <ql/math/optimization/method.hpp>
#include <ql/math/optimization/constraint.hpp>
#include <ql/patterns/lazyobject.hpp>
#include <ql/math/array.hpp>
#include <ql/utilities/clone.hpp>
Expand Down Expand Up @@ -203,6 +204,8 @@ namespace QuantLib {
Array l2() const;
//! return optimization method being used
ext::shared_ptr<OptimizationMethod> optimizationMethod() const;
//! return constraint of the solution being used
Constraint constraint() const;
//! open discountFunction to public
DiscountFactor discount(const Array& x, Time t) const;
protected:
Expand All @@ -213,7 +216,8 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod>(),
Array l2 = Array(),
Real minCutoffTime = 0.0,
Real maxCutoffTime = QL_MAX_REAL);
Real maxCutoffTime = QL_MAX_REAL,
Constraint constraint = NoConstraint{});
//! rerun every time instruments/referenceDate changes
virtual void init();
//! discount function called by FittedBondDiscountCurve
Expand Down Expand Up @@ -253,6 +257,8 @@ namespace QuantLib {
ext::shared_ptr<OptimizationMethod> optimizationMethod_;
// flat extrapolation of instantaneous forward before / after cutoff
Real minCutoffTime_, maxCutoffTime_;
// constraint for the solution
Constraint constraint_;
};

// inline
Expand Down Expand Up @@ -323,6 +329,11 @@ namespace QuantLib {
return optimizationMethod_;
}

inline Constraint
FittedBondDiscountCurve::FittingMethod::constraint() const {
return constraint_;
}

inline DiscountFactor FittedBondDiscountCurve::FittingMethod::discount(const Array& x, Time t) const {
if (t < minCutoffTime_) {
// flat fwd extrapolation before min cutoff time
Expand Down

0 comments on commit 9c5321e

Please sign in to comment.