2016-08-17 1 views
2

私はPythonでQLを使用しており、サンプルファイルの一部を翻訳しています http://quantlib.org/reference/_fitted_bond_curve_8cpp-example.html#_a25; 札入れにイールドカーブを当てはめる方法について、Nelson-Siegel イールドカーブを一連の所与の校正ボンドに適合させるための方法を提供する。quantlibのNelson-Siegelイールドカーブのパラメータ制限

このような非線形当てはめを実行するとき、通常の結果は、初期条件および目的関数の多くの(経済的に意味のない)最小値に強く依存します。このため、パラメータの制約を に設定することが成功のために不可欠です。例を挙げると、私は時々負の値になります。 タウ/ラムダのパラメータとマイ・イールド・カーブが異なっています。

NelsonSiegelFittingクラスまたはFittedBondDiscountCurveクラスでこれらのパラメータ制約を指定する方法が見つかりませんでした。私は QLのNSのフィッティングを実行する誰もが同じ 問題に遭遇すると想像することができます。

答えて

2

アンデレス・ヘルナンデスのおかげで答え:

現在のところ、それはできません。しかし、QLを拡張するのは非常に簡単ですが、C++で行う必要があると思います。したがって、あなたがPythonでQLを使用していても、C++コードを変更して新しいバインディングをエクスポートできますか?はいの場合は、次のコードを使用できます。そうでない場合は、コードにチェックインできますが、プル要求が受け入れられるまでには時間がかかります。あなたは、その後にそれを追加する必要があり

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; 
} 

:nonlinearfittingmethods.hppで

:nonlinearfittingmethods.cppで

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_; 
    }; 

あなたは、コードに触れることができる場合、あなたはこのような何かを追加することができますswigインターフェイスを使用することはできますが、そうするのは簡単です。

関連する問題