Hi,
Glenn, did you finish your translation of fitted bond curve from C++ to Python in QuantLib? I've got a problem with my own translation. So, I'm really interested by your code if possible. Also, I would like to understand where my mistake is. Now, I don't find any solutions except making a c++ function that creates my FittedBondDiscountCurve from simple parameters. I guess it is not a clean solution. Firstly, I wrote a swig file to call FittedBondDiscountCurve from Python. Secondly, I called it from a python script. Finally, this prototype is not recognized. Someone has some tips to find mistakes with Swig (like swig option to activate…)?
#ifndef quantlib_fitted_bond_i #define quantlib_fitted_bond_i
%include termstructures.i %include ratehelpers.i
%{ using QuantLib::FittedBondDiscountCurve; using QuantLib::FittedBondDiscountCurve::FittingMethod; typedef boost::shared_ptr<FittedBondDiscountCurve> FittedBondDiscountCurvePtr; %}
%rename(FittedBondDiscountCurve) FittedBondDiscountCurvePtr; class FittedBondDiscountCurvePtr : public boost::shared_ptr<YieldTermStructure> { public: %extend { FittedBondDiscountCurvePtr(Natural settlementDays, const Calendar& calendar, const std::vector<boost::shared_ptr<FixedRateBondHelper> >& instruments, const DayCounter& dayCounter, const FittingMethod& fittingMethod, Real accuracy = 1.0e-10, Size maxEvaluations = 10000, const Array& guess = Array(), Real simplexLambda = 1.0) { return new FittedBondDiscountCurvePtr( new FittedBondDiscountCurve(settlementDays, calendar, instruments, dayCounter, fittingMethod, accuracy, maxEvaluations, guess, simplexLambda)); } FittedBondDiscountCurvePtr(const Date &referenceDate, const std::vector<boost::shared_ptr<FixedRateBondHelper> >& instruments, const DayCounter& dayCounter, const FittingMethod& fittingMethod, Real accuracy = 1.0e-10, Size maxEvaluations = 10000, const Array &guess = Array(), Real simplexLambda = 1.0) { return new FittedBondDiscountCurvePtr( new FittedBondDiscountCurve(referenceDate, instruments, dayCounter, fittingMethod, accuracy, maxEvaluations, guess, simplexLambda)); } } };
%{ using QuantLib::ExponentialSplinesFitting; using QuantLib::NelsonSiegelFitting; using QuantLib::CubicBSplinesFitting; %}
class ExponentialSplinesFitting : public QuantLib::FittedBondDiscountCurve::FittingMethod { public: %extend { ExponentialSplinesFitting(bool constrainAtZero = true) { return new ExponentialSplinesFitting(constrainAtZero); } } };
class NelsonSiegelFitting : public QuantLib::FittedBondDiscountCurve::FittingMethod { public: %extend { NelsonSiegelFitting() { return new NelsonSiegelFitting(); } } };
class CubicBSplinesFitting : public QuantLib::FittedBondDiscountCurve::FittingMethod { public: %extend { CubicBSplinesFitting(const std::vector<Time>& knotVector, bool constrainAtZero = true) { return new CubicBSplinesFitting(knotVector, constrainAtZero); } } };
#endif
I call if from:
print type(instruments[0]) print type(self.today_ql), type(instruments), type(dc), type(exponentialSplines)
ts = FittedBondDiscountCurve(self.today_ql, instruments, dc, exponentialSplines) And I get:
<class 'QuantLib.QuantLib.FixedRateBondHelper'> <class 'QuantLib.QuantLib.Date'> <type 'list'> <class 'QuantLib.QuantLib.SimpleDayCounter'> <class 'QuantLib.QuantLib.ExponentialSplinesFitting'> [...] NotImplementedError: Wrong number of arguments for overloaded function 'new_FittedBondDiscountCurve'. Possible C/C++ prototypes are: FittedBondDiscountCurvePtr(Date const &,std::vector< boost::shared_ptr< FixedRateBondHelper >,std::allocator< boost::shared_ptr< FixedRateBondHelper > > > const &,DayCounter const &,FittingMethod const &)
Thank you Frédéric
-----Original
Message-----
On Sun, 2008-04-20 at 20:25 -0500, glenn andrews wrote: > I am trying to convert the example below from C++ to Python using SWIG. > I am stuck on how to convert some of the lines such as the following to > Python: > > 1) std::vector< boost::shared_ptr<SimpleQuote> > quote; > > 2) RelinkableHandle<Quote> quoteHandle[numberOfBonds] > > 3) Real coupons[] = { 0.0200, 0.0225, 0.0250, 0.0275, 0.0300, > 0.0325, 0.0350, 0.0375, 0.0400, 0.0425, > 0.0450, 0.0475, 0.0500, 0.0525, 0.0550 };
In each case, you can use Python lists. For instance, the third case would be:
coupons = [ 0.0200, 0.0225, 0.0250, 0.0275, 0.0300, 0.0325, 0.0350, 0.0375, 0.0400, 0.0425, 0.0450, 0.0475, 0.0500, 0.0525, 0.055 ]
In the first two cases, you don't need separate initialization. You can create the list using list comprehension; for example, 1) would be:
quote = [ SimpleQuote(cleanPrice[i]) for i in range(len(cleanPrice)) ]
or better yet
quote = [ SimpleQuote(p) for p in cleanPrice ]
In general: - shared_ptr is hidden in the SWIG interfaces, so you can just omit it and use the pointed class directly; - for vectors, use Python lists.
Luigi
--
Weiler's Law: Nothing is impossible for the man who doesn't have to do it himself.
------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ QuantLib-users mailing list https://lists.sourceforge.net/lists/listinfo/quantlib-users
------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
On Wed, 2008-07-16 at 09:44 +0200, =?ISO-8859-1?Q? Fr=E9d=E9ric_Degraeve
_ wrote: > Also, I would like to understand where my mistake is. Now, I don't > find any solutions except making a c++ function that creates my > FittedBondDiscountCurve from simple parameters. I guess it is not a > clean solution. > > Firstly, I wrote a swig file to call FittedBondDiscountCurve from > Python. Secondly, I called it from a python script. Finally, this > prototype is not recognized. Try fully qualifying QuantLib::FittedBondDiscountCurve::FittingMethod in the constructor. If that fails, try exporting QuantLib::FittedBondDiscountCurve::FittingMethod trough SWIG. Luigi -- No, I'm not interested in developing a powerful brain. All I'm after is just a mediocre brain, something like the president of American Telephone and Telegraph Company. -- Alan Turing on the possibilities of a thinking machine, 1943. ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |