Error when using a Cubic interpolator for PiecewiseYieldCurve

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

Error when using a Cubic interpolator for PiecewiseYieldCurve

Robert Philipp
I'm just starting out with QuantlLib and started going through the examples . In an attempt to get smoother forwards, I would like to use a cubic (or log-cubic, or monotonic) interpolator instead of the log-linear interpolator. So I modified the example code "swapvaluation.cpp" by replacing

        boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
            new PiecewiseYieldCurve< Discount, LogLinear>(
                                       settlementDate, depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance));

with
        boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
            new PiecewiseYieldCurve< Discount, Cubic >(
                                       settlementDate, depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance));

I get the following error message:
        1st iteration: failed at 10th instrument, maturity December 20th, 2006, reference date September 22nd, 2004:
        root not bracketed: f[2.22045e-016,0.932437] -> [-3.323214e+018,-3.911669e+000]

The full output is:
        Today: Monday, September 20th, 2004
        Settlement date: Wednesday, September 22nd, 2004
        ====================================================================
        5-year market swap-rate = 4.43 %
        ====================================================================
                5-years swap paying 4.00 %
        term structure | net present value | fair spread | fair fixed rate |
        --------------------------------------------------------------------
             depo-swap |          19065.88 |     -0.42 % |          4.43 % |
        1st iteration: failed at 10th instrument, maturity December 20th, 2006, reference date September 22nd, 2004:
        root not bracketed: f[2.22045e-016,0.932437] -> [-3.323214e+018,-3.911669e+000]


Is there something additional I must do when using an interpolator other than LogLinear?

Thanks,
-- 
Robert Philipp
Synapse Financial Engineering
703.623.4063 (mobile)
703.573.0119 (fax)

[hidden email]
www.synapsefe.com

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

Kim Kuen Tang
Robert Philipp schrieb:

> I'm just starting out with QuantlLib and started going through the
> examples . In an attempt to get smoother forwards, I would like to use
> a cubic (or log-cubic, or monotonic) interpolator instead of the
> log-linear interpolator. So I modified the example code
> "swapvaluation.cpp" by replacing
>
>         boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
>             new PiecewiseYieldCurve< Discount, *LogLinear*>(
>                                        settlementDate,
> depoFutSwapInstruments,
>                                        termStructureDayCounter,
>                                        tolerance));
>
> with
>         boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
>             new PiecewiseYieldCurve< Discount, *Cubic *>(
>                                        settlementDate,
> depoFutSwapInstruments,
>                                        termStructureDayCounter,
>                                        tolerance));
>
> I get the following error message:
>         1st iteration: failed at 10th instrument, maturity December
> 20th, 2006, reference date September 22nd, 2004:
>         root not bracketed: f[2.22045e-016,0.932437] ->
> [-3.323214e+018,-3.911669e+000]
This can be a design or implementation issue. solver1d.hpp  uses
assertions to detect ill-posed problems and throws an error if the code
is being misused. Take a look at lines 195 :

            QL_REQUIRE(fxMin_*fxMax_ < 0.0,
                       "root not bracketed: f["
                       << xMin_ << "," << xMax_ << "] -> ["
                       << std::scientific
                       << fxMin_ << "," << fxMax_ << "]");

For testing you can comment it out, and calculate the results. It might
be that the assertion is being too restrictive.
Do you mind to supply the data that you want to bootstrapp? (
Instrument, Maturity,value,...)
HTH
-Kim

>
> The full output is:
>         Today: Monday, September 20th, 2004
>         Settlement date: Wednesday, September 22nd, 2004
>        
> ====================================================================
>         5-year market swap-rate = 4.43 %
>        
> ====================================================================
>                 5-years swap paying 4.00 %
>         term structure | net present value | fair spread | fair fixed
> rate |
>        
> --------------------------------------------------------------------
>              depo-swap |          19065.88 |     -0.42 % |          
> 4.43 % |
>         1st iteration: failed at 10th instrument, maturity December
> 20th, 2006, reference date September 22nd, 2004:
>         root not bracketed: f[2.22045e-016,0.932437] ->
> [-3.323214e+018,-3.911669e+000]
>
> Is there something additional I must do when using an interpolator
> other than LogLinear?
>
> Thanks,
> --
> Robert Philipp
> Synapse Financial Engineering
> 703.623.4063 (mobile)
> 703.573.0119 (fax)
>
> [hidden email]
> www.synapsefe.com
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------------
> The Palm PDK Hot Apps Program offers developers who use the
> Plug-In Development Kit to bring their C/C++ apps to Palm for a share
> of $1 Million in cash or HP Products. Visit us here for more details:
> http://p.sf.net/sfu/dev2dev-palm
> ------------------------------------------------------------------------
>
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>  


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

MonkeyMan-3
The data the poster uses is the data in the example code.

I am struggling with the same issue. Other people have suggested looking at the
fitted bond curve example, but that is somewhat different. I just want to calc
forward swap rates. Commenting out the check seems a little dangerous. Any help
with this would be appreciated. The documentation is a little thin.

If it helps, I am messing around with different cubic methods. Here's how you
spec the interpolation with a little more control. Check the documentation here:

http://quantlib.org/reference/class_quant_lib_1_1_cubic_interpolation.html

Cubic cube(CubicInterpolation::FritschButland, true,
CubicInterpolation::Lagrange, 0,CubicInterpolation::Lagrange, 0);

        boost::shared_ptr<YieldTermStructure> termStructure(
             new PiecewiseYieldCurve<Discount,Cubic>(
                                       settlementDate,
                                       depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance,
                                       cube));



------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

MonkeyMan-3
MonkeyMan <feersum_monkey <at> yahoo.com> writes:

For what it's worth, here is the data I'm using (not the example data):

Today: Sunday, January 4th, 2004
Settlement date: Wednesday, January 7th, 2004
deposit: sw 0.0108375
deposit: 2w 0.0109125
deposit: 1m 0.0112
deposit: 2m 0.0113
deposit: 3m 0.0115
deposit: 6m 0.012275
deposit: 9m 0.0135
deposit: 1y 0.0151
future: 2004-03-17 98.765
future: 2004-06-16 98.525
future: 2004-09-15 98.145
future: 2004-12-15 97.695
future: 2005-03-16 97.26
future: 2005-06-15 96.855
future: 2005-09-21 96.51
future: 2005-12-21 96.21
swap: 10y 4.758
swap: 12y 4.973
swap: 15y 5.21
swap: 2y 2.245
swap: 20y 5.405
swap: 3y 2.875
swap: 30y 5.484
swap: 4y 3.36
swap: 5y 3.755
swap: 6y 4.045
swap: 7y 4.268
swap: 8y 4.46
swap: 9y 4.622

Then I blow up on the first instrument when I try to price a
1yf2y swap with this error:

1st iteration: failed at 1st instrument, maturity January 14th, 2004,
reference date January 7th, 2004: root not bracketed:
f[2.22045e-16,1] ->[1.083750e-02,1.083750e-02]

For this instance I used the ConvexMonotone:

Real quadraticity = 0.3;
Real monotonicity = 0.7;
bool forcePositive = true;
ConvexMonotone monotone = ConvexMonotone(
   quadraticity,
   monotonicity,
   forcePositive);

boost::shared_ptr<YieldTermStructure> termStructure(
             new PiecewiseYieldCurve<Discount,ConvexMonotone>(
                                       settlementDate,
                                       depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance,
                                       monotone));

Note that my swap rates are pushed into the RateHelper out of order.
This is the order they come out of the database.
I assume that doesn't matter.

Is there some better way to do this? The parameters for the
ConvexMonotone are the defaults. I have tried using the Hagan
parameters, but that didn't help.

What do other people do to bootstrap their curves?
Am I missing something basic?



------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

MonkeyMan-3
In reply to this post by MonkeyMan-3
MonkeyMan <feersum_monkey <at> yahoo.com> writes:

MonkeyMan <feersum_monkey <at> yahoo.com> writes:

For what it's worth, here is the data I'm using (not the example data):

Today: Sunday, January 4th, 2004
Settlement date: Wednesday, January 7th, 2004
deposit: sw 0.0108375
deposit: 2w 0.0109125
deposit: 1m 0.0112
deposit: 2m 0.0113
deposit: 3m 0.0115
deposit: 6m 0.012275
deposit: 9m 0.0135
deposit: 1y 0.0151
future: 2004-03-17 98.765
future: 2004-06-16 98.525
future: 2004-09-15 98.145
future: 2004-12-15 97.695
future: 2005-03-16 97.26
future: 2005-06-15 96.855
future: 2005-09-21 96.51
future: 2005-12-21 96.21
swap: 10y 4.758
swap: 12y 4.973
swap: 15y 5.21
swap: 2y 2.245
swap: 20y 5.405
swap: 3y 2.875
swap: 30y 5.484
swap: 4y 3.36
swap: 5y 3.755
swap: 6y 4.045
swap: 7y 4.268
swap: 8y 4.46
swap: 9y 4.622

