Re: CMS Bonds in Python

Posted by Lluis Pujol Bajador on
URL: http://quantlib.414.s1.nabble.com/CMS-Bonds-in-Python-tp458p462.html

Hi Luigi,

I believe that I've succesfully exposed to Python the pricer, and
GFunctionFactory and overloaded the setPricer.

In order to test it I am trying to port the cms test of the C++
testsuite. (probably too optimistic for me). I am currently stuck on the
SwaptionVolCube2 class.

I exported it like this:

%{
using QuantLib::SwaptionVolCube2;
typedef boost::shared_ptr<SwaptionVolatilityStructure>
     SwaptionVolCube2Ptr;
%}

%rename(SwaptionVolCube2) SwaptionVolCube2Ptr;
class SwaptionVolCube2Ptr
     : public boost::shared_ptr<SwaptionVolatilityStructure>  {
   public:
     %extend {
         SwaptionVolCube2Ptr(const Handle<SwaptionVolatilityStructure>&  atmVolStructure,
                             const std::vector<Period>&  optionTenors,
                             const std::vector<Period>&  swapTenors,
                             const std::vector<Spread>&  strikeSpreads,
                             const std::vector<std::vector<Handle<Quote>  >  >&  volSpreads,
                             const boost::shared_ptr<SwapIndex>&  swapIndexBase,
                             const boost::shared_ptr<SwapIndex>&  shortSwapIndexBase,
                             bool vegaWeightedSmileFit) {
             return new SwaptionVolCube2Ptr(
                 new SwaptionVolCube2(atmVolStructure,optionTenors,swapTenors,strikeSpreads,
                                        volSpreads, swapIndexBase, shortSwapIndexBase, vegaWeightedSmileFit));
         }
     }
};


It wraps and compiles without problem, but when I try to use it with a
Python list of lists of Handles, I get the following error complaining
about the vector of vectors of Handles

Traceback (most recent call last):
   File "D:\QuantLib\QuantLib-SWIG\Python\test\cms.py", line 114, in<module>
     vegaWeightedSmileFit)
   File "D:\Python26\Lib\site-packages\QuantLib\QuantLib.py", line 4432, in __init__
     this = _QuantLib.new_SwaptionVolCube2(*args)
TypeError: in method 'new_SwaptionVolCube2', argument 5 of type 'std::vector<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >,std::allocator<  std::vector<  Handle<  Quote>,std::allocator<  Handle<  Quote>  >  >  >  >  const&'


How can I create a vector of vectors of Handles in Python?.
Any workaround?.


Thanks in advance.

Lluís.



El 27/06/2011 17:23, Luigi Ballabio escribió:

