1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Allen Kuo
Copyright (C) 2010 Alessandro Roveda
Copyright (C) 2015 Andres Hernandez
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/math/bernsteinpolynomial.hpp>
#include <ql/termstructures/yield/nonlinearfittingmethods.hpp>
#include <utility>
namespace QuantLib {
ExponentialSplinesFitting::ExponentialSplinesFitting(
bool constrainAtZero,
const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
const Size numCoeffs,
const Real fixedKappa,
Constraint constraint)
: 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 Size numCoeffs, const Real fixedKappa,
Constraint constraint)
: ExponentialSplinesFitting(constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime,
numCoeffs, fixedKappa, std::move(constraint)) {}
ExponentialSplinesFitting::ExponentialSplinesFitting(
bool constrainAtZero,
const Size numCoeffs,
const Real fixedKappa,
const Array& weights,
Constraint constraint)
: ExponentialSplinesFitting(constrainAtZero, weights, {}, Array(),
0.0, QL_MAX_REAL,
numCoeffs, fixedKappa, std::move(constraint)) {}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
ExponentialSplinesFitting::clone() const {
return std::make_unique<ExponentialSplinesFitting>(*this);
}
Size ExponentialSplinesFitting::size() const {
Size N = constrainAtZero_ ? numCoeffs_ : numCoeffs_ + 1;
return (fixedKappa_ != Null<Real>()) ? N-1 : N; //One fewer optimization parameters if kappa is fixed
}
DiscountFactor ExponentialSplinesFitting::discountFunction(const Array& x,
Time t) const {
DiscountFactor d = 0.0;
Size N = size();
//Use the interal fixedKappa_ member if non-zero, otherwise take kappa from the passed x[] array
Real kappa = (fixedKappa_ != Null<Real>()) ? fixedKappa_: x[N-1];
Real coeff = 0;
if (!constrainAtZero_) {
for (Size i = 0; i < N - 1; ++i) {
d += x[i] * std::exp(-kappa * (i + 1) * t);
}
} else {
// notation:
// d(t) = coeff* exp(-kappa*1*t) + x[0]* exp(-kappa*2*t) +
// x[1]* exp(-kappa*3*t) + ..+ x[7]* exp(-kappa*9*t)
for (Size i = 0; i < N - 1; i++) {
d += x[i] * std::exp(-kappa * (i + 2) * t);
coeff += x[i];
}
coeff = 1.0 - coeff;
d += coeff * std::exp(-kappa * t);
}
return d;
}
NelsonSiegelFitting::NelsonSiegelFitting(
const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}
NelsonSiegelFitting::NelsonSiegelFitting(
const Array& weights,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: NelsonSiegelFitting(weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
NelsonSiegelFitting::clone() const {
return std::make_unique<NelsonSiegelFitting>(*this);
}
Size NelsonSiegelFitting::size() const {
return 4;
}
DiscountFactor NelsonSiegelFitting::discountFunction(const Array& x,
Time t) const {
Real kappa = x[size()-1];
Real zeroRate = x[0] + (x[1] + x[2])*
(1.0 - std::exp(-kappa*t))/
((kappa+QL_EPSILON)*(t+QL_EPSILON)) -
(x[2])*std::exp(-kappa*t);
DiscountFactor d = std::exp(-zeroRate * t) ;
return d;
}
SvenssonFitting::SvenssonFitting(const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint 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)
: SvenssonFitting(weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SvenssonFitting::clone() const {
return std::make_unique<SvenssonFitting>(*this);
}
Size SvenssonFitting::size() const {
return 6;
}
DiscountFactor SvenssonFitting::discountFunction(const Array& x,
Time t) const {
Real kappa = x[size()-2];
Real kappa_1 = x[size()-1];
Real zeroRate = x[0] + (x[1] + x[2])*
(1.0 - std::exp(-kappa*t))/
((kappa+QL_EPSILON)*(t+QL_EPSILON)) -
(x[2])*std::exp(-kappa*t) +
x[3]* (((1.0 - std::exp(-kappa_1*t))/((kappa_1+QL_EPSILON)*(t+QL_EPSILON)))- std::exp(-kappa_1*t));
DiscountFactor d = std::exp(-zeroRate * t) ;
return d;
}
CubicBSplinesFitting::CubicBSplinesFitting(
const std::vector<Time>& knots,
bool constrainAtZero,
const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2,
minCutoffTime, maxCutoffTime, std::move(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::CubicBSplinesFitting(
const std::vector<Time>& knots,
bool constrainAtZero,
const Array& weights,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint constraint)
: CubicBSplinesFitting(knots, constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}
Real CubicBSplinesFitting::basisFunction(Integer i, Time t) const {
return splines_(i,t);
}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
CubicBSplinesFitting::clone() const {
return std::make_unique<CubicBSplinesFitting>(*this);
}
Size CubicBSplinesFitting::size() const {
return size_;
}
DiscountFactor CubicBSplinesFitting::discountFunction(const Array& x,
Time t) const {
DiscountFactor d = 0.0;
if (!constrainAtZero_) {
for (Size i=0; i<size_; ++i) {
d += x[i] * splines_(i,t);
}
} else {
const Real T = 0.0;
Real sum = 0.0;
for (Size i=0; i<size_; ++i) {
if (i < N_) {
d += x[i] * splines_(i,t);
sum += x[i] * splines_(i,T);
} else {
d += x[i] * splines_(i+1,t);
sum += x[i] * splines_(i+1,T);
}
}
Real coeff = 1.0 - sum;
coeff /= splines_(N_,T);
d += coeff * splines_(N_,t);
}
return d;
}
SimplePolynomialFitting::SimplePolynomialFitting(
Natural degree,
bool constrainAtZero,
const Array& weights,
const ext::shared_ptr<OptimizationMethod>& optimizationMethod,
const Array& l2,
const Real minCutoffTime,
const Real maxCutoffTime,
Constraint 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)
: SimplePolynomialFitting(degree, constrainAtZero, weights, {}, l2,
minCutoffTime, maxCutoffTime, std::move(constraint)) {}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SimplePolynomialFitting::clone() const {
return std::make_unique<SimplePolynomialFitting>(*this);
}
Size SimplePolynomialFitting::size() const {
return size_;
}
DiscountFactor SimplePolynomialFitting::discountFunction(const Array& x,
Time t) const {
DiscountFactor d = 0.0;
if (!constrainAtZero_) {
for (Size i=0; i<size_; ++i)
d += x[i] * BernsteinPolynomial::get(i,i,t);
} else {
d = 1.0;
for (Size i=0; i<size_; ++i)
d += x[i] * BernsteinPolynomial::get(i+1,i+1,t);
}
return d;
}
SpreadFittingMethod::SpreadFittingMethod(const ext::shared_ptr<FittingMethod>& method,
Handle<YieldTermStructure> discountCurve,
const Real minCutoffTime,
const Real maxCutoffTime)
: FittedBondDiscountCurve::FittingMethod(
method != nullptr ? method->constrainAtZero() : true,
method != nullptr ? method->weights() : Array(),
method != nullptr ? method->optimizationMethod() : ext::shared_ptr<OptimizationMethod>(),
method != nullptr ? method->l2() : Array(),
minCutoffTime,
maxCutoffTime),
method_(method), discountingCurve_(std::move(discountCurve)) {
QL_REQUIRE(method, "Fitting method is empty");
QL_REQUIRE(!discountingCurve_.empty(), "Discounting curve cannot be empty");
}
std::unique_ptr<FittedBondDiscountCurve::FittingMethod>
SpreadFittingMethod::clone() const {
return std::make_unique<SpreadFittingMethod>(*this);
}
Size SpreadFittingMethod::size() const {
return method_->size();
}
DiscountFactor SpreadFittingMethod::discountFunction(const Array& x, Time t) const{
return method_->discount(x, t)*discountingCurve_->discount(t, true)/rebase_;
}
void SpreadFittingMethod::init(){
//In case discount curve has a different reference date,
//discount to this curve's reference date
if (curve_->referenceDate() != discountingCurve_->referenceDate()){
rebase_ = discountingCurve_->discount(curve_->referenceDate());
}
else{
rebase_ = 1.0;
}
//Call regular init
FittedBondDiscountCurve::FittingMethod::init();
}
}
|