Then I blow up on the first instrument when I try to price a
1yf2y swap with this error:

1st iteration: failed at 1st instrument, maturity January 14th, 2004,
reference date January 7th, 2004: root not bracketed:
f[2.22045e-16,1] ->[1.083750e-02,1.083750e-02]

For this instance I used the ConvexMonotone:

Real quadraticity = 0.3;
Real monotonicity = 0.7;
bool forcePositive = true;
ConvexMonotone monotone = ConvexMonotone(
   quadraticity,
   monotonicity,
   forcePositive);

boost::shared_ptr<YieldTermStructure> termStructure(
             new PiecewiseYieldCurve<Discount,ConvexMonotone>(
                                       settlementDate,
                                       depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance,
                                       monotone));

Note that my swap rates are pushed into the RateHelper out of order.
This is the order they come out of the database.
I assume that doesn't matter.

Is there some better way to do this? The parameters for the
ConvexMonotone are the defaults. I have tried using the Hagan
parameters, but that didn't help.

What do other people do to bootstrap their curves?
Am I missing something basic?



------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

MikeD
In reply to this post by Robert Philipp
you need to pair the interpolation method correctly with the curve output...

if using discount factors:
MonotonicLogCubicNaturalSpline or LogLinear should work

if using zero rates:
try MonotonicCubicNaturalSpline or CubicNaturalSpline

the reason you are getting an error is because the bootstrapping for the entire curve cant be completed with the interpolation method you selected (cubic on discount factors).  even if it did work, the swap rates you would have received would have been off-market. 

On Thu, Jul 29, 2010 at 12:31 PM, Robert Philipp <[hidden email]> wrote:
I'm just starting out with QuantlLib and started going through the examples . In an attempt to get smoother forwards, I would like to use a cubic (or log-cubic, or monotonic) interpolator instead of the log-linear interpolator. So I modified the example code "swapvaluation.cpp" by replacing

        boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
            new PiecewiseYieldCurve< Discount, LogLinear>(
                                       settlementDate, depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance));

with
        boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(
            new PiecewiseYieldCurve< Discount, Cubic >(
                                       settlementDate, depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance));

I get the following error message:
        1st iteration: failed at 10th instrument, maturity December 20th, 2006, reference date September 22nd, 2004:
        root not bracketed: f[2.22045e-016,0.932437] -> [-3.323214e+018,-3.911669e+000]

The full output is:
        Today: Monday, September 20th, 2004
        Settlement date: Wednesday, September 22nd, 2004
        ====================================================================
        5-year market swap-rate = 4.43 %
        ====================================================================
                5-years swap paying 4.00 %
        term structure | net present value | fair spread | fair fixed rate |
        --------------------------------------------------------------------
             depo-swap |          19065.88 |     -0.42 % |          4.43 % |
        1st iteration: failed at 10th instrument, maturity December 20th, 2006, reference date September 22nd, 2004:
        root not bracketed: f[2.22045e-016,0.932437] -> [-3.323214e+018,-3.911669e+000]


Is there something additional I must do when using an interpolator other than LogLinear?

Thanks,
-- 
Robert Philipp
Synapse Financial Engineering
703.623.4063 (mobile)
703.573.0119 (fax)

[hidden email]
www.synapsefe.com

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Error when using a Cubic interpolator for PiecewiseYieldCurve

MonkeyMan-3
In reply to this post by Robert Philipp
Thanks for the advice. That seems to work for me for some test dates. Here is
the explicit call I used to create the MonotonicLogCubicNaturalSpline for anyone
else following this:

        //MonotonicLogCubicNaturalSpline
        LogCubic mlcns = LogCubic(
                                  CubicInterpolation::Spline,
                                  true,
                                  CubicInterpolation::SecondDerivative,
                                  0,
                                  CubicInterpolation::SecondDerivative,
                                  0)  ;

        boost::shared_ptr<YieldTermStructure> termStructure(
             new PiecewiseYieldCurve<Discount,LogCubic>(
                                       settlementDate,
                                       depoFutSwapInstruments,
                                       termStructureDayCounter,
                                       tolerance,
                                       mlcns));


Also, apologies for the multiple posts before. Didn't think it went through...

Monkey


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users