簡體   English   中英

Quantlib中Nelson-Siegel屈服曲線的參數限制

[英]Parameter restrictions for Nelson-Siegel yield curve in quantlib

我在Python中使用QL並翻譯了部分示例文件http://quantlib.org/reference/_fitted_bond_curve_8cpp-example.html#_a25 ; 如何使屈服曲線與債券擬合,以使Nelson-Siegel收益率曲線適合一組給定的校准債券。

通常在執行這種非線性擬合時,結果強烈依賴於初始條件,並且存在目標函數的許多(經濟上無意義的)最小值。 這就是為什么對參數施加約束對成功至關重要的原因。 舉個例子,有時我得到負tau / lambda參數,我的收益率曲線也有所不同。

我沒有找到如何在NelsonSiegelFitting或FittedBondDiscountCurve類中指定這些參數約束。 我可以想象任何在QL中執行NS擬合的人都會遇到同樣的問題。

感謝Andres Hernandez的回答:

目前這是不可能的。 但是,擴展QL以允許它是非常容易的,但我認為它需要在c ++上完成。 所以即使你在python中使用QL,你能修改c ++代碼並導出一個新的綁定嗎? 如果是,那么您可以使用以下代碼,如果沒有,那么我可以將其檢查到代碼中,但是拉取請求需要一些時間才能被接受。 如果您可以觸摸代碼,您可以添加如下內容:

在nonlinearfittingmethods.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_;
    };

在nonlinearfittingmethods.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;
}

然后,您需要將其添加到swig界面,但這樣做應該是微不足道的。

我沒有足夠的聲譽評論上面的答案,但我認為接受的答案可能會略有改善。 使用此代碼,參數受lower_[n]+upper_[n]約束。 但是,它們應該僅限於upper_[n] 為此,可以使用以下代碼:

    Real kappa = lower_[3] + (upper_[3]-lower_[3])/(1.0+exp(-x[3]));
Real x0 = lower_[0] + (upper_[0]-lower_[0])/(1.0+exp(-x[0])),
        x1 = lower_[1] + (upper_[1]-lower_[1])/(1.0+exp(-x[1])),
        x2 = lower_[2] + (upper_[2]-lower_[2])/(1.0+exp(-x[2])),;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM