gpt4 book ai didi

c++ - QuantLib:需要 Garch 帮助

转载 作者:行者123 更新时间:2023-11-28 05:03:14 28 4
gpt4 key购买 nike

我正在一个简单的 Windows 命令提示应用程序中使用 QuantLib,但无法使 Garch 函数正常工作。

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

我想做的是通过一个方法传递一个价格 vector (最小 4,因为这是 garch 模型对象的最小值)并将该系列的波动率作为 vector 或 double 值返回,不要介意。

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

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

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

调试我的代码后,我发现使用 Greeks 传递的 Garch11 构造函数没有注册传递的相同值。这一个问题。

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

我遇到的另一个问题是我无法将 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;
}

提前致谢。

最佳答案

代码如

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

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

无效。如果你想委托(delegate)给另一个构造函数,在 C++11 和更高版本中你可以这样写

CGarch::CGarch() : CGarch(0.0, 0.1, 0.3)
{
_method = Method::Greeks;
}

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

关于c++ - QuantLib:需要 Garch 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424916/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com