Quantlib's equivalent of Matlab's lsqnonlin

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

Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
Hi,
 
I'm trying to convert some code written in matlab to C++. I am looking for the Quantlib equivalent of Matlab's non-linear least squares optimizer function, lsqnonlin.
 
I really appreciate any instructions on how to do this in quantlib or in C++ in general. Any code example would be reall great.
 
Many thanks.
 
CJ 


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
I was able to find the following two links on this subject. However, the example code posted in the links do not compile with recent versions of QuantLib. I am wondering if anyone has a working version of this code that works with current versions of QuantLib.

http://quantlib.org/quep/quep003.html

http://article.gmane.org/gmane.comp.finance.quantlib.user/277 


Thank you very much.

CJ
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

Luigi Ballabio
On Thu, 2008-06-05 at 08:22 -0700, cc2008 wrote:
> I was able to find the following two links on this subject. However, the
> example code posted in the links do not compile with recent versions of
> QuantLib. I am wondering if anyone has a working version of this code that
> works with current versions of QuantLib.
>
> http://quantlib.org/quep/quep003.html
>
> http://article.gmane.org/gmane.comp.finance.quantlib.user/277 

The optimization routines are in ql/math/optimization.
They're used, for instance, in the CalibratedModel::calibrate() method
(see <ql/models/model.cpp>.)  An example of setup is in the
BermudanSwaption example.

Luigi



--

Perfection is reached, not when there is no longer anything to add, but
when there is no longer anything to take away.
-- Antoine de Saint-Exupery



-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
Thanks for the reply!

I'm also looking at the test-suite/hestonmodel.cpp example which is kind of closer to what I want to do.

I have a question about the way the calibration is done. In testBlackCalibration and testDAXCalibration methods, I don't see any use of a market price. So, what are we calibrating this model to ?   I'm sure I'm missing something ;)

Thank you.

CJ
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

Klaus Spanderen-2
Hi

the prices are calculated within the HestonModelHelper class using the implied
volatility surface (prices are provided via the volatility surface.).

The Model calibrates to match the volatility surface or the prices in a least
square sense. Depends on how the HestonModelHelper is instantiated (see last
argument of the constructor)

cheers
 Klaus


On Friday 06 June 2008 20:33:01 cc2008 wrote:

> Thanks for the reply!
>
> I'm also looking at the test-suite/hestonmodel.cpp example which is kind of
> closer to what I want to do.
>
> I have a question about the way the calibration is done. In
> testBlackCalibration and testDAXCalibration methods, I don't see any use of
> a market price. So, what are we calibrating this model to ?   I'm sure I'm
> missing something ;)
>
> Thank you.
>
> CJ



--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
EMail: [hidden email] (remove NOSPAM from the address)

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
Hi, Klaus,

 Thank you very much. Yes, I understand it now. I have one other question pls ;)

 If  I want to use the put prices for the calibration, how can I use the HestonModelHelper class. I see it does the work for Call payoff. I don't see how to change that to a Put payoff ?

 Thank you very much.

CJ
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
Besides my question about how to use put payoffs for the calibration, I have another question ; )

To use the prices for the calibration, do I only have to use calibrateVolatility=false ? how do you pass the option price to this constructor -- is it just passing the price instead of volatility (5th parameter) in the constructor below ?  --Btw, I tried that but the calibration process did not change change any of my intial guesses .

Thank you very much.

// constructor for ATM option
        HestonModelHelper(const Period& maturity,
                          const Calendar& calendar,
                          const Real s0,
                          const Real strikePrice,
                          const Handle<Quote>& volatility,
                          const Handle<YieldTermStructure>& riskFreeRate,
                          const Handle<YieldTermStructure>& dividendYield,
                          bool calibrateVolatility = false);
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

Klaus Spanderen-2
Hi


On Tuesday 10 June 2008 02:09:03 cc2008 wrote:
> To use the prices for the calibration, do I only have to use
> calibrateVolatility=false ?
If the parameter calibrateVolatility is true the optimizer searchs for the
minimum of sum_{calibrationInstruments} (modelVolatility - marketVolatiliy)^2
otherwise the optimizer is minimizing sum_{calibrationInstruments}
((modelPrices - marketPrices)/marketPrices)^2.


> how do you pass the option price to this
> constructor -- is it just passing the price instead of volatility
Market prices are always given via the implied market volatility of the
calibration instruments  (Volatility parameter of the constructor. This
should be the market volatility. If you have only prices you need to convert
them to implied vols.). HestonModelHelper are by default european call
options.

