Generalized Hull-White model problems

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

Generalized Hull-White model problems

Alexander Lotter
Hello Javit,

I found the Generalized Hull-White model checked by Luigi into the trunk a couple of months ago.

As you told me, the newest version was (see .zip file): http://old.nabble.com/Generalized-Hull-White-model-with-non-constant-parameters-td26287370.html#a26635940

The last version in trunk on sourceforge as far as I know 17226. This version have some differences to the version you posted in that archive in december 2009.

There is also no examples in the current trunk of the QuantLib, which contains your version of BermudanSwaptions or some code for the test-suite.

There is no a version of PiecewiseConstantParameter2 as you proposed to implement. So, without this change, I couldn't get the current trunk version to run without run-time error (vector subscript out of range). As I added this class, I got the current version to run, but calibration took me too long to wait.

To be simple:

trunk version (quick check http://quantlib.svn.sourceforge.net/viewvc/quantlib/trunk/QuantLib/ql/experimental/shortrate/)

generalizedhullwhite.cpp

...
a_ = PiecewiseConstantParameter(speedperiods_, PositiveConstraint());
...

your version in the zip file

a_ = PiecewiseConstantParameter2(speedperiods_, PositiveConstraint());

I reduced the TimeGrid from 30 to 1 as you proposed (see your BermudanSwaption example):

// Building time-grid
TimeGrid grid(times.begin(), times.end(), 1);

The calibration still take too long, never comes back.

I get stuck here on calibrateModel:

---------------------------------------
        std::cout << "Generalized Hull-White calibration" << std::endl;
        for (i=0; i<swaptions.size(); i++)
            swaptions[i]->setPricingEngine(boost::shared_ptr<PricingEngine>(
                                      new TreeSwaptionEngine(generalizedHWmodel, grid)));

                calibrateModel(generalizedHWmodel, swaptions);

---------------------------------------

I tried

 // Building time-grid
        TimeGrid grid(times.begin(), times.end(), 30);

as well as

 // Building time-grid
        TimeGrid grid(times.begin(), times.end(), 1);

Any suggestions?

I would like to assist on this issues and get the trunk version to work properly. We could also add the sample to the BermudanSwaption and test-suite as well.

Please correct me if I missed something.

Take care

Alex
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Luigi Ballabio
On Sun, 2010-05-23 at 01:27 -0700, Alexander Lotter wrote:

> Hello Javit,
>
> I found the Generalized Hull-White model checked by Luigi into the trunk a
> couple of months ago.
>
> As you told me, the newest version was (see .zip file):
> http://old.nabble.com/Generalized-Hull-White-model-with-non-constant-parameters-td26287370.html#a26635940
>
> The last version in trunk on sourceforge as far as I know 17226. This
> version have some differences to the version you posted in that archive in
> december 2009.

Yes. The differences are meant to be.

> There is also no examples in the current trunk of the QuantLib, which
> contains your version of BermudanSwaptions or some code for the test-suite.

True, for some reason the example didn't make into the commit.
I remember it working, though.

> There is no a version of PiecewiseConstantParameter2 as you proposed to
> implement. So, without this change, I couldn't get the current trunk version
> to run without run-time error (vector subscript out of range). As I added
> this class, I got the current version to run, but calibration took me too
> long to wait.

The PiecewiseConstantParameter class was modified in revision 17224 to
take an optional constraint, so that's taken care of; the code in the
trunk instantiates it with a PositiveConstraint.

Anyway, I'll try to get the example working and get back.

Luigi


--

Don't let school get in the way of your education.
-- Mark Twain



------------------------------------------------------------------------------

_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Alexander Lotter
Hello Luigi,

thanks for the quick reply.

>The PiecewiseConstantParameter class was modified in revision 17224 to
>take an optional constraint, so that's taken care of; the code in the
>trunk instantiates it with a PositiveConstraint.

I am not sure this is enough. This peace of code in QL causes problems:

----
 PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size()+1,
                    boost::shared_ptr<Parameter::Impl>(
                                 new PiecewiseConstantParameter::Impl(times)),
                    constraint)
        {}
