http://quantlib.414.s1.nabble.com/Implement-Bermudan-swaption-in-QuantLibXL-tp291p297.html
I'm having trouble with adding Quantlib's model.hpp to QuantLibXL. Here is what I have done so far.
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Added by Hyungseok 37243 on 2012/07/04
*/
#ifndef qla_model_hpp
#define qla_model_hpp
#include <oh/libraryobject.hpp>
#include <ql/types.hpp>
namespace QuantLib {
class CalibratedModel;
class Constraint;
class CalibrationHelper;
class EndCriteria;
class OptimizationMethod;
}
namespace QuantLibAddin {
class CalibrationHelper : public ObjectHandler::LibraryObject<QuantLib::CalibrationHelper> {
protected:
OH_LIB_CTOR(CalibrationHelper, QuantLib::CalibrationHelper);
};
class CalibratedModel :
public ObjectHandler::LibraryObject<QuantLib::CalibratedModel> {
public:
CalibratedModel(
const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
const QuantLib::Size nArguments,
bool permanent);
void calibrate(
const std::vector<boost::shared_ptr<QuantLib::CalibrationHelper> >& instrument,
QuantLib::OptimizationMethod &method,
const QuantLib::EndCriteria &endCriteria,
const QuantLib::Constraint &additionalConstraint,
const std::vector<QuantLib::Real>& weights);
};
OH_LIB_CLASS(OptimizationMethod, QuantLib::OptimizationMethod);
OH_LIB_CLASS(EndCriteria, QuantLib::EndCriteria);
OH_LIB_CLASS(Constraint, QuantLib::Constraint);
}
#endif
(2) Created model.cpp under \QuantLibAddin\qlo
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Added by Hyungseok 37243 on 2012/07/04
*/
#if defined(HAVE_CONFIG_H) // Dynamically created by configure
#include <qlo/config.hpp>
#endif
#include <qlo/model.hpp>
#include <ql/models/model.hpp>
namespace QuantLibAddin {
CalibratedModel::CalibratedModel(
const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
const QuantLib::Size nArguments,
bool permanent)
: ObjectHandler::LibraryObject<QuantLib::CalibratedModel>(properties, permanent) {
libraryObject_ = boost::shared_ptr<QuantLib::CalibratedModel>(new
QuantLib::CalibratedModel(nArguments));
}
void CalibratedModel::calibrate(
const std::vector<boost::shared_ptr<QuantLib::CalibrationHelper> >& instrument,
QuantLib::OptimizationMethod &method,
const QuantLib::EndCriteria &endCriteria,
const QuantLib::Constraint &additionalConstraint,
const std::vector<QuantLib::Real>& weights) {
libraryObject_->calibrate(instrument, method, endCriteria, additionalConstraint, weights);
}
}
(3) Added in types.xml
<DataType defaultSuperType='libraryClass'>QuantLib::CalibratedModel</DataType>
(4) Created model.xml under QuantLibAddin\gensrc\metadata\functions
<Category name='model'>
<description>functions to construct and use model objects.</description>
<displayName>Model</displayName>
<xlFunctionWizardCategory>QuantLib - Financial</xlFunctionWizardCategory>
<serializationIncludes>
<include>qlo/model.hpp</include>
<include>ql/models/model.hpp</include>
</serializationIncludes>
<addinIncludes>
<include>qlo/model.hpp</include>
<include>ql/models/model.hpp</include>
</addinIncludes>
<copyright>
Hyungseok Hahm, 37243
</copyright>
<Functions>
<Member name='qlCalibratedModelCalibrate' type='QuantLib::CalibratedModel' superType='libraryClass'>
<description>calibrate.</description>
<libraryFunction>calibrate</libraryFunction>
<SupportedPlatforms>
<SupportedPlatform name='Excel'/>
<!--SupportedPlatform name='Cpp'/-->
</SupportedPlatforms>
<ParameterList>
<Parameters>
<Parameter name='CalibrationHelper' default='""'>
<type>QuantLib::CalibrationHelper</type>
<tensorRank>vector</tensorRank>
<description>Calibration helper ID.</description>
</Parameter>
<Parameter name='OptimizationMethod' default='""'>
<type>QuantLib::OptimizationMethod</type>
<tensorRank>scalar</tensorRank>
<description>OptimizationMethod object ID.</description>
</Parameter>
<Parameter name='EndCriteria' default='""'>
<type>QuantLib::EndCriteria</type>
<tensorRank>scalar</tensorRank>
<description>EndCriteria object ID.</description>
</Parameter>
<Parameter name='Constraint' default='""'>
<type>QuantLib::Constraint</type>
<tensorRank>scalar</tensorRank>
<description>Leave it blank.</description>
</Parameter>
<Parameter name='Weights' default='""'>
<type>QuantLib::Real</type>
<tensorRank>vector</tensorRank>
<description>Leave it blank.</description>
</Parameter>
</Parameters>
</ParameterList>
<ReturnValue>
<type>bool</type>
<tensorRank>scalar</tensorRank>
</ReturnValue>
</Member>
</Functions>
</Category>
Now when I ran it, it auto-generated model.cpp under QuantLibXL\qlxl\functions
/*
Hyungseok Hahm, 37243
This file is part of QuantLib, a free-software/open-source library
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
// This file was generated automatically by gensrc.py. If you edit this file
// manually then your changes will be lost the next time gensrc runs.
// This source code file was generated from the following stub:
// d:/build_ql_1_1_0/QuantLibAddin/gensrc/stubs/stub.excel.includes
#include <qlo/qladdindefines.hpp>
#include <qlo/model.hpp>
#include <ql/models/model.hpp>
#include <ohxl/objecthandlerxl.hpp>
#include <ohxl/callingrange.hpp>
#include <qlxl/session.hpp>
#include <qlxl/conversions/all.hpp>
#define XLL_DEC DLLEXPORT
XLL_DEC short int *qlCalibratedModelCalibrate(
char *ObjectId,
OPER *CalibrationHelper,
OPER *OptimizationMethod,
OPER *EndCriteria,
OPER *Constraint,
OPER *Weights,
OPER *Trigger) {
// declare a shared pointer to the Function Call object
boost::shared_ptr<ObjectHandler::FunctionCall> functionCall;
try {
// instantiate the Function Call object
functionCall = boost::shared_ptr<ObjectHandler::FunctionCall>(
new ObjectHandler::FunctionCall("qlCalibratedModelCalibrate"));
ObjectHandler::validateRange(Trigger, "Trigger");
// initialize the session ID (if enabled)
SET_SESSION_ID
// convert input datatypes to C++ datatypes
std::vector<std::string> CalibrationHelperCpp =
ObjectHandler::operToVector<std::string>(*CalibrationHelper, "CalibrationHelper");
std::string OptimizationMethodCpp = ObjectHandler::convert2<std::string>(
ObjectHandler::ConvertOper(*OptimizationMethod), "OptimizationMethod", "");
std::string EndCriteriaCpp = ObjectHandler::convert2<std::string>(
ObjectHandler::ConvertOper(*EndCriteria), "EndCriteria", "");
std::string ConstraintCpp = ObjectHandler::convert2<std::string>(
ObjectHandler::ConvertOper(*Constraint), "Constraint", "");
std::vector<double> WeightsCpp =
ObjectHandler::operToVector<double>(*Weights, "Weights");
// convert input datatypes to QuantLib datatypes
std::vector<QuantLib::Real> WeightsLib =
ObjectHandler::operToVector<QuantLib::Real>(*Weights, "Weights");
// convert object IDs into library objects
OH_GET_REFERENCE(ObjectIdLibObjPtr, ObjectId,
QuantLibAddin::CalibratedModel, QuantLib::CalibratedModel)
std::vector<boost::shared_ptr<QuantLib::CalibrationHelper> > CalibrationHelperLibObjPtr =
ObjectHandler::getLibraryObjectVector<QuantLibAddin::CalibrationHelper, QuantLib::CalibrationHelper>(CalibrationHelperCpp);
OH_GET_REFERENCE_DEFAULT(OptimizationMethodLibObjPtr, OptimizationMethodCpp,
QuantLibAddin::OptimizationMethod, QuantLib::OptimizationMethod)
OH_GET_REFERENCE_DEFAULT(EndCriteriaLibObjPtr, EndCriteriaCpp,
QuantLibAddin::EndCriteria, QuantLib::EndCriteria)
OH_GET_REFERENCE_DEFAULT(ConstraintLibObjPtr, ConstraintCpp,
QuantLibAddin::Constraint, QuantLib::Constraint)
// invoke the member function
static short int returnValue;
returnValue = ObjectIdLibObjPtr->calibrate(
CalibrationHelperLibObjPtr,
OptimizationMethodLibObjPtr,
EndCriteriaLibObjPtr,
ConstraintLibObjPtr,
WeightsLib);
// convert and return the return value
return &returnValue;
} catch (const std::exception &e) {
ObjectHandler::RepositoryXL::instance().logError(e.what(), functionCall);
return 0;
} catch (...) {
ObjectHandler::RepositoryXL::instance().logError("unkown error type", functionCall);
return 0;
}
}
and came back with the error
4>d:\build_ql_1_1_0\quantlibxl\qlxl\functions\model.cpp(109) : error C2664: 'QuantLib::CalibratedModel::calibrate' : can't convert 'boost::shared_ptr<T>' to 'QuantLib::OptimizationMethod &'
4> with
4> [
4> T=QuantLib::OptimizationMethod
4> ]
Obviously, the problem is in model.cpp at QuantLibXL\qlxl\functions, there is a function call
OH_GET_REFERENCE_DEFAULT(OptimizationMethodLibObjPtr, OptimizationMethodCpp,
QuantLibAddin::OptimizationMethod, QuantLib::OptimizationMethod)
OH_GET_REFERENCE_DEFAULT(EndCriteriaLibObjPtr, EndCriteriaCpp,
QuantLibAddin::EndCriteria, QuantLib::EndCriteria)
OH_GET_REFERENCE_DEFAULT(ConstraintLibObjPtr, ConstraintCpp,
QuantLibAddin::Constraint, QuantLib::Constraint)
returnValue = ObjectIdLibObjPtr->calibrate(
CalibrationHelperLibObjPtr,
OptimizationMethodLibObjPtr,
EndCriteriaLibObjPtr,
ConstraintLibObjPtr,
WeightsLib);
But in fact, void CalibratedModel::calibrate is
void CalibratedModel::calibrate(
const std::vector<boost::shared_ptr<CalibrationHelper> >& instruments,
OptimizationMethod& method,
const EndCriteria& endCriteria,
const Constraint& additionalConstraint,
const std::vector<Real>& weights)
And there is mismatch in types between two functions.
So I need to create in model.cpp at QuantLibXL\qlxl\functions
OptimizationMethodLibObjPtr, EndCriteriaLibObjPtr, ConstraintLibObjPtr,
not in the type of boost::shared_ptr<T>, but as in QuantLib::OptimizationMethod&, EndCriteria&, Contraint&. The problem is that the following segment,
OH_GET_REFERENCE_DEFAULT(OptimizationMethodLibObjPtr, OptimizationMethodCpp,
QuantLibAddin::OptimizationMethod, QuantLib::OptimizationMethod)
OH_GET_REFERENCE_DEFAULT(EndCriteriaLibObjPtr, EndCriteriaCpp,
QuantLibAddin::EndCriteria, QuantLib::EndCriteria)
OH_GET_REFERENCE_DEFAULT(ConstraintLibObjPtr, ConstraintCpp,
QuantLibAddin::Constraint, QuantLib::Constraint)
is automatically generated, so I do not know how to control it. Any good ideas?
- Hyung
On Thu, Jul 5, 2012 at 10:27 PM, Hyung-Seok Hahm
<[hidden email]> wrote:
Hi Eric,
CalibrationHelper/SwaptionHelper classes have been successfully exposed and I'm playing around them in Excel.
At the moment, I've push-backed 10 by 7 swaption vols in SwaptionHelper and all but one work well - what I mean is that every swaption swaptionhelper has been created/returned fine in Excel along with relevant inputs such as swaption maturities, lengths, fixed/floating daycounters, termstructure and so on.
Only one swaption vol has failed in Excel, but I don't think there is anything wrong in Excel exposing because it generates the same error under QuantLib as well. It happened with the very last swaption vol, 10 year by 10 year and it looks like somehow the swaption maturity over-stretches the termstructure and comes back with an error because extrapolate option is turned off.
All in all, it looks good so far. I'll figure out the above error and move on
(2) ShortRateModel->calibrate.
Thanks.
threat landscape has changed and how IT managers can respond. Discussions
threats.