Implement Bermudan swaption in QuantLibXL

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Implement Bermudan swaption in QuantLibXL

Hyung-Seok Hahm
Never mind. I've added the following line in calibrationhelpers.xml

    <include>qlo/pricingengines.hpp</include>

and it compiles now.

- Hyung

On Thu, Aug 9, 2012 at 11:14 AM, Hyung-Seok Hahm <[hidden email]> wrote:
Eric or anybody who can help,

I was able to fix the previous problems and yet another comes up.

Now I'm implementing the following line in QuantLibXL

>>

boost::shared_ptr<HullWhite> modelHW(new HullWhite(rhTermStructure);
....
std::vector<boost::shared_ptr<CalibrationHelper>> swaptions;
.....
swaptions[i]->setPricingEngine(boost::shared_ptr<PricingEngine>(
    new JamshidianSwaptionEngine(modelHW)));

>>

I believe the key to this is to expose setPricingEngine function in CalibrationHelper class, which is not placed in the current version of QuantLibXL.

I've noticed that there is already a similar implementation in baseinstrument.*pp

//baseinstruments.hpp

#ifndef qla_baseinstruments_hpp
#define qla_baseinstruments_hpp

#include <oh/libraryobject.hpp>

namespace QuantLib {
    class Instrument;
}

namespace QuantLibAddin {
    
    class PricingEngine;
    
    //OH_LIB_CLASS(Instrument, QuantLib::Instrument);
    class Instrument : public ObjectHandler::LibraryObject<QuantLib::Instrument> {
      public:
        void setPricingEngine(boost::shared_ptr<PricingEngine>& e) const;
      protected:
        OH_LIB_CTOR(Instrument, QuantLib::Instrument)
    };

    OH_OBJ_CLASS(OneAssetOption, Instrument);

}

#endif

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//baseinstruments.cpp

#include <qlo/baseinstruments.hpp>
#include <qlo/pricingengines.hpp>
#include <ql/instrument.hpp>

namespace QuantLibAddin {

    void Instrument::setPricingEngine(boost::shared_ptr<PricingEngine>& engine) const {
        boost::shared_ptr<QuantLib::PricingEngine> ql_engine;
        engine->getLibraryObject(ql_engine);

        libraryObject_->setPricingEngine(ql_engine);
        
        boost::shared_ptr<ObjectHandler::ValueObject> inst_properties = properties();
        boost::shared_ptr<ObjectHandler::ValueObject> eng_properties = engine->properties();
        
        std::string engineId = eng_properties->objectId();
        inst_properties->setProperty("EngineID", engineId);
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Inspired by them, I've done the similar settings in calibrationhelpers.*pp

// calibrationhelpers.hpp

....
   class CalibrationHelper : public ObjectHandler::LibraryObject<QuantLib::CalibrationHelper> {
      public:
 // Imitate baseinstruments.hpp
void setPricingEngine(boost::shared_ptr<PricingEngine>& engine) const;
      protected:
// boost::shared_ptr<PricingEngine> engine_;
        OH_LIB_CTOR(CalibrationHelper, QuantLib::CalibrationHelper);
    };
...

// calibrationhelpers.cpp

,,,,

void CalibrationHelper::setPricingEngine(boost::shared_ptr<PricingEngine>& engine) const {
        boost::shared_ptr<QuantLib::PricingEngine> ql_engine;
        engine->getLibraryObject(ql_engine);

        libraryObject_->setPricingEngine(ql_engine);
        
        boost::shared_ptr<ObjectHandler::ValueObject> inst_properties = properties();
        boost::shared_ptr<ObjectHandler::ValueObject> eng_properties = engine->properties();
        
        std::string engineId = eng_properties->objectId();
        inst_properties->setProperty("EngineID", engineId);

}
...

which gives the following error

10>c:\boost\boost_1_44\boost\smart_ptr\shared_ptr.hpp(259) : error C2680: 'QuantLibAddin::PricingEngine *' : the corresponding type of dynamic_cast is wrong
10>        'QuantLibAddin::PricingEngine' : In order to use dynamic_cast, the class should be defined first
10>        c:\boost\boost_1_44\boost\smart_ptr\shared_ptr.hpp(522) : check the reference to  'boost::shared_ptr<T>::shared_ptr<ObjectHandler::Object>(const boost::shared_ptr<ObjectHandler::Object> &,boost::detail::dynamic_cast_tag)'
10>        with
10>        [
10>            T=QuantLibAddin::PricingEngine
10>        ]
10>        d:\build_ql_1_1_0\objecthandler\oh\repository.hpp(91) :  check the reference to'boost::shared_ptr<T> boost::dynamic_pointer_cast<T,ObjectHandler::Object>(const boost::shared_ptr<ObjectHandler::Object> &)'
10>        with
10>        [
10>            T=QuantLibAddin::PricingEngine
10>        ]
10>        d:\build_ql_1_1_0\quantlibxl\qlxl\functions\calibrationhelpers.cpp(72) :  check the reference to  'void ObjectHandler::Repository::retrieveObject<QuantLibAddin::PricingEngine>(boost::shared_ptr<T> &,const std::string &)'
10>        with
10>        [
10>            T=QuantLibAddin::PricingEngine
10>        ]
10>c:\boost\boost_1_44\boost\smart_ptr\shared_ptr.hpp(260) : fatal error C1903:

I guess that the range of dynamic_castable types should be declared somewhere in qlgensrc. Please let me know if you know how.

Thanks!

- Hyung


On Thu, Aug 2, 2012 at 9:53 AM, Hyung-Seok Hahm <[hidden email]> wrote:
Eric,

Yeop. Thanks to your advice, I've passed the obstacle and still moving forward. 

Now I'm stuck at implementing the following segment in BermudanSwaption.cpp

        boost::shared_ptr<HullWhite> modelHW(new HullWhite(rhTermStructure));
......
        calibrateModel(modelHW, swaptions);

And the function, calibrateModel is declared as 

void calibrateModel(
          const boost::shared_ptr<CalibratedModel>& model,
          const std::vector<boost::shared_ptr<CalibrationHelper> >& helpers)

Given the fact that the above example perfectly works, I know that HullWhite can be dynamically cast to CalibratedModel in QuantLib.

Now back to QuantLibXL, I've exposed HullWhite and CalibratedModel. However, when HullWhite is throwed into CalibratedModel, it generates 

qlCalibratedModelCalibrate - Error retrieving object with id 'obj_0000e#0000' - unable to convert reference to type 'class QuantLibAddin::CalibratedModel' found instead 'class QuantLibAddin::HullWhite'

Debugging indicates that the error originates in

\ObjectHandler\oh\repository.hpp

        //! Template member function to retrieve the Object with given ID.
        /*! Retrieve the object with the given ID and downcast it to the desired type.
            Throw an exception if no Object exists with that ID.
            This template passes the work off to function retrieveObjectImpl which
            may be overridden in derived classes.
        */
        template <class T>
        void retrieveObject(boost::shared_ptr<T> &ret,
                            const std::string &id) {
            boost::shared_ptr<Object> object = retrieveObjectImpl(id);
            ret = boost::dynamic_pointer_cast<T>(object);
            OH_REQUIRE(ret, "Error retrieving object with id '"
                << id << "' - unable to convert reference to type '"
                << typeid(T).name() << "' found instead '"
                << typeid(*object).name() << "'");
        }

So my guess is that in QuantLib, QuantLib::HullWhite is dynamically castable to QuantLib::CalibratedModel while in QuantLibAddin, QuantLibAddin::Hullwhite is yet to do so to QuantLibAddin::CalibratedModel.

Please let me know where and how I can set this. 

Thanks!

- Hyung





--

Hyungseok Hahm, PhD, FRM   

Quantitative Analyst 

Industrial Bank of Korea

Trading Department

Tel      : 82-2-729-7081

Fax       : 82-2-729-6084

E-mail  : [hidden email]

Yahoo Msn : hyungseok.hahm

Google Talk : hs.hahm.ibk[hidden email]

LinkedIn: http://kr.linkedin.com/pub/dir/Hyung-seok/Hahm



------------------------------------------------------------------------------
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
12