32#ifndef ql_xabr_interpolation_hpp
33#define ql_xabr_interpolation_hpp
56 const std::vector<Real>& params,
57 const std::vector<bool>& paramIsFixed,
58 std::vector<Real> addParams)
61 QL_REQUIRE(
t > 0.0,
"expiry time must be positive: " <<
t
63 QL_REQUIRE(params.size() == Model().dimension(),
64 "wrong number of parameters (" << params.size()
66 << Model().dimension());
67 QL_REQUIRE(paramIsFixed.size() == Model().dimension(),
68 "wrong number of fixed parameters flags ("
69 << paramIsFixed.size() <<
"), should be "
70 << Model().dimension());
72 for (
Size i = 0; i < params.size(); ++i) {
101template <
class I1,
class I2,
typename Model>
110 const std::vector<Real>& params,
111 const std::vector<bool>& paramIsFixed,
113 ext::shared_ptr<EndCriteria> endCriteria,
114 ext::shared_ptr<OptimizationMethod> optMethod,
115 const Real errorAccept,
116 const bool useMaxError,
117 const Size maxGuesses,
118 const std::vector<Real>& addParams = std::vector<Real>(),
127 optMethod_ = ext::shared_ptr<OptimizationMethod>(
133 60000, 100, 1e-8, 1e-8, 1e-8);
136 std::vector<Real>(xEnd - xBegin, 1.0 / (xEnd - xBegin));
151 Real weightsSum = 0.0;
152 for (; x != this->
xEnd_; ++x, ++
y) {
153 Real stdDev = std::sqrt((*
y) * (*
y) * this->
t_);
156 weightsSum += this->
weights_.back();
160 for (; w != this->
weights_.end(); ++w)
166 this->paramIsFixed_.end(),
true,
167 std::logical_and<>())) {
175 Array guess(Model().dimension());
176 for (
Size i = 0; i < guess.
size(); ++i)
180 Size freeParameters = 0;
182 Array bestParameters;
183 for (
Size i = 0; i < Model().dimension(); ++i)
188 Real tmpInterpolationError;
192 if (iterations > 0) {
195 this->
t_,
s.value, this->addParams_);
205 costFunction, inversedTransformatedGuess,
208 Array projectedGuess(
209 constrainedXABRError.
project(inversedTransformatedGuess));
212 Problem problem(constrainedXABRError, constraint,
217 constrainedXABRError.
include(projectedResult));
224 if (tmpInterpolationError < bestError) {
225 bestError = tmpInterpolationError;
226 bestParameters = result;
233 for (
Size i = 0; i < bestParameters.
size(); ++i)
234 this->
params_[i] = bestParameters[i];
249 Real error, totalError = 0.0;
253 for (; x != this->
xEnd_; ++x, ++
y, ++w) {
255 totalError += error * error * (*w);
267 for (; x != this->
xEnd_; ++x, ++
r, ++w, ++
y) {
268 *
r = (
value(*x) - *
y) * std::sqrt(*w);
276 return std::sqrt(
n * squaredError / (
n==1 ? 1 : (
n - 1)));
283 for (; i != this->
xEnd_; ++i, ++j) {
284 error = std::fabs(
value(*i) - *j);
285 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.