QuantLib:需要加奇的帮助

QuantLib: Garch help needed

本文关键字:帮助 QuantLib      更新时间:2023-10-16

我在一个简单的Windows命令提示符应用程序中玩QuantLib,无法让Garch函数工作。

我不确定我是否了解如何使用 Garch11 对象,这可能是我的程序不起作用的结果。我也找不到如何使用它的任何示例。文件也(IMO(含糊不清。我感谢有关如何使用它的任何帮助或线索。

我想做的是向一个方法传递价格向量(最小值 4,因为这是 garch 模型对象的最小值(,并将该序列的波动率作为向量或双精度返回,不要介意。

有两种方法可以构建我的对象,一种是通过希腊人,另一种是通过自我优化。这两种方法都将在我的应用程序中进行测试。

目前,当我运行代码时,每次运行代码时都会得到不同的输出,例如:

D:Usersypx7647DocumentsVisual Studio 2017ProjectsGarchTestRelease>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:Usersypx7647DocumentsVisual Studio 2017ProjectsGarchTestRelease>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 构造函数没有注册传递的相同值。这个问题。

接下来,返回的对象返回与输入对象相同数量的值。我不想要这个,只需要返回波动性。这是第二个问题。

我遇到的另一个问题是我无法将 EndCriteria 参数的size_t参数作为变量传递,这只会使应用程序崩溃。据我所知,所有参数都配置为size_t参数,因此没有转换或强制转换。

我需要更改什么才能使我的代码正常工作(我不是C++方面的专家(?

这是我的代码(包括调试行(:

加奇测试.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;
}

提前谢谢。

代码,例如

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

我不知道您的版本是做什么的(这取决于您的编译器决定它的含义(,但我怀疑它只是创建了一个单独的临时文件,并且您的数据成员没有初始化。