35 class FittedBondDiscountCurve::FittingMethod::FittingCost
36 :
public CostFunction {
37 friend class FittedBondDiscountCurve::FittingMethod;
40 FittedBondDiscountCurve::FittingMethod* fittingMethod);
41 Real value(
const Array& x)
const override;
42 Array values(
const Array& x)
const override;
45 FittedBondDiscountCurve::FittingMethod* fittingMethod_;
52 vector<ext::shared_ptr<BondHelper> > bondHelpers,
59 Size maxStationaryStateIterations)
61 maxEvaluations_(maxEvaluations), simplexLambda_(simplexLambda),
62 maxStationaryStateIterations_(maxStationaryStateIterations), guessSolution_(
std::move(guess)),
63 bondHelpers_(
std::move(bondHelpers)), fittingMethod_(fittingMethod) {
70 const Date& referenceDate,
71 vector<ext::shared_ptr<BondHelper> > bondHelpers,
78 Size maxStationaryStateIterations)
80 maxEvaluations_(maxEvaluations), simplexLambda_(simplexLambda),
81 maxStationaryStateIterations_(maxStationaryStateIterations), guessSolution_(
std::move(guess)),
82 bondHelpers_(
std::move(bondHelpers)), fittingMethod_(fittingMethod) {
96 maxEvaluations_(0), guessSolution_(
std::move(parameters)),
97 maxDate_(maxDate), fittingMethod_(fittingMethod) {
104 const Date& referenceDate,
110 maxEvaluations_(0), guessSolution_(
std::move(parameters)),
111 maxDate_(maxDate), fittingMethod_(fittingMethod) {
136 "no bond helpers or max date given");
148 bond->maturityDate() <<
") has an invalid price quote");
149 Date bondSettlement = bond->settlementDate();
152 bondSettlement <<
") before curve reference date (" <<
156 bondSettlement <<
" settlement date (maturity"
157 " being " << bond->maturityDate() <<
")");
170 bool constrainAtZero,
171 const Array& weights,
172 ext::shared_ptr<OptimizationMethod> optimizationMethod,
174 const Real minCutoffTime,
175 const Real maxCutoffTime,
177 : constrainAtZero_(constrainAtZero), weights_(weights), l2_(
std::move(l2)),
178 calculateWeights_(weights.empty()), optimizationMethod_(
std::move(optimizationMethod)),
179 constraint_(
std::move(constraint)),
180 minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime) {
187 if (
curve_->maxEvaluations_ == 0)
196 costFunction_ = ext::make_shared<FittingCost>(
this);
198 for (
auto& bondHelper :
curve_->bondHelpers_) {
199 bondHelper->setTermStructure(
curve_);
202 if (calculateWeights_) {
203 if (weights_.empty())
206 Real squaredSum = 0.0;
207 for (
Size i=0; i<
curve_->bondHelpers_.size(); ++i) {
208 ext::shared_ptr<Bond> bond =
curve_->bondHelpers_[i]->bond();
210 Real amount =
curve_->bondHelpers_[i]->quote()->value();
213 Date bondSettlement = bond->settlementDate();
215 yieldDC, yieldComp, yieldFreq,
219 yieldDC, yieldComp, yieldFreq,
222 weights_[i] = 1.0/dur;
223 squaredSum += weights_[i]*weights_[i];
225 weights_ /= std::sqrt(squaredSum);
229 "Given weights do not cover all boostrapping helpers");
233 "Given penalty factors do not cover all parameters");
241 if (
curve_->maxEvaluations_ == 0)
251 "wrong number of parameters");
253 solution_ =
curve_->guessSolution_;
255 numberOfIterations_ = 0;
262 FittingCost& costFunction = *costFunction_;
265 Array x(size(), 0.0);
266 if (!
curve_->guessSolution_.empty()) {
267 QL_REQUIRE(
curve_->guessSolution_.size() == size(),
"wrong size for guess");
268 x =
curve_->guessSolution_;
272 ext::shared_ptr<OptimizationMethod> optimization = optimizationMethod_;
274 optimization = ext::make_shared<Simplex>(
curve_->simplexLambda_);
276 Problem problem(costFunction, constraint_, x);
283 curve_->maxStationaryStateIterations_,
286 gradientNormEpsilon);
288 errorCode_ = optimization->minimize(problem,endCriteria);
295 curve_->guessSolution_ = solution_;
299 FittedBondDiscountCurve::FittingMethod::FittingCost::FittingCost(
304 Real FittedBondDiscountCurve::FittingMethod::FittingCost::value(
305 const Array& x)
const {
306 Real squaredError = 0.0;
307 Array vals = values(x);
308 for (
Real val : vals) {
314 Array FittedBondDiscountCurve::FittingMethod::FittingCost::values(
const Array &x)
const {
323 for (
Size i=0; i<
n; ++i) {
324 ext::shared_ptr<BondHelper> helper =
fittingMethod_->curve_->bondHelpers_[i];
325 Real error = helper->impliedQuote() - helper->quote()->value();
327 values[i] = weightedError * weightedError;
331 for (
Size i = 0; i < N; ++i) {
ZeroSpreadedTermStructure curve_
Cash-flow analysis functions.
1-D array used in linear algebra.
bool empty() const
whether the array is empty
Size size() const
dimension of the array
static Date minDate()
earliest allowed date
Criteria to end optimization process:
Base fitting method used to construct a fitted bond discount curve.
virtual void init()
rerun every time instruments/referenceDate changes
FittingMethod(bool constrainAtZero=true, const Array &weights=Array(), ext::shared_ptr< OptimizationMethod > optimizationMethod=ext::shared_ptr< OptimizationMethod >(), Array l2=Array(), Real minCutoffTime=0.0, Real maxCutoffTime=QL_MAX_REAL, Constraint constraint=NoConstraint())
constructors
Discount curve fitted to a set of fixed-coupon bonds.
void performCalculations() const override
std::vector< ext::shared_ptr< BondHelper > > bondHelpers_
Clone< FittingMethod > fittingMethod_
void resetGuess(const Array &guess)
FittedBondDiscountCurve(Natural settlementDays, const Calendar &calendar, std::vector< ext::shared_ptr< BondHelper > > bonds, const DayCounter &dayCounter, const FittingMethod &fittingMethod, Real accuracy=1.0e-10, Size maxEvaluations=10000, Array guess=Array(), Real simplexLambda=1.0, Size maxStationaryStateIterations=100)
reference date based on current evaluation date
template class providing a null value for a given type.
Constrained optimization problem.
const Array & currentValue() const
current value of the local minimum
Real functionValue() const
value of cost function
Integer functionEvaluation() const
number of evaluation of cost function
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
Interest-rate term structure.
Abstract constraint class.
Optimization cost function class.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
discount curve fitted to a set of bonds
Frequency
Frequency of events.
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
Real Time
continuous quantity with 1-year units
unsigned QL_INTEGER Natural
positive integer
std::size_t Size
size of a container
Compounding
Interest rate coumpounding rule.
Simple day counter for reproducing theoretical calculations.
Simplex optimization method.
static bool isTradable(const Bond &bond, Date settlementDate=Date())
static Rate yield(const Bond &bond, Bond::Price price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05)
static Time duration(const Bond &bond, const InterestRate &yield, Duration::Type type=Duration::Modified, Date settlementDate=Date())