----

should be

----
public:
        PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size(),
----                    

times.size() not times.size() + 1, in this case I get run time exception in the following functions

---

 boost::function<Real (Time)> GeneralizedHullWhite::speed() const {

                std::vector<Real> speedvals;
                speedvals.push_back(a_(0.0001));
                for (Size i=0;i<a_.size()-1;i++)
                        speedvals.push_back(
                        a_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(speedperiods_, speedvals);
        }

    boost::function<Real (Time)> GeneralizedHullWhite::vol() const {

                std::vector<Real> volvals;
                volvals.push_back(sigma_(0.0001));
                for (Size i=0;i<sigma_.size()-1;i++)
                        volvals.push_back(
                        sigma_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(volperiods_, volvals);
        }
---

vector subscript out of range

a_.size() - 1 and sigma_.size() - 1 are bigger than speedstructure_[i+1]
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Alexander Lotter
In reply to this post by Luigi Ballabio
After this changes I get the project run, but calibration never returns... Playing with TimeGrid didn't help.
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Javit Hafizoglu
In reply to this post by Alexander Lotter
Alexander,

I believe Luigi forgot to change the generalized HW code when he made the change in the parameter class to include the positive constraint. Your point for the "size()"  vs "size()+1" is valid.

I didn't notice anything because I still use the older version of GHW with quantlib 9.7.

Per the tests, I tested the GHW code and it worked when I submitted. I ran my tests by adding couple of lines into BermudanSwaption.cpp I used only two timepoints for the volatility and the reversion parameters. It takes significantly long if you include more time points. I would suggest both decreasing the timegrid and the timepoints (let me know if you don't understand what I meant here by timepoints) to decrease the optimization time. And once optimized, initiate your next search with the most recent model parameters.

Luigi, please let me know if you would like me to get involved also.

Thank you,
Javit


<quote author="Alexander Lotter">
Hello Luigi,

thanks for the quick reply.

>The PiecewiseConstantParameter class was modified in revision 17224 to
>take an optional constraint, so that's taken care of; the code in the
>trunk instantiates it with a PositiveConstraint.

I am not sure this is enough. This peace of code in QL causes problems:

----
 PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size()+1,
                    boost::shared_ptr<Parameter::Impl>(
                                 new PiecewiseConstantParameter::Impl(times)),
                    constraint)
        {}
----

should be

----
public:
        PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size(),
----                    

times.size() not times.size() + 1, in this case I get run time exception in the following functions

---

 boost::function<Real (Time)> GeneralizedHullWhite::speed() const {

                std::vector<Real> speedvals;
                speedvals.push_back(a_(0.0001));
                for (Size i=0;i<a_.size()-1;i++)
                        speedvals.push_back(
                        a_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(speedperiods_, speedvals);
        }

    boost::function<Real (Time)> GeneralizedHullWhite::vol() const {

                std::vector<Real> volvals;
                volvals.push_back(sigma_(0.0001));
                for (Size i=0;i<sigma_.size()-1;i++)
                        volvals.push_back(
                        sigma_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(volperiods_, volvals);
        }
---

vector subscript out of range

a_.size() - 1 and sigma_.size() - 1 are bigger than speedstructure_[i+1]

</quote>
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Alexander Lotter
Hello Javit,

I used this setting:

  TimeGrid grid(times.begin(), times.end(), 1);


        // defining the models
        boost::shared_ptr<G2> modelG2(new G2(rhTermStructure));
        boost::shared_ptr<HullWhite> modelHW(new HullWhite(rhTermStructure));
        boost::shared_ptr<HullWhite> modelHW2(new HullWhite(rhTermStructure));
        boost::shared_ptr<BlackKarasinski> modelBK(
                                        new BlackKarasinski(rhTermStructure));

                //------------------ GHW model ---------

                std::vector<Date> GHWdates;
                GHWdates.push_back(calendar.advance(settlementDate,0,Years));
                GHWdates.push_back(calendar.advance(settlementDate,12,Months,floatingLegConvention));
                GHWdates.push_back(calendar.advance(settlementDate,73,Months,floatingLegConvention));

                std::vector<Real> a(3, 0.04);
                std::vector<Real> sigma(3, 0.1);

                boost::shared_ptr<GeneralizedHullWhite> generalizedHWmodel(
                        new GeneralizedHullWhite(rhTermStructure, GHWdates, GHWdates, a, sigma));

Please correct me, if I've forgotten something.

Cheers

Alexander


<quote author="Javit Hafizoglu">
Alexander,

I believe Luigi forgot to change the generalized HW code when he made the change in the parameter class to include the positive constraint. Your point for the "size()"  vs "size()+1" is valid.

I didn't notice anything because I still use the older version of GHW with quantlib 9.7.

Per the tests, I tested the GHW code and it worked when I submitted. I ran my tests by adding couple of lines into BermudanSwaption.cpp I used only two timepoints for the volatility and the reversion parameters. It takes significantly long if you include more time points. I would suggest both decreasing the timegrid and the timepoints (let me know if you don't understand what I meant here by timepoints) to decrease the optimization time. And once optimized, initiate your next search with the most recent model parameters.

Luigi, please let me know if you would like me to get involved also.

Thank you,
Javit


Alexander Lotter wrote
Hello Luigi,

thanks for the quick reply.

>The PiecewiseConstantParameter class was modified in revision 17224 to
>take an optional constraint, so that's taken care of; the code in the
>trunk instantiates it with a PositiveConstraint.

I am not sure this is enough. This peace of code in QL causes problems:

----
 PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size()+1,
                    boost::shared_ptr<Parameter::Impl>(
                                 new PiecewiseConstantParameter::Impl(times)),
                    constraint)
        {}
----

should be

----
public:
        PiecewiseConstantParameter(const std::vector<Time>& times,
                                   const Constraint& constraint =
                                                             NoConstraint())
        : Parameter(times.size(),
----                    

times.size() not times.size() + 1, in this case I get run time exception in the following functions

---

 boost::function<Real (Time)> GeneralizedHullWhite::speed() const {

                std::vector<Real> speedvals;
                speedvals.push_back(a_(0.0001));
                for (Size i=0;i<a_.size()-1;i++)
                        speedvals.push_back(
                        a_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(speedperiods_, speedvals);
        }

    boost::function<Real (Time)> GeneralizedHullWhite::vol() const {

                std::vector<Real> volvals;
                volvals.push_back(sigma_(0.0001));
                for (Size i=0;i<sigma_.size()-1;i++)
                        volvals.push_back(
                        sigma_(
                        (speedstructure_[i+1]-speedstructure_[0])/365.0
                        - 0.00001));

                return PiecewiseLinearCurve(volperiods_, volvals);
        }
---

vector subscript out of range

a_.size() - 1 and sigma_.size() - 1 are bigger than speedstructure_[i+1]

</quote>
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Luigi Ballabio
In reply to this post by Javit Hafizoglu
On Thu, 2010-05-27 at 10:04 -0700, Javit Hafizoglu wrote:
> I believe Luigi forgot to change the generalized HW code when he made the
> change in the parameter class to include the positive constraint.

Yes, possibly.

> Your point for the "size()"  vs "size()+1" is valid.

> Luigi, please let me know if you would like me to get involved also.

Yes, please. If you could correct the size issue and check that the
trunk version calibrates correctly, that would be great.

Thanks,
        Luigi


--

Ogden's Law:
The sooner you fall behind, the more time you have to catch up.



------------------------------------------------------------------------------

_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Javit Hafizoglu
I'm relocating. Lots of things to do. I'm planning to finish this update by the end of June.

Luigi Ballabio wrote
On Thu, 2010-05-27 at 10:04 -0700, Javit Hafizoglu wrote:
> I believe Luigi forgot to change the generalized HW code when he made the
> change in the parameter class to include the positive constraint.

Yes, possibly.

> Your point for the "size()"  vs "size()+1" is valid.

> Luigi, please let me know if you would like me to get involved also.

Yes, please. If you could correct the size issue and check that the
trunk version calibrates correctly, that would be great.

Thanks,
        Luigi


--

Ogden's Law:
The sooner you fall behind, the more time you have to catch up.



------------------------------------------------------------------------------

_______________________________________________
QuantLib-dev mailing list
QuantLib-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Luigi Ballabio
On Thu, 2010-06-10 at 10:16 -0700, Javit Hafizoglu wrote:
> I'm relocating. Lots of things to do. I'm planning to finish this update by
> the end of June.

No problem, take your time.

Luigi



> Luigi Ballabio wrote:
> >
> > On Thu, 2010-05-27 at 10:04 -0700, Javit Hafizoglu wrote:
> >> I believe Luigi forgot to change the generalized HW code when he made the
> >> change in the parameter class to include the positive constraint.
> >
> > Yes, possibly.
> >
> >> Your point for the "size()"  vs "size()+1" is valid.
> >
> >> Luigi, please let me know if you would like me to get involved also.
> >
> > Yes, please. If you could correct the size issue and check that the
> > trunk version calibrates correctly, that would be great.
> >
> > Thanks,
> > Luigi
> >
> >
> > --
> >
> > Ogden's Law:
> > The sooner you fall behind, the more time you have to catch up.
> >
> >
> >
> > ------------------------------------------------------------------------------
> >
> > _______________________________________________
> > QuantLib-dev mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/quantlib-dev
> >
> >
>


--

Father's got the sack from the water-works
For smoking of his old cherry-briar;
Father's got the sack from the water-works
'Cos he might set the water-works on fire.



------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Alexander Lotter
In reply to this post by Javit Hafizoglu

Javit Hafizoglu wrote
I'm relocating. Lots of things to do. I'm planning to finish this update by the end of June.
Hi Javit,

Great. I could assist you by tests. Just let me know, if you need any help.

Take care

Alex
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Javit Hafizoglu

I updated the generalized Hull White model and performed tests with QL-1.0.1. Tests are successful. For some reason calibration takes longer now. In the BermudanSwaption.cpp, I changed the optimization end criteria down to 40 and 10 from 400 and 100 for the max iteration and stationary iterations. This way, it takes shorter to calibrate.

I attached the files. The changes:

-- For some reason, parameter.hpp in QL-1.0.1 is not the same as trunk version 17224. The trunk version is the correct one except the line
145        : Parameter(times.size()+1,


where the “+1” should go away.

-- The changes in GHW classes are only at the lines where a PiecewiseConstantParameter class is initiated. The new parameter.hpp accepts constraints at the initialization. I imposed positive constraints for both the a_ and sigma_ parameters in the model.

Here are my suggestions:

-- Update the parameter.hpp in QL-1.0.1 as above.
-- Update the GHW model files with the ones attached.
-- The BermudanSwaption.cpp test file can be modified as the one I attached to include the GHW test in the test-suite.

The BermudanSwaption.cpp output from my laptop is below. I look forward to your comments.

Thank you,
Javit



G2 (analytic formulae) calibration
1x5: model 10.05475 %, market 11.48000 % (-1.42525 %)
2x4: model 10.48803 %, market 11.08000 % (-0.59197 %)
3x3: model 10.67504 %, market 10.70000 % (-0.02496 %)
4x2: model 10.83340 %, market 10.21000 % (+0.62340 %)
5x1: model 10.98264 %, market 10.00000 % (+0.98264 %)
calibrated to:
a     = 0.099851, sigma = 0.010545
b     = 0.099851, eta   = 0.010545
rho   = -0.74998

Hull-White (analytic formulae) calibration
1x5: model 11.87889 %, market 11.48000 % (+0.39889 %)
2x4: model 11.76594 %, market 11.08000 % (+0.68594 %)
3x3: model 11.87495 %, market 10.70000 % (+1.17495 %)
4x2: model 11.88579 %, market 10.21000 % (+1.67579 %)
5x1: model 11.85322 %, market 10.00000 % (+1.85322 %)
calibrated to:
a = 0.10014, sigma = 0.0076919

Hull-White (numerical) calibration
1x5: model 10.31044 %, market 11.48000 % (-1.16956 %)
2x4: model 10.50842 %, market 11.08000 % (-0.57158 %)
3x3: model 10.62836 %, market 10.70000 % (-0.07164 %)
4x2: model 10.71895 %, market 10.21000 % (+0.50895 %)
5x1: model 10.81202 %, market 10.00000 % (+0.81202 %)
calibrated to:
a = 0.10014, sigma = 0.006877

Black-Karasinski (numerical) calibration
1x5: model 12.85815 %, market 11.48000 % (+1.37815 %)
2x4: model 13.11871 %, market 11.08000 % (+2.03871 %)
3x3: model 13.25333 %, market 10.70000 % (+2.55333 %)
4x2: model 13.33942 %, market 10.21000 % (+3.12942 %)
5x1: model 13.43137 %, market 10.00000 % (+3.43137 %)
calibrated to:
a = 0.08249, sigma = 0.1667

Generalized Hull-White calibration
1x5: model 11.16996 %, market 11.48000 % (-0.31004 %)
2x4: model 11.15188 %, market 11.08000 % (+0.07188 %)
3x3: model 10.87750 %, market 10.70000 % (+0.17750 %)
4x2: model 10.42011 %, market 10.21000 % (+0.21011 %)
5x1: model 9.78110 %, market 10.00000 % (-0.21890 %)
parameter[0] =, 0.04
parameter[1] =, 0.37449
parameter[2] =, 0.084144
parameter[3] =, 0.1
parameter[4] =, 0.24785
parameter[5] =, 0.053831
Payer bermudan swaption struck at 5.00000 % (ATM)
Generalized HW: 14.515
G2:       14.549
HW:       15.005
HW (num): 13.404
BK:       16.457
Payer bermudan swaption struck at 6.00000 % (OTM)
Generalized HW: 3.671
G2:       3.4158
HW:       3.6088
HW (num): 2.6985
BK:       5.6241
Payer bermudan swaption struck at 4.00000 % (ITM)
Generalized HW: 42.622
G2:       42.838
HW:       43.154
HW (num): 42.52
BK:       42.998

Run completed in 15 m 31 s

Press any key to continue . . .


Alexander Lotter wrote
Javit Hafizoglu wrote
I'm relocating. Lots of things to do. I'm planning to finish this update by the end of June.
Hi Javit,

Great. I could assist you by tests. Just let me know, if you need any help.

Take care

Alex
GHW.rar
Reply | Threaded
Open this post in threaded view
|

Re: Generalized Hull-White model problems

Luigi Ballabio
On Thu, 2010-07-29 at 22:49 -0700, Javit Hafizoglu wrote:

> I updated the generalized Hull White model and performed tests with
> QL-1.0.1. Tests are successful. For some reason calibration takes longer
> now. In the BermudanSwaption.cpp, I changed the optimization end criteria
> down to 40 and 10 from 400 and 100 for the max iteration and stationary
> iterations. This way, it takes shorter to calibrate.
>
> I attached the files. The changes:
>
> -- For some reason, parameter.hpp in QL-1.0.1 is not the same as trunk
> version 17224.

That's because 1.0.1 is a bug-fix release, and wasn't supposed to
contain any new stuff.  Your contribution and the related parameter
changes went on the trunk so that it will be in 1.1 (whenever that comes
out.)  In fact, it would be nice if you could base your files on the
trunk version so that it will be easier to merge in your changes.  Do
you think you might find some time to do this?

Thanks,
        Luigi


--

Discontent is the first necessity of progress.
-- Thomas A. Edison



------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev