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) {
101 bond->maturityDate() <<
") has an invalid price quote");
102 Date bondSettlement = bond->settlementDate();
105 bondSettlement <<
") before curve reference date (" <<
109 bondSettlement <<
" settlement date (maturity"
110 " being " << bond->maturityDate() <<
")");
121 bool constrainAtZero,
122 const Array& weights,
123 ext::shared_ptr<OptimizationMethod> optimizationMethod,
125 const Real minCutoffTime,
126 const Real maxCutoffTime)
127 : constrainAtZero_(constrainAtZero), weights_(weights), l2_(
std::move(l2)),
128 calculateWeights_(weights.empty()), optimizationMethod_(
std::move(optimizationMethod)),
129 minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime) {}
138 costFunction_ = ext::make_shared<FittingCost>(
this);
140 for (
auto& bondHelper :
curve_->bondHelpers_) {
141 bondHelper->setTermStructure(
curve_);
144 if (calculateWeights_) {
145 if (weights_.empty())
148 Real squaredSum = 0.0;
149 for (
Size i=0; i<
curve_->bondHelpers_.size(); ++i) {
150 ext::shared_ptr<Bond> bond =
curve_->bondHelpers_[i]->bond();
152 Real amount =
curve_->bondHelpers_[i]->quote()->value();
155 Date bondSettlement = bond->settlementDate();
157 yieldDC, yieldComp, yieldFreq,
161 yieldDC, yieldComp, yieldFreq,
164 weights_[i] = 1.0/dur;
165 squaredSum += weights_[i]*weights_[i];
167 weights_ /= std::sqrt(squaredSum);
171 "Given weights do not cover all boostrapping helpers");
175 "Given penalty factors do not cover all parameters");
183 FittingCost& costFunction = *costFunction_;
187 Array x(size(), 0.0);
188 if (!
curve_->guessSolution_.empty()) {
189 x =
curve_->guessSolution_;
192 if (
curve_->maxEvaluations_ == 0)
202 solution_ =
curve_->guessSolution_;
204 numberOfIterations_ = 0;
205 costValue_ = costFunction.value(solution_);
212 ext::shared_ptr<OptimizationMethod> optimization = optimizationMethod_;
214 optimization = ext::make_shared<Simplex>(
curve_->simplexLambda_);
216 Problem problem(costFunction, constraint, x);
223 curve_->maxStationaryStateIterations_,
226 gradientNormEpsilon);
228 errorCode_ = optimization->minimize(problem,endCriteria);
235 curve_->guessSolution_ = solution_;
239 FittedBondDiscountCurve::FittingMethod::FittingCost::FittingCost(
244 Real FittedBondDiscountCurve::FittingMethod::FittingCost::value(
245 const Array& x)
const {
246 Real squaredError = 0.0;
247 Array vals = values(x);
248 for (
Real val : vals) {
254 Array FittedBondDiscountCurve::FittingMethod::FittingCost::values(
const Array &x)
const {
263 for (
Size i=0; i<
n; ++i) {
264 ext::shared_ptr<BondHelper> helper =
fittingMethod_->curve_->bondHelpers_[i];
265 Real error = helper->impliedQuote() - helper->quote()->value();
267 values[i] = weightedError * weightedError;
271 for (
Size i = 0; i < N; ++i) {
ZeroSpreadedTermStructure curve_
Cash-flow analysis functions.
1-D array used in linear algebra.
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)
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_
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
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 Rate yield(const Bond &bond, Real price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean)
static bool isTradable(const Bond &bond, Date settlementDate=Date())
static Time duration(const Bond &bond, const InterestRate &yield, Duration::Type type=Duration::Modified, Date settlementDate=Date())