Re: Implement Bermudan swaption in QuantLibXL

Posted by Hyung-Seok Hahm on
URL: http://quantlib.414.s1.nabble.com/Implement-Bermudan-swaption-in-QuantLibXL-tp291p299.html

Hi Eric,

Welcome back. I know you're doing a favor to the whole community, so please don't be sorry. ;-)

I've looked into qlAbcdInterpolation, but there is a discrepancy in that and my case. 

For instance,   qlAbcdInterpolation eventually gets to class AbcdInterpolation in QuantLib which has a parameter of type,

const boost::shared_ptr<OptimizationMethod>& opMethod

Therefore setting boost::shared_ptr<QuantLib::OptimizationMethod> in QuantLibAddin/qlo/model.?pp will do the job.

However, in my case, the ql-interface function eventually reaches void CalibrateModel::calibrate in model.cpp in QuantLib which has a parameter of type,

OptimizationMethod& method,

Therefore somehow it has to be declared as such in QuantLibAddin/qlo/model.?pp. What I have tried is

1. Create QuantLibAddin/qlo/model.hpp

#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 {

        OH_LIB_CLASS(OptimizationMethod, QuantLib::OptimizationMethod);
OH_LIB_CLASS(EndCriteria, QuantLib::EndCriteria);
OH_LIB_CLASS(Constraint, QuantLib::Constraint);



    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& om,
            const QuantLib::EndCriteria &endCriteria,
     const QuantLib::Constraint &additionalConstraint,
       const std::vector<QuantLib::Real>& weights);


};

}

#endif

2. Create QuantLibAddin/qlo/model.cpp

#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& om,
                   const QuantLib::EndCriteria &endCriteria,
    const QuantLib::Constraint &additionalConstraint,
  const std::vector<QuantLib::Real>& weights) {

  libraryObject_->calibrate(instrument, om, endCriteria, additionalConstraint, weights);

}



}

3. model.xml

<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>
            <supertype> underlyingClass </supertype>
            <tensorRank>scalar</tensorRank>
            <description>OptimizationMethod object ID.</description>
          </Parameter>
          <Parameter name='EndCriteria' default='""'>
            <type>QuantLib::EndCriteria</type>
            <supertype> underlyingClass </supertype>
            <tensorRank>scalar</tensorRank>
            <description>EndCriteria object ID.</description>            
          </Parameter>
          <Parameter name='Constraint' default='""'>
            <type>QuantLib::Constraint</type>
            <supertype> underlyingClass </supertype>
            <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>

And yet I'm still getting an error, "error C2664: Can't convert QuantLib::CalibrateModel::calibrate : boost::shared_ptr<T> to QuantLib::OptimizationMethod &".

I believe this is due to the fact that in QuantLibAddin, OptimizationMethod is created as in boost::shared_ptr<QuantLib::OptimizationMethod>, not in QuantLib::OptimizationMethod &, which is what I want to do.

When I set superType to underlyingClass in Optimization in model.xml, I wanted it to be create in QuantLib::OptimizationMethod &, which obviously failed. 

Please let me know if you have any ideas.

Thanks.

- Hyung

On Wed, Jul 25, 2012 at 1:14 AM, Eric Ehlers <[hidden email]> wrote:
Hi Hyung,

Apologies for the delay in my response, I was away.

> is automatically generated, so I do not know how to control it. Any good
> ideas?

You need to change the types in QuantLibAddin/qlo/model.?pp.

For example, your parameter OptimizationMethod should be declared in
the above files with type
boost::shared_ptr<QuantLib::OptimizationMethod>.

Search the source code for existing examples.  For example function
qlAbcdInterpolation has a parameter of type
QuantLib::OptimizationMethod, you can copy that.

I think you might also need to change the name of the variable because
your current variable name "OptimizationMethod" is the same as the type.

Kind Regards,
Eric

===================================================
Eric Ehlers
nazcatech sprl | Brussels | http://www.nazcatech.be
* Distributed computing for pricing analytics
* Use Microsoft Excel as a client to the Grid


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users




------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users