Re: CMS Bonds in Python

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/CMS-Bonds-in-Python-tp458p461.html


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 list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users

--

The rule on staying alive as a forecaster is to give 'em a number or
give 'em a date, but never give 'em both at once.
-- Jane Bryant Quinn



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