> (5th
> parameter) in the constructor below ?  --Btw, I tried that but the
> calibration process did not change change any of my intial guesses .
>
> Thank you very much.
>
> // constructor for ATM option
>         HestonModelHelper(const Period& maturity,
>                           const Calendar& calendar,
>                           const Real s0,
>                           const Real strikePrice,
>                           const Handle& volatility,
>                           const Handle<YieldTermStructure>& riskFreeRate,
>                           const Handle<YieldTermStructure>& dividendYield,
>                           bool calibrateVolatility = false);



--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
EMail: [hidden email] (remove NOSPAM from the address)

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
Hi Klaus,
 Thank you very much.

CJ
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
When I ran my program, I am getting a large number of messages saying
"root not bracketed".

Does that mean I am not reaching the global minimum in the optimization? If so, are there any suggestions to improve my calibration ?

Thanks much.

CJ
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
specifically, I have the following initial guesses for the parameters,
    Real v0      = 0.14;
    Real kappa = 1.32;
    Real theta  = 0.35;
    Real sigma = 0.2;
    Real rho    = 0.4;

Then, in my code I do (which is cut and paste from test-suite/hestonmodel.cpp

LevenbergMarquardt om(1e-8, 1e-8, 1e-8);  
hestonModel->calibrate(options, om, EndCriteria(400, 40, 1.0e-8, 1.0e-8, 1.0e-8));

the program outputs the following messages saying "root not bracketed". output attached. Could you pls help me interpret these messages so I can change my initial parameters or any other parameters so I can get a good calibration.

At the end of the calibration I get the following values for the heston parameters:
 
theta   = 0.600356
kappa  = 3.37307
sigma  = 0.00585459
rho     = 0.27113
v0      = 0.284722

Thank you very much.

CJoutput.txt
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

Klaus Spanderen-2
Hi

could you pls send me the complete test program.

cheers

On Wednesday 11 June 2008 19:13:00 cc2008 wrote:

> specifically, I have the following initial guesses for the parameters,
>     Real v0      = 0.14;
>     Real kappa = 1.32;
>     Real theta  = 0.35;
>     Real sigma = 0.2;
>     Real rho    = 0.4;
>
> Then, in my code I do (which is cut and paste from
> test-suite/hestonmodel.cpp
>
> LevenbergMarquardt om(1e-8, 1e-8, 1e-8);
> hestonModel->calibrate(options, om, EndCriteria(400, 40, 1.0e-8, 1.0e-8,
> 1.0e-8));
>
> the program outputs the following messages saying "root not bracketed".
> output attached. Could you pls help me interpret these messages so I can
> change my initial parameters or any other parameters so I can get a good
> calibration.
>
> At the end of the calibration I get the following values for the heston
> parameters:
>
> theta   = 0.600356
> kappa  = 3.37307
> sigma  = 0.00585459
> rho     = 0.27113
> v0      = 0.284722
>
> Thank you very much.
>
> CJ http://www.nabble.com/file/p17782708/output.txt output.txt



--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
EMail: [hidden email] (remove NOSPAM from the address)

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
Hi Klaus,
 Attached, pls find my test program and data. I just tried to covert some prices I'm reading from data.txt to implied volatilities and feed them into heston calibration helper. I was looking to see if there's an easier way to get the implied vols from the prices, but this is how I'm doing it at the moment. If you know a easier way, pls feel free to point those out as well.

Also, I was not sure about the value of last parameter (flatVolTS) of the BlackScholesMertonProcess should be (since my goal is to get implied vol) so I made something up.

Volatility volatility = 0.2; // I'm just setting it to some value here ... not sure if that's right.

Handle<BlackVolTermStructure> flatVolTS(boost::shared_ptr<BlackVolTermStructure>(
                new BlackConstantVol(settlementDate, volatility, dayCounter)));

boost::shared_ptr<StochasticProcess> stochasticProcess(new BlackScholesMertonProcess(s0, dividendTS, riskFreeTS, flatVolTS));


Thank you very much.

CJHestonCalibration.cppdata.txt
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib's equivalent of Matlab's lsqnonlin

nabbleuser2008
In reply to this post by nabbleuser2008
I found that I'm getting "root not bracketed" messages from my implied volatility calculations, not from model calibration. Sorry about that.

Now I'm trying to figure out how to get the implied vols for those price quotes.  ;(

Any ideas, Thanks much.

CJ