> Hi Lluis,
> from the code you pasted, it looks like you forgot to put the the
> CmsCouponPricerPtr constructor inside %extend (you can copy how it's
> done in the BlackIborCouponPricerPtr interface.)
>
> However, I think you're making the wrong associations.  You're modeling
> (CmsCouponPricer, FloatingRateCouponPricer) after
> (BlackIborCouponPricer, IborCouponPricer).  Instead, CmsCouponPricer is
> at the same level as IborCouponPricer, as they're both inherited from
> FloatingRateCouponPricer; and in the role of BlackIborCouponPricer,
> you'll have one of the CMS pricers defined in conundrumpricer.hpp.
> Exporting CmsCouponPricer alone won't work, since it's an abstract
> class.  What I'd suggest is something like:
>
> %{
> using QuantLib::CmsCouponPricer;
> using QuantLib::AnalyticHaganPricer  // for instance
> typedef boost::shared_ptr<CmsCouponPricer>
> QuantLib::AnalyticHaganPricerPtr;
> %}
>
> %ignore QuantLib::CmsCouponPricer;
> class CmsCouponPricer {
>    public:
>      Handle<SwaptionVolatilityStructure>  swaptionVolatility() const;
>      void setSwaptionVolatility(
>          const Handle<SwaptionVolatilityStructure>&  v =
>              Handle<SwaptionVolatilityStructure>());
> };
>
> %template(CmsCouponPricer) boost::shared_ptr<CmsCouponPricer>;
>
> %rename(AnalyticHaganPricer) AnalyticHaganPricerPtr;
> class AnalyticHaganPricerPtr : public boost::shared_ptr<CmsCouponPricer>
> {
>    public:
>      %extend {
>          AnalyticHaganPricerPtr(
>              const Handle<SwaptionVolatilityStructure>&  v,
>              GFunctionFactory::YieldCurveModel model,
>              const Handle<Quote>&  meanReversion) {
>            return new AnalyticHaganPricerPtr(
>                new AnalyticHaganPricer(v, model, meanReversion));
>          }
>      }
> };
>
> You'll also have to export the GFunctionFactory class before
> AnalyticHaganPricer, and you'll need to overload setPricer so that it
> also take a CmsCouponPricer.  Write again if you get stuck, and I'll try
> to find some time and send a patch.
>
> Luigi
>
>
> On Thu, 2011-06-23 at 09:58 +0200, Lluis Pujol Bajador wrote:
>> Hello,
>>
>> I am trying to create CMS Bonds in Python and thus I need to create
>> the relate SWIG interface files.
>> As I am quite newbie to both SWIG and Quantlib, I am currently stuck
>> with the CMS pricer.
>>
>> I have succesfully been able to export (at least it builds without
>> complains) :
>>
>> 1) The CMSCoupon:
>>
>> using QuantLib::CmsCoupon;
>>
>> typedef boost::shared_ptr<CashFlow>  CmsCouponPtr;
>>
>> %rename(CmsCoupon) CmsCouponPtr;
>> class CmsCouponPtr : public FloatingRateCouponPtr {
>>        public:
>>      %extend {
>>          CmsCouponPtr(const Date&  paymentDate, Real nominal,
>>                    const Date&  startDate, const Date&  endDate,
>>                    Integer fixingDays, const
>> boost::shared_ptr<SwapIndex>&  index,
>>                    Real gearing = 1.0, Spread spread = 0.0,
>>                    const Date&  refPeriodStart = Date(),
>>                    const Date&  refPeriodEnd = Date(),
>>                    const DayCounter&  dayCounter = DayCounter(),
>>                    bool isInArrears = false) {
>>              const boost::shared_ptr<SwapIndex>  swi =
>>            boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsCouponPtr(
>>          new CmsCoupon(paymentDate,nominal,startDate,endDate,
>>                    fixingDays,swi,gearing,spread,
>>                    refPeriodStart, refPeriodEnd,
>> dayCounter,isInArrears));
>>          }
>>
>>       }
>>
>> };
>>
>> 2) The CMSBond:
>> using QuantLib::CmsRateBond;
>> typedef boost::shared_ptr<Instrument>  CmsRateBondPtr;
>> %rename(CmsRateBond) CmsRateBondPtr;
>> class CmsRateBondPtr : public BondPtr {
>>      %feature("kwargs") CmsRateBondPtr;
>>        public:
>>      %extend {
>>          CmsRateBondPtr(Size settlementDays,
>>                      Real faceAmount,
>>                      const Schedule&  schedule,
>>                      const SwapIndexPtr&  index,
>>                      const DayCounter&  paymentDayCounter,
>>                      BusinessDayConvention paymentConvention,
>>                      Natural fixingDays ,
>>                      const std::vector<Real>&  gearings ,
>>                      const std::vector<Spread>&  spreads ,
>>                      const std::vector<Rate>&  caps ,
>>                      const std::vector<Rate>&  floors,
>>                      bool inArrears = false,
>>                      Real redemption = 100.0,
>>                      const Date&  issueDate = Date()){
>>              boost::shared_ptr<SwapIndex>  swap =
>> boost::dynamic_pointer_cast<SwapIndex>(index);
>>              return new CmsRateBondPtr(
>>                  new CmsRateBond(settlementDays,
>>                                       faceAmount,
>>                                       schedule,
>>                                       swap,
>>                                       paymentDayCounter,
>>                                       paymentConvention,
>>                                       fixingDays,
>>                                       gearings,
>>                                       spreads,
>>                                       caps,
>>                                       floors,
>>                                       inArrears,
>>                                       redemption,
>>                                       issueDate));
>>          }
>>        }
>>      };
>>
>>
>> But when try to extend the current Pricers in (cashflows.i) as
>> follows: (I paste all the pricers altough I am just addind the CMS
>> pricer based on how the IborCouponPricer is defined).
>>
>> %{
>> using QuantLib::FloatingRateCouponPricer;
>> using QuantLib::IborCouponPricer;
>> using QuantLib::BlackIborCouponPricer;
>> using QuantLib::CmsCouponPricer;
>> typedef boost::shared_ptr<IborCouponPricer>  BlackIborCouponPricerPtr;
>> typedef boost::shared_ptr<FloatingRateCouponPricer>
>> CmsCouponPricerPtr;
>> %}
>>
>>
>> %ignore IborCouponPricer;
>> class IborCouponPricer {
>>    public:
>>      Handle<OptionletVolatilityStructure>  capletVolatility() const;
>>      void setCapletVolatility(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>());
>> };
>>
>> %template(IborCouponPricer) boost::shared_ptr<IborCouponPricer>;
>> %template(FloatingRateCouponPricer)
>> boost::shared_ptr<FloatingRateCouponPricer>;
>> void setCouponPricer(const Leg&, const
>> boost::shared_ptr<IborCouponPricer>&);
>>
>>
>> %rename(BlackIborCouponPricer) BlackIborCouponPricerPtr;
>> class BlackIborCouponPricerPtr : public
>> boost::shared_ptr<IborCouponPricer>  {
>>    public:
>>      %extend {
>>          BlackIborCouponPricerPtr(const
>> Handle<OptionletVolatilityStructure>&  v =
>>
>> Handle<OptionletVolatilityStructure>()) {
>>              return new BlackIborCouponPricerPtr(new
>> BlackIborCouponPricer(v));
>>          }
>>      }
>> };
>>
>> %rename(CmsCouponPricer) CmsCouponPricerPtr;
>> class CmsCouponPricerPtr : public
>> boost::shared_ptr<FloatingRateCouponPricer>{
>>        public:
>>         CmsCouponPricerPtr(const Handle<SwaptionVolatilityStructure>&  v
>> =
>>
>> Handle<SwaptionVolatilityStructure>()) {
>>              return new CmsCouponPricerPtr(new CmsCouponPricer(v));
>>
>>
>>         }
>> };
>>
>> I get the following error (translated to english).
>>
>> QuantLib/quantlib_wrap.cpp(127710) : error C2664:
>> 'boost::shared_ptr<T>::shared_
>> ptr(const boost::shared_ptr<T>  &)' : cannot convert parameter 1 from
>> 'co
>> nst QuantLib::Handle<T>' to 'const boost::shared_ptr<T>  &'
>>          with
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          and
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>          Reason: Cannot find the conversion from 'const
>> QuantLib::Handle<T>'
>>   to 'const boost::shared_ptr<T>'
>>          with
>>          [
>>              T=QuantLib::SwaptionVolatilityStructure
>>          ]
>>          and
>>          [
>>              T=QuantLib::FloatingRateCouponPricer
>>          ]
>>
>> It is not clear to me if I have also to export
>> FloatingRateCouponPricer...
>>
>> As I am quite lost with SWIG I don't know if someone could hint me a
>> way to solve this or have any guidance on how to create the SWIG
>> interface files for Quantlib.
>>
>> Thanks in advance for your help.
>>
>> Lluís
>> ------------------------------------------------------------------------------
>> Simplify data backup and recovery for your virtual environment with vRanger.
>> Installation's a snap, and flexible recovery options mean your data is safe,
>> secure and there when you need it. Data protection magic?
>> Nope - It's vRanger. Get your free trial download today.
>> http://p.sf.net/sfu/quest-sfdev2dev
>> _______________________________________________ QuantLib-users mailing [hidden email]  https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users