24#ifndef quantext_piecewiseconstant_helper_hpp
25#define quantext_piecewiseconstant_helper_hpp
29#include <ql/experimental/math/piecewisefunction.hpp>
30#include <ql/math/array.hpp>
31#include <ql/math/comparison.hpp>
32#include <ql/time/date.hpp>
43 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
45 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
47 const Array&
t()
const;
48 const QuantLib::ext::shared_ptr<Parameter>
p()
const;
51 Real
y(
const Time
t)
const;
55 Real
direct(
const Real x)
const;
61 const QuantLib::ext::shared_ptr<PseudoParameter>
y_;
64 mutable std::vector<Real>
b_;
75 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint1 = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
76 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint2 = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
78 const Handle<YieldTermStructure>& yts,
79 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint1 = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
80 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint2 = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
96 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
98 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
99 const Array&
t()
const;
100 const QuantLib::ext::shared_ptr<Parameter>
p()
const;
103 Real
y(
const Time
t)
const;
109 Real
direct(
const Real x)
const;
118 const QuantLib::ext::shared_ptr<PseudoParameter>
y_;
121 mutable std::vector<Real>
b_,
c_;
130 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint1 = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
131 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint2 = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
133 const Handle<YieldTermStructure>& yts,
134 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint1 = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
135 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& constraint2 = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
137 const Array&
t1()
const;
138 const Array&
t2()
const;
139 const Array&
tUnion()
const;
140 const QuantLib::ext::shared_ptr<Parameter>
p1()
const;
141 const QuantLib::ext::shared_ptr<Parameter>
p2()
const;
145 Real
y1(
const Time t)
const;
147 Real
y2(
const Time t)
const;
151 Real
direct1(
const Real x)
const;
153 Real
direct2(
const Real x)
const;
163 const QuantLib::ext::shared_ptr<PseudoParameter>
y1_,
y2_;
167 mutable std::vector<Real>
b_,
c_;
182 b_.resize(
t_.size());
183 for (Size i = 0; i <
t_.size(); ++i) {
202 Real
sum = 0.0, sum2 = 0.0;
203 b_.resize(
t_.size());
204 c_.resize(
t_.size());
205 for (Size i = 0; i <
t_.size(); ++i) {
206 Real t0 = (i == 0 ? 0.0 :
t_[i - 1]);
209 Real b2Tmp = (i == 0 ? 0.0 :
b_[i - 1]);
211 sum2 += (
t_[i] - t0) * std::exp(-b2Tmp);
213 sum2 += (std::exp(-b2Tmp) - std::exp(-b2Tmp -
direct(
y_->params()[i]) * (
t_[i] - t0))) /
237 std::vector<Real> tTmp(
t1_.begin(),
t1_.end());
238 tTmp.insert(tTmp.end(),
t2_.begin(),
t2_.end());
239 std::sort(tTmp.begin(), tTmp.end());
240 std::vector<Real>::const_iterator end =
241 std::unique(tTmp.begin(), tTmp.end(), [](
const Real x,
const Real y) { return QuantLib::close_enough(x, y); });
242 tTmp.resize(end - tTmp.begin());
243 tUnion_ = Array(tTmp.begin(), tTmp.end());
246 for (Size i = 0; i <
tUnion_.size() + 1; ++i) {
253 Real
sum = 0.0, sum2 = 0.0;
256 for (Size i = 0; i <
tUnion_.size(); ++i) {
257 Real t0 = (i == 0 ? 0.0 :
tUnion_[i - 1]);
260 Real b2Tmp = (i == 0 ? 0.0 :
b_[i - 1]);
273 return direct(QL_PIECEWISE_FUNCTION(
t_,
y_->params(),
t));
277 return direct(QL_PIECEWISE_FUNCTION(
t_,
y_->params(),
t));
281 return direct1(QL_PIECEWISE_FUNCTION(
t1_,
y1_->params(), t));
285 return direct2(QL_PIECEWISE_FUNCTION(
t2_,
y2_->params(), t));
291 Size i = std::upper_bound(
t_.begin(),
t_.end(),
t) -
t_.begin();
294 res +=
b_[std::min(i - 1,
b_.size() - 1)];
295 Real a =
direct(
y_->params()[std::min(i,
y_->size() - 1)]);
296 res += a * a * (
t - (i == 0 ? 0.0 :
t_[i - 1]));
303 Size i = std::upper_bound(
t_.begin(),
t_.end(),
t) -
t_.begin();
306 res +=
b_[std::min(i - 1,
b_.size() - 1)];
307 Real a =
y_->params()[std::min(i,
y_->size() - 1)];
308 res += a * (
t - (i == 0 ? 0.0 :
t_[i - 1]));
309 return std::exp(-res);
315 Size i = std::upper_bound(
t_.begin(),
t_.end(),
t) -
t_.begin();
318 res +=
c_[std::min(i - 1,
c_.size() - 1)];
319 Real a =
direct(
y_->params()[std::min(i,
y_->size() - 1)]);
320 Real t0 = (i == 0 ? 0.0 :
t_[i - 1]);
321 Real b2Tmp = (i == 0 ? 0.0 :
b_[i - 1]);
323 res += std::exp(-b2Tmp) * (
t - t0);
325 res += (std::exp(-b2Tmp) - std::exp(-b2Tmp - a * (
t - t0))) / a;
336 res +=
c_[std::min(i - 1,
c_.size() - 1)];
339 Real t0 = (i == 0 ? 0.0 :
tUnion_[i - 1]);
340 Real b2Tmp = (i == 0 ? 0.0 :
b_[i - 1]);
342 res += b * b * std::exp(2.0 * b2Tmp) * (t - t0);
344 res += b * b * (std::exp(2.0 * b2Tmp + 2.0 * a * (t - t0)) - std::exp(2.0 * b2Tmp)) / (2.0 * a);
Piecewise Constant Helper 11.
const PiecewiseConstantHelper1 h2_
const PiecewiseConstantHelper1 h1_
const PiecewiseConstantHelper1 & helper1() const
PiecewiseConstantHelper11(const Array &t1, const Array &t2, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint1=QuantLib::ext::make_shared< QuantLib::NoConstraint >(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint2=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
PiecewiseConstantHelper11(const std::vector< Date > &dates1, const std::vector< Date > &dates2, const Handle< YieldTermStructure > &yts, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint1=QuantLib::ext::make_shared< QuantLib::NoConstraint >(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint2=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
const PiecewiseConstantHelper1 & helper2() const
Piecewise Constant Helper 1.
Real y(const Time t) const
Real inverse(const Real y) const
PiecewiseConstantHelper1(const std::vector< Date > &dates, const Handle< YieldTermStructure > &yts, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
const QuantLib::ext::shared_ptr< PseudoParameter > y_
const QuantLib::ext::shared_ptr< Parameter > p() const
Real int_y_sqr(const Time t) const
int_0^t y^2(s) ds
PiecewiseConstantHelper1(const Array &t, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real direct(const Real x) const
Piecewise Constant Helper2.
Real y(const Time t) const
Real inverse(const Real y) const
PiecewiseConstantHelper2(const Array &t, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real exp_m_int_y(const Time t) const
exp(int_0^t -y(s)) ds
PiecewiseConstantHelper2(const std::vector< Date > &dates, const Handle< YieldTermStructure > &yts, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real int_exp_m_int_y(const Time t) const
int_0^t exp(int_0^s -y(u) du) ds
const QuantLib::ext::shared_ptr< PseudoParameter > y_
const QuantLib::ext::shared_ptr< Parameter > p() const
Real direct(const Real x) const
Piecewise Constant Helper 3.
const QuantLib::ext::shared_ptr< PseudoParameter > y1_
PiecewiseConstantHelper3(const Array &t1, const Array &t2, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint1=QuantLib::ext::make_shared< QuantLib::NoConstraint >(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint2=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real y1(const Time t) const
Real inverse2(const Real y) const
Real int_y1_sqr_exp_2_int_y2(const Time t) const
int_0^t y1^2(s) exp(2*int_0^s y2(u) du) ds
const Array & tUnion() const
Real direct1(const Real x) const
Real y2(const Time t) const
const QuantLib::ext::shared_ptr< Parameter > p2() const
const QuantLib::ext::shared_ptr< PseudoParameter > y2_
Real inverse1(const Real y) const
const QuantLib::ext::shared_ptr< Parameter > p1() const
PiecewiseConstantHelper3(const std::vector< Date > &dates1, const std::vector< Date > &dates2, const Handle< YieldTermStructure > &yts, const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint1=QuantLib::ext::make_shared< QuantLib::NoConstraint >(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &constraint2=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real direct2(const Real x) const
Real sum(const Cash &c, const Cash &d)
parameter giving access to calibration machinery