Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python quantlib中Nelson-Siegel屈服曲线的参数限制_Python_Quantlib - Fatal编程技术网

Python quantlib中Nelson-Siegel屈服曲线的参数限制

Python quantlib中Nelson-Siegel屈服曲线的参数限制,python,quantlib,Python,Quantlib,我在Python中使用QL,并翻译了示例文件的部分内容 ; 如何用债券拟合收益率曲线以拟合Nelson Siegel 一组给定校准键的收益率曲线 与通常一样,当执行这种非线性拟合时,结果强烈依赖于 关于系统的初始条件和许多(经济上无意义的)极小值 目标函数存在。这就是为什么要对参数施加约束 这是成功的关键。举个例子,有时候我会觉得消极 tau/lambda参数和我的收益率曲线发散 我没有找到如何在中指定这些参数约束 NelsonSiegelFitting或FittedBondDiscountCu

我在Python中使用QL,并翻译了示例文件的部分内容 ; 如何用债券拟合收益率曲线以拟合Nelson Siegel 一组给定校准键的收益率曲线

与通常一样,当执行这种非线性拟合时,结果强烈依赖于 关于系统的初始条件和许多(经济上无意义的)极小值 目标函数存在。这就是为什么要对参数施加约束 这是成功的关键。举个例子,有时候我会觉得消极 tau/lambda参数和我的收益率曲线发散

我没有找到如何在中指定这些参数约束 NelsonSiegelFitting或FittedBondDiscountCurve类。我可以 想象一下,任何在QL中执行NS拟合的人都会遇到同样的问题
问题。

感谢安德烈斯·埃尔南德斯的回答:

目前这是不可能的。但是,扩展QL非常容易,但我认为它需要在C++上完成。因此,即使您在Python中使用QL,您是否可以修改C++代码并导出新绑定?如果是,那么您可以使用以下代码,如果不是,那么我可以将其签入代码中,但是接受拉请求需要一些时间。如果您可以触摸代码,您可以添加如下内容:

在非线性拟合方法.hpp中:

  class NelsonSiegelConstrainedFitting
        : public FittedBondDiscountCurve::FittingMethod {
      public:
        NelsonSiegelConstrainedFitting(const Array& lower, const Array& upper,
                           const Array& weights = Array(),
                            boost::shared_ptr<OptimizationMethod> optimizationMethod
                                          = boost::shared_ptr<OptimizationMethod>());
        std::auto_ptr<FittedBondDiscountCurve::FittingMethod> clone() const;
      private:
        Size size() const;
        DiscountFactor discountFunction(const Array& x, Time t) const;
        Array lower_, upper_;
    };
class NelsonSiegelConstrainedFitting
:public FittedBondDiscountCurve::FittingMethod{
公众:
NelsonSiegelConstrainedFitting(常数阵列和下部、常数阵列和上部、,
常量数组&权重=数组(),
boost::shared_ptr优化方法
=boost::shared_ptr());
std::auto_ptr clone()常量;
私人:
大小()常数;
折扣因子折扣函数(常量数组&x,时间t)常量;
数组下uu,上u;
};
在非线性拟合方法.cpp中:

NelsonSiegelConstrainedFitting::NelsonSiegelConstrainedFitting(
                                         const Array& lower, const Array& upper, const Array& weights,
                                         boost::shared_ptr<OptimizationMethod> optimizationMethod)
: FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod),
  lower_(lower), upper_(upper){
    QL_REQUIRE(lower_.size() == 4, "Lower constraint must have 4 elements");
    QL_REQUIRE(upper_.size() == 4, "Lower constraint must have 4 elements");
}
std::auto_ptr<FittedBondDiscountCurve::FittingMethod>
NelsonSiegelConstrainedFitting::clone() const {
    return std::auto_ptr<FittedBondDiscountCurve::FittingMethod>(
                                          new NelsonSiegelFitting(*this));
}
Size NelsonSiegelConstrainedFitting::size() const {
    return 4;
}
DiscountFactor NelsonSiegelConstrainedFitting::discountFunction(const Array& x,
                                                     Time t) const {
    ///extreme values of kappa result in colinear behaviour of x[1] and x[2], so it should be constrained not only
    ///to be positive, but also not very extreme
    Real kappa = lower_[3] + upper_[3]/(1.0+exp(-x[3]));
    Real x0 = lower_[0] + upper_[0]/(1.0+exp(-x[0])),
            x1 = lower_[1] + upper_[1]/(1.0+exp(-x[1])),
            x2 = lower_[2] + upper_[2]/(1.0+exp(-x[2])),;
    Real zeroRate = x0 + (x1 + x2)*
                        (1.0 - std::exp(-kappa*t))/
                        ((kappa+QL_EPSILON)*(t+QL_EPSILON)) -
                        x2*std::exp(-kappa*t);
    DiscountFactor d = std::exp(-zeroRate * t) ;
    return d;
}
NelsonSiegelConstrainedFitting::NelsonSiegelConstrainedFitting(
常量数组和下限、常量数组和上限、常量数组和权重,
boost::共享(ptr优化方法)
:FittedBondDiscountCurve::FittingMethod(true、权重、优化方法),
下(下)、上(上){
QL_REQUIRE(lower_u.size()==4,“lower约束必须有4个元素”);
QL_REQUIRE(upper_u.size()==4,“Lower constraint必须有4个元素”);
}
标准::自动测试
NelsonSiegelConstrainedFitting::clone()常量{
返回标准::自动检查(
新NelsonSiegelFitting(*本));
}
大小NelsonSiegelConstrainedFitting::Size()常量{
返回4;
}
DiscountFactor NelsonSiegelConstrainedFitting::discountFunction(常量数组和x,
时间t)常数{
///kappa的极值导致x[1]和x[2]的共线行为,因此它不仅应该受到约束
///要积极,但也不是很极端
实际kappa=下[3]+上[3]/(1.0+exp(-x[3]);
实数x0=下限值[0]+上限值[0]/(1.0+exp(-x[0]),
x1=下[1]+上[1]/(1.0+exp(-x[1]),
x2=下_u[2]+上_u[2]/(1.0+exp(-x[2]),;
实际零利率=x0+(x1+x2)*
(1.0-std::exp(-kappa*t))/
((kappa+QL_ε)*(t+QL_ε))-
x2*std::exp(-kappa*t);
折扣因子d=std::exp(-zeroRate*t);
返回d;
}
然后需要将其添加到swig接口,但这样做应该很简单