32#ifndef ql_xabr_interpolation_hpp
33#define ql_xabr_interpolation_hpp
54 const std::vector<Real>& params,
55 const std::vector<bool>& paramIsFixed,
56 std::vector<Real> addParams)
59 QL_REQUIRE(
t > 0.0,
"expiry time must be positive: " <<
t
61 QL_REQUIRE(params.size() == Model().dimension(),
62 "wrong number of parameters (" << params.size()
64 << Model().dimension());
65 QL_REQUIRE(paramIsFixed.size() == Model().dimension(),
66 "wrong number of fixed parameters flags ("
67 << paramIsFixed.size() <<
"), should be "
68 << Model().dimension());
70 for (
Size i = 0; i < params.size(); ++i) {
99template <
class I1,
class I2,
typename Model>
108 const std::vector<Real>& params,
109 const std::vector<bool>& paramIsFixed,
111 ext::shared_ptr<EndCriteria> endCriteria,
112 ext::shared_ptr<OptimizationMethod> optMethod,
113 const Real errorAccept,
114 const bool useMaxError,
115 const Size maxGuesses,
116 const std::vector<Real>& addParams = std::vector<Real>(),
125 optMethod_ = ext::shared_ptr<OptimizationMethod>(
131 60000, 100, 1e-8, 1e-8, 1e-8);
134 std::vector<Real>(xEnd - xBegin, 1.0 / (xEnd - xBegin));
149 Real weightsSum = 0.0;
150 for (; x != this->
xEnd_; ++x, ++
y) {
151 Real stdDev = std::sqrt((*
y) * (*
y) * this->
t_);
154 weightsSum += this->
weights_.back();
158 for (; w != this->
weights_.end(); ++w)
164 this->paramIsFixed_.end(),
true,
165 std::logical_and<>())) {
173 Array guess(Model().dimension());
174 for (
Size i = 0; i < guess.
size(); ++i)
178 Size freeParameters = 0;
180 Array bestParameters;
181 for (
Size i = 0; i < Model().dimension(); ++i)
186 Real tmpInterpolationError;
190 if (iterations > 0) {
193 this->
t_,
s.value, this->addParams_);
203 costFunction, inversedTransformatedGuess,
206 Array projectedGuess(
207 constrainedXABRError.
project(inversedTransformatedGuess));
210 Problem problem(constrainedXABRError, constraint,
215 constrainedXABRError.
include(projectedResult));
222 if (tmpInterpolationError < bestError) {
223 bestError = tmpInterpolationError;
224 bestParameters = result;
231 for (
Size i = 0; i < bestParameters.
size(); ++i)
232 this->
params_[i] = bestParameters[i];
247 Real error, totalError = 0.0;
251 for (; x != this->
xEnd_; ++x, ++
y, ++w) {
253 totalError += error * error * (*w);
265 for (; x != this->
xEnd_; ++x, ++
r, ++w, ++
y) {
266 *
r = (
value(*x) - *
y) * std::sqrt(*w);
274 return std::sqrt(
n * squaredError / (
n==1 ? 1 : (
n - 1)));
281 for (; i != this->
xEnd_; ++i, ++j) {
282 error = std::fabs(
value(*i) - *j);
283 maxError = std::max(maxError, error);
1-D array used in linear algebra.
Size size() const
dimension of the array
Cost function abstract class for optimization problem.
Halton low-discrepancy sequence generator.
const sample_type & nextSequence() const
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
Levenberg-Marquardt optimization method.
template class providing a null value for a given type.
Constrained optimization problem.
const Array & currentValue() const
current value of the local minimum
Parameterized cost function.
virtual Array include(const Array &projectedParameters) const
returns whole set of parameters corresponding to the set
virtual Array project(const Array ¶meters) const
returns the subset of free parameters corresponding
void updateModelInstance()
EndCriteria::Type XABREndCriteria_
ext::shared_ptr< typename Model::type > modelInstance_
XABRCoeffHolder(const Time t, const Real &forward, const std::vector< Real > ¶ms, const std::vector< bool > ¶mIsFixed, std::vector< Real > addParams)
std::vector< bool > paramIsFixed_
std::vector< Real > addParams_
std::vector< Real > params_
std::vector< Real > weights_
virtual ~XABRCoeffHolder()=default
XABRInterpolationImpl * xabr_
Real value(const Array &x) const override
method to overload to compute the cost function value in x
XABRError(XABRInterpolationImpl *xabr)
Array values(const Array &x) const override
method to overload to compute the cost function values in x
ext::shared_ptr< EndCriteria > endCriteria_
Real interpolationSquaredError() const
Real value(Real x) const override
Real derivative(Real) const override
Real secondDerivative(Real) const override
XABRInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Time t, const Real &forward, const std::vector< Real > ¶ms, const std::vector< bool > ¶mIsFixed, bool vegaWeighted, ext::shared_ptr< EndCriteria > endCriteria, ext::shared_ptr< OptimizationMethod > optMethod, const Real errorAccept, const bool useMaxError, const Size maxGuesses, const std::vector< Real > &addParams=std::vector< Real >(), VolatilityType volatilityType=VolatilityType::ShiftedLognormal)
Real primitive(Real) const override
Real interpolationMaxError() const
Real interpolationError() const
Array interpolationErrors() const
ext::shared_ptr< OptimizationMethod > optMethod_
VolatilityType volatilityType_
Abstract constraint class.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Real Time
continuous quantity with 1-year units
std::size_t Size
size of a container
Halton low-discrepancy sequence generator.
base class for 1-D interpolations
Levenberg-Marquardt optimization method.
Abstract optimization method class.
Matrix inverse(const Matrix &m)
ext::shared_ptr< YieldTermStructure > r
Simplex optimization method.