2017-07-31 8 views
-1

私は単純なWindowsコマンドプロンプトアプリケーションでQuantLibを使いこなしていますが、Garch関数の作業を行うことはできません。QuantLib:Garchヘルプが必要です

私はGarch11オブジェクトの使い方を理解していません。なぜこのプログラムが動作しないのかがわかります。私はそれを使う方法の例を見つけることができませんでした。ドキュメントも(IMO)あいまいです。私はそれを使用する方法についての助けや手がかりを感謝します。

私がしたいことは、価格のベクトル(これはgarchモデルオブジェクトの最小値であるため最小4)を渡し、この系列のボラティリティをベクトルまたは倍数として返すことです。気にしないでください。

私のオブジェクトを構成する方法は、ギリシア人とセルフ・オプティマイゼーションの2つです。どちらの方法も私のアプリケーションでテストされます。現在

、私は出力私は、コード例を実行するたびに異なる取得するコードを実行する場合:私のコードをデバッグした後

D:\Users\ypx7647\Documents\Visual Studio 2017\Projects\GarchTest\Release>GarchTest.exe 
iQuotes size = 4 
ts size = 4 
GarchByGreeks iTs size = 4 
_alpha = 0.000000 _beta = 0.000000 _omega = 0.000000 
oTs size = 4 
tsOut size = 4 
oGarch[0] = 121.182504 
oGarch[1] = 121.182504 
oGarch[2] = 121.182504 
oGarch[3] = 121.182504 
oGarch size = 4 
og size = 4 

D:\Users\ypx7647\Documents\Visual Studio 2017\Projects\GarchTest\Release>GarchTest.exe 
iQuotes size = 4 
ts size = 4 
GarchByGreeks iTs size = 4 
_alpha = 0.000000 _beta = 0.000000 _omega = 0.000000 
oTs size = 4 
tsOut size = 4 
oGarch[0] = 11003897096.575457 
oGarch[1] = 11000266346.069284 
oGarch[2] = 10995727907.936573 
oGarch[3] = 10998450970.816200 
oGarch size = 4 
og size = 4 

を、私はギリシャ人を使用してGarch11コンストラクタ通過は同じを登録しないことを確認渡される値。この1つの問題。

次に、返されるオブジェクトは、入力オブジェクトと同じ数の値を返します。私はこれを望まず、ボラティリティを返す必要があります。これは第2の問題です。

私が経験しているもう1つの問題は、EndCriteria引数のsize_tパラメータを変数として渡すことができず、これがアプリをクラッシュさせてしまうことです。私の知る限りでは、すべてのパラメータはsize_tパラメータとして設定されているため、変換もキャストもありません。

私のコードを動作させるには何が変わる必要がありますか(私はC++の専門家ではありません)?

ここで(デバッグラインを含む)私のコードだ:

GarchTest.cpp

#include "stdafx.h" 
#include "CGarch.h" 

int main() 
{ 
// CGarch* Garch = new CGarch(Method::SelfOptimisation); 
CGarch* Garch = new CGarch(); 

std::vector<double> p, og; 

p.push_back(121.230000); 
p.push_back(121.190000); 
p.push_back(121.140000); 
p.push_back(121.170000); 

Garch->GarchOnArray(p,og); 

std::cout << "og size = " << std::to_string(og.size()) << std::endl; 

delete Garch; 
return 0; 
} 

` CGarch.h

#include <ql/quantlib.hpp> 

#define _MD 

enum Method { 
    Greeks, 
    SelfOptimisation 
}; 

enum OptimizationMethodType { 
    simplex, 
    levenbergMarquardt, 
    levenbergMarquardt2, 
    conjugateGradient, 
    conjugateGradient_goldstein, 
    steepestDescent, 
    steepestDescent_goldstein, 
    bfgs, 
    bfgs_goldstein 
}; 

enum Model { 
    LevenbergMarquardt, 
    Simplex 
}; 

enum Mode { 
    MomentMatchingGuess, /*!< The initial guess is a moment 
          matching estimates for 
          mean(r2), acf(0), and acf(1). */ 
    GammaGuess,   /*!< The initial guess is an 
          estimate of gamma based on the 
          property: 
          acf(i+1) = gamma*acf(i) for i > 1. */ 
    BestOfTwo,    /*!< The best of the two above modes */ 
    DoubleOptimization  /*!< Double optimization */ 
}; 

class CGarch 
{ 
private: 
    int GarchByGreeks(const QuantLib::TimeSeries<QuantLib::Volatility> &iTs, QuantLib::TimeSeries<QuantLib::Volatility> &oTs); 
    int GarchByOptimisation(const QuantLib::TimeSeries<QuantLib::Volatility> &iTs, QuantLib::TimeSeries<QuantLib::Volatility> &oTs); 

protected: 
    int   _method; 
    int   _model; 
    int   _mode; 
    std::size_t _maxIterations; 
    std::size_t _maxStationaryStateInterations; 
    double  _rootEpsilon; 
    double  _functionEpsilon; 
    double  _gradientEpsilon; 
    double  _omega; 
    double  _beta; 
    double  _alpha; 

public: 
    CGarch(); 
    CGarch(Method pMethod); 
    CGarch(double pOmega, double pBeta, double pAlpha); 
    CGarch(Model pModel, Mode pMode, std::size_t pMaxIterations, std::size_t pMaxStationaryStateIterations, double pRootEpsilon, double pFunctionEpsilon, double pGradientNormEpsilon); 
    ~CGarch(); 
    int GarchOnArray(const std::vector<double> &iPrices, std::vector<double> &oGarch); 
}; 

CGarch.cpp

#include "stdafx.h" 
#include "CGarch.h" 
#include <vector> 
#include <ql/auto_link.hpp> 
#include <ql/models/volatility/garch.hpp> 
//#include <ql/math/optimization/simplex.hpp> 
#include <ql/math/optimization/levenbergmarquardt.hpp> 

using namespace QuantLib; 

CGarch::CGarch() 
{ 
    _method = Method::Greeks; 

    CGarch::CGarch(0.0, 0.1, 0.3); 
} 

CGarch::CGarch(Method pMethod) 
{ 
    _method = pMethod; 

    switch (pMethod) { 
    case Method::Greeks:   CGarch::CGarch(); break; 
    case Method::SelfOptimisation: CGarch::CGarch(Model::LevenbergMarquardt, Mode::BestOfTwo, 20, 3, 0.0, 0.0, 0.0); break; 
    } 
} 

CGarch::CGarch(double pOmega, double pBeta, double pAlpha) 
{ 
    _method = Method::Greeks; 
    _model = 0; 
    _mode = 0; 
    _maxIterations = 0; 
    _maxStationaryStateInterations = 0; 
    _rootEpsilon = 0.0; 
    _functionEpsilon = 0.0; 
    _gradientEpsilon = 0.0; 
    _omega = pOmega; 
    _beta = pBeta; 
    _alpha = pAlpha; 
} 

CGarch::CGarch(Model pModel, Mode pMode, std::size_t pMaxIterations, std::size_t pMaxStationaryStateIterations, double pRootEpsilon, double pFunctionEpsilon, double pGradientNormEpsilon) 
{ 
    _method = Method::SelfOptimisation; 
    _model = pModel; 
    _mode = pMode;; 
    _maxIterations = pMaxIterations; 
    _maxStationaryStateInterations = pMaxStationaryStateIterations; 
    _rootEpsilon = pRootEpsilon; 
    _functionEpsilon = pFunctionEpsilon; 
    _gradientEpsilon = pGradientNormEpsilon; 
    _omega = 0.0; 
    _beta = 0.0; 
    _alpha = 0.0; 

    std::cout << "SelfOptimisation _maxIterations = " << std::to_string((unsigned int)_maxIterations) << " _maxStationaryStateInterations = " << std::to_string((unsigned int)_maxStationaryStateInterations) << " _rootEpsilon = " << std::to_string(_rootEpsilon) << " _functionEpsilon = " << std::to_string(_functionEpsilon) << " _gradientEpsilon = " << std::to_string(_gradientEpsilon) << std::endl; 
} 

CGarch::~CGarch() 
{ 
} 

int CGarch::GarchByGreeks(const TimeSeries<Volatility> &iTs, TimeSeries<Volatility> &oTs) 
{ 
    std::cout << "GarchByGreeks iTs size = " << std::to_string(iTs.size()) << std::endl; 

    if (iTs.empty()) { 
//  QL_FAIL("ERROR: input array (iTs) is empty"); 
     return -1; 
    } 

    std::cout << "_alpha = " << std::to_string(_alpha) << " _beta = " << std::to_string(_beta) << " _omega = " << std::to_string(_omega) << std::endl; 

    Garch11* g11 = new Garch11(_alpha, _beta, _omega); 
// Garch11 g11(_alpha, _beta, _omega); 
    /* 
    if (g11 == NULL) { 
    QL_FAIL("FATAL; Unable to instantiate Garch11 object"); 
    return -1; 
    } 
    */ 
    g11->calibrate(iTs); 
    oTs = g11->calculate(iTs); 

    std::cout << "oTs size = " << std::to_string(oTs.size()) << std::endl; 

    delete g11; 

    return 0; 
} 

int CGarch::GarchByOptimisation(const TimeSeries<Volatility> &iTs, TimeSeries<Volatility> &oTs) 
{ 
    std::cout << "GarchByOptimisation iTs size = " << std::to_string(iTs.size()) << std::endl; 

    if (iTs.empty()) { 
     return -1; 
    } 

    std::cout << "GarchByOptimisation _maxIterations = " << std::to_string(_maxIterations) << " _maxStationaryStateInterations = " << std::to_string(_maxStationaryStateInterations) << " _rootEpsilon = " << std::to_string(_rootEpsilon) << " _functionEpsilon = " << std::to_string(_functionEpsilon) << " _gradientEpsilon = " << std::to_string(_gradientEpsilon) << std::endl; 

    Garch11* g11 = new Garch11(iTs, Garch11::MomentMatchingGuess); 

    if (g11 == nullptr) { 
     std::cout << "FATAL: Failed to create g11 object " << std::endl; 
     return -2; 
    } 

    std::cout << "Here 1" << std::endl; 

    QuantLib::LevenbergMarquardt om; 
    std::cout << "Here 2" << std::endl; 
    g11->calibrate(iTs, om, EndCriteria(200, 3, _rootEpsilon, _functionEpsilon, _gradientEpsilon)); 
    std::cout << "Here 3" << std::endl; 
    g11->calibrate(iTs); 
    std::cout << "Here 4" << std::endl; 
    oTs = g11->calculate(iTs); 

    std::cout << "Here 5" << std::endl; 

    std::cout << "alpha = " << std::to_string(g11->alpha()) << " beta = " << std::to_string(g11->beta()) << " omega = " << std::to_string(g11->omega()) << std::endl; 

    std::cout << "oTs size = " << std::to_string(oTs.size()) << std::endl; 

    delete g11; 

    return 0; 
} 

int CGarch::GarchOnArray(const std::vector<double> &iQuotes, std::vector<double> &oGarch) 
{ 
    oGarch.clear(); 

    std::cout << "iQuotes size = " << std::to_string(iQuotes.size()) << std::endl; 

    if (iQuotes.empty()) { 
//  QL_FAIL("ERROR: input array (ts) is empty"); 
     return -1; 
    } 

    if (iQuotes.size() < 4) { 
//  QL_FAIL("ERROR: minimum (3) individual prices not present in ts array"); 
     return -2; 
    } 

    Date ds(7, July, 1962); 
    TimeSeries<Volatility> ts(ds, iQuotes.begin(), iQuotes.end()), tsOut; 

    std::cout << "ts size = " << std::to_string(ts.size()) << std::endl; 

    if (_method == Method::SelfOptimisation) 
    { 
     if (GarchByOptimisation(ts, tsOut) < 0) 
      return -3; 
    } 
    else { 
     if (GarchByGreeks(ts, tsOut) < 0) 
      return -4; 
    } 

// QL_ASSERT(false, "tsOut size = " + std::to_string(tsOut.size())); 
    std::cout << "tsOut size = " << std::to_string(tsOut.size()) << std::endl; 

// tsOut.find(ds + ts.size()); 
    oGarch = tsOut.values(); 

    for (int i = 0; i < oGarch.size(); i++) 
     std::cout << "oGarch[" << std::to_string(i) << "] = " << std::to_string(oGarch.at(i)) << std::endl; 

    std::cout << "oGarch size = " << std::to_string(oGarch.size()) << std::endl; 
// QL_ASSERT(false, "oGarch size = " + std::to_string(oGarch.size())); 

    return 0; 
} 

前もって感謝します。

+0

これらのタイプの一貫性のない動作は、通常、コード内に初期化されていない変数があることを指します。 – Frank

+0

デバッガを使用してコードをステップ実行し、結果が予想と異なる場所を見つけます。 – hnefatl

+0

@Frankはい、これは私の心を越えましたが、QuantLibコールのアプリケーションが正しいかどうかはわかりません(テストスイートコードとドキュメントに基づいています)初期化されていない。しかし基本的にはあなたはおそらく正しいでしょう。 – Andrew

答えて

0

コードなど

CGarch::CGarch() 
{ 
    _method = Method::Greeks; 

    CGarch::CGarch(0.0, 0.1, 0.3); 
} 

は有効ではありません。あなたがC++ 11には、別のコンストラクタに委任すると、後であなたが

代わり

CGarch::CGarch() : CGarch(0.0, 0.1, 0.3) 
{ 
    _method = Method::Greeks; 
} 
を書くことができますしたい場合は、私はあなたのバージョンが何を知っている(それがあなたのコンパイラは、それが意味することを決定したものに依存します)が、私はありません別の一時的なものを作成しているだけで、データメンバーが初期化されていないと考えられます。

+0

ありがとうございます。私はこれを知らなかった。しかし、あなたがQuantLib固有のコードについてコメントしていないので、私はそれが正しいと仮定しなければなりません。私の仮定は正しいのですか? – Andrew

+0

私はGarchを自分で使ったことが一度もありませんでしたので、あなたのコードを見る時間を見つけなければなりません。上記の修正を行うとどうなりますか? –