📄 sabrinterpolation.hpp
字号:
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Marco Bianchetti
Copyright (C) 2007 Fran鏾is du Vignaud
Copyright (C) 2007 Giorgio Facchinetti
Copyright (C) 2006 Ferdinando Ametrano
Copyright (C) 2006 Mario Pucci
Copyright (C) 2006 StatPro Italia srl
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
<http://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.
*/
/*! \file sabrinterpolation.hpp
\brief SABR interpolation interpolation between discrete points
*/
#ifndef quantlib_sabr_interpolation_hpp
#define quantlib_sabr_interpolation_hpp
#include <ql/math/interpolation.hpp>
#include <ql/math/optimization/method.hpp>
#include <ql/math/optimization/simplex.hpp>
#include <ql/pricingengines/blackformula.hpp>
#include <ql/utilities/null.hpp>
#include <ql/utilities/dataformatters.hpp>
#include <ql/termstructures/volatilities/sabr.hpp>
#include <ql/math/optimization/projectedcostfunction.hpp>
namespace QuantLib {
namespace detail {
template <class I1, class I2> class SABRInterpolationImpl;
class SABRCoefficientHolder {
public:
SABRCoefficientHolder(Time t,
const Real& forward,
Real alpha,
Real beta,
Real nu,
Real rho,
bool alphaIsFixed,
bool betaIsFixed,
bool nuIsFixed,
bool rhoIsFixed)
: t_(t), forward_(forward),
alpha_(alpha), beta_(beta), nu_(nu), rho_(rho),
alphaIsFixed_(false),
betaIsFixed_(false),
nuIsFixed_(false),
rhoIsFixed_(false),
error_(Null<Real>()),
maxError_(Null<Real>()),
SABREndCriteria_(EndCriteria::None)
{
QL_REQUIRE(t>0.0, "expiry time must be positive: "
<< t << " not allowed");
if (alpha_ != Null<Real>())
alphaIsFixed_ = alphaIsFixed;
else alpha_ = std::sqrt(0.2);
if (beta_ != Null<Real>())
betaIsFixed_ = betaIsFixed;
else beta_ = 0.5;
if (nu_ != Null<Real>())
nuIsFixed_ = nuIsFixed;
else nu_ = std::sqrt(0.4);
if (rho_ != Null<Real>())
rhoIsFixed_ = rhoIsFixed;
else rho_ = 0.0;
validateSabrParameters(alpha_, beta_, nu_, rho_);
}
virtual ~SABRCoefficientHolder() {}
/*! Option expiry */
Real t_;
/*! */
const Real& forward_;
/*! Sabr parameters */
Real alpha_, beta_, nu_, rho_;
bool alphaIsFixed_, betaIsFixed_, nuIsFixed_, rhoIsFixed_;
Real error_, maxError_;
EndCriteria::Type SABREndCriteria_;
};
}
//! %SABR smile interpolation between discrete volatility points.
class SABRInterpolation : public Interpolation {
public:
template <class I1, class I2>
SABRInterpolation(const I1& xBegin, // x = strikes
const I1& xEnd,
const I2& yBegin, // y = volatilities
Time t, // option expiry
const Real& forward,
Real alpha,
Real beta,
Real nu,
Real rho,
bool alphaIsFixed,
bool betaIsFixed,
bool nuIsFixed,
bool rhoIsFixed,
bool vegaWeighted = false,
const boost::shared_ptr<EndCriteria>& endCriteria
= boost::shared_ptr<EndCriteria>(),
const boost::shared_ptr<OptimizationMethod>& method
= boost::shared_ptr<OptimizationMethod>()) {
impl_ = boost::shared_ptr<Interpolation::Impl>(new
detail::SABRInterpolationImpl<I1,I2>(xBegin, xEnd, yBegin,
t, forward,
alpha, beta, nu, rho,
alphaIsFixed, betaIsFixed,
nuIsFixed, rhoIsFixed,
vegaWeighted,
endCriteria,
method));
coeffs_ =
boost::dynamic_pointer_cast<detail::SABRCoefficientHolder>(
impl_);
}
Real expiry() const { return coeffs_->t_; }
Real forward() const { return coeffs_->forward_; }
Real alpha() const { return coeffs_->alpha_; }
Real beta() const { return coeffs_->beta_; }
Real nu() const { return coeffs_->nu_; }
Real rho() const { return coeffs_->rho_; }
Real interpolationError() const { return coeffs_->error_; }
Real interpolationMaxError() const { return coeffs_->maxError_; }
//const std::vector<Real>& interpolationWeights() const {
// return impl_->weights_; }
EndCriteria::Type endCriteria(){ return coeffs_->SABREndCriteria_; }
private:
boost::shared_ptr<detail::SABRCoefficientHolder> coeffs_;
};
//! %SABR interpolation factory
class SABR {
public:
SABR(Time t, Real forward,
Real alpha, Real beta, Real nu, Real rho,
bool alphaIsFixed, bool betaIsFixed,
bool nuIsFixed, bool rhoIsFixed,
bool vegaWeighted = false,
const boost::shared_ptr<EndCriteria> endCriteria
= boost::shared_ptr<EndCriteria>(),
const boost::shared_ptr<OptimizationMethod> method
= boost::shared_ptr<OptimizationMethod>())
: t_(t), forward_(forward),
alpha_(alpha), beta_(beta), nu_(nu), rho_(rho),
alphaIsFixed_(alphaIsFixed), betaIsFixed_(betaIsFixed),
nuIsFixed_(nuIsFixed), rhoIsFixed_(rhoIsFixed),
vegaWeighted_(vegaWeighted),
endCriteria_(endCriteria),
method_(method) {}
SABR() {};
template <class I1, class I2>
Interpolation interpolate(const I1& xBegin, const I1& xEnd,
const I2& yBegin) const {
return SABRInterpolation(xBegin, xEnd, yBegin,
t_, forward_,
alpha_, beta_, nu_, rho_,
alphaIsFixed_, betaIsFixed_,
nuIsFixed_, rhoIsFixed_,
vegaWeighted_, endCriteria_, method_);
}
enum { global = 1 };
private:
Time t_;
Real forward_;
Real alpha_, beta_, nu_, rho_;
bool alphaIsFixed_, betaIsFixed_, nuIsFixed_, rhoIsFixed_;
bool vegaWeighted_;
const boost::shared_ptr<EndCriteria> endCriteria_;
const boost::shared_ptr<OptimizationMethod> method_;
};
namespace detail {
template <class I1, class I2>
class SABRInterpolationImpl : public Interpolation::templateImpl<I1,I2>,
public SABRCoefficientHolder {
private:
class SabrParametersTransformation :
public ParametersTransformation {
mutable Array y_;
const Real eps1_, eps2_;
public:
SabrParametersTransformation() : y_(Array(4)),
eps1_(.0000001),
eps2_(.9999) {
}
Array direct(const Array& x) const {
y_[0] = x[0]*x[0] + eps1_;
//y_[1] = std::abs(eps2 * std::sin(x[1]));
y_[1] = std::exp(-(x[1]*x[1]));
y_[2] = x[2]*x[2] + eps1_;
y_[3] = eps2_ * std::sin(x[3]);
return y_;
}
Array inverse(const Array& x) const {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -