Can anyone help me with Levenberg optimizatiion in Quantlib

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

Can anyone help me with Levenberg optimizatiion in Quantlib

Benqing Shen

Hi all,

 

I am new to Quantlib and trying to use LevenbergMarquardt to optimize a toy problem.

 

I’ve derived a function class from CostFunction:

// =====================================================

class ObjFun : public CostFunction

{

public:

                virtual Real value(const Array& x) const;

                virtual Disposable<Array> values(const Array& x) const;

};

 

Real ObjFun::value(const Array& x) const

{

                Real fval = exp(x[0]) * (4 * x[0] * x[0] + 2 * x[1] * x[1] + 4 * x[0] * x[1] + 2 * x[1] + 1);

                return fval;

}

 

Disposable<Array> ObjFun::values(const Array& x) const

{

                Array arr = x;

                Disposable<Array> dispArr(arr);

                return dispArr;

}

//---------------------------------------------------------

 

And then I wrote the following codes:

// ================================================

void testLMA()

{

                NoConstraint noCon;

                LevenbergMarquardt lm(1e-8);

                EndCriteria endCrit(1000,400,1e-8,1e-8,1e-8);

                ObjFun myfun;

                Array init(2);

                init[0] = 0.5;

                init[1] = -1;

                Real testVal = myfun.value(init);

                Array grad(2);

                myfun.gradient(grad,init);

                Real g2 = grad[1];

                Problem problem1(myfun,noCon,init);

                EndCriteria::Type endType = lm.minimize(problem1,endCrit);

                Real x0 = problem1.currentValue()[0];

                testVal = problem1.functionValue();

}

//------------------------------------------------------------------------

 

Despite all possible initial guess I’ve input, the method just converges to the same non-optimized point every time, namely (0,0) in this case.

 

Can anyone help me out of this problem? Thanks in advance.

 

Ben

******************************************************************
This communication is intended solely for the addressee and is confidential. If you
are not the intended recipient, any disclosure, copying, distribution or any action taken
or omitted to be taken in reliance on it, is prohibited and may be unlawful. Unless
indicated to the contrary: it does not constitute professional advice or opinions upon
which reliance may be made by the addressee or any other party, and it should be
considered to be a work in progress.
******************************************************************


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

答复: Can anyone help me with Levenberg optimizatiion in Quantlib

cheng li

Hi Benqing

 

I think you have misunderstood the LM algorithm. LevenbergMarquardt optimization is actually a multi �Cobjectives fitting method. As  the implementation in Quantlib, the core functionality you should define is the member functions “values” in CostFunction instead of “value”. E.g. as in your case, you defined it as follows:

 

Disposable<Array> ObjFun::values(const Array& x) const

{

                Array arr = x;

                Disposable<Array> dispArr(arr);

                return dispArr;

}

 

As seen by the optimization engine, you set the array “dispArr” as the objectives. That means the engine will try it best to adjust the parameter “x” to make each cell of “dispArr” close to 0. In your setting, it happily find the solution should be [0,0].

 

http://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm

 

Regards

Cheng

 

发件人: Benqing Shen [mailto:[hidden email]]
发送时间: 201318 1:30
收件人: [hidden email]
主题: [Quantlib-users] Can anyone help me with Levenberg optimizatiion in Quantlib

 

Hi all,

 

I am new to Quantlib and trying to use LevenbergMarquardt to optimize a toy problem.

 

I’ve derived a function class from CostFunction:

// =====================================================

class ObjFun : public CostFunction

{

public:

                virtual Real value(const Array& x) const;

                virtual Disposable<Array> values(const Array& x) const;

};

 

Real ObjFun::value(const Array& x) const

{

                Real fval = exp(x[0]) * (4 * x[0] * x[0] + 2 * x[1] * x[1] + 4 * x[0] * x[1] + 2 * x[1] + 1);

                return fval;

}

 

Disposable<Array> ObjFun::values(const Array& x) const

{

                Array arr = x;

                Disposable<Array> dispArr(arr);

                return dispArr;

}

//---------------------------------------------------------

 

And then I wrote the following codes:

// ================================================

void testLMA()

{

                NoConstraint noCon;

                LevenbergMarquardt lm(1e-8);

                EndCriteria endCrit(1000,400,1e-8,1e-8,1e-8);

                ObjFun myfun;

                Array init(2);

                init[0] = 0.5;

                init[1] = -1;

                Real testVal = myfun.value(init);

                Array grad(2);

                myfun.gradient(grad,init);

                Real g2 = grad[1];

                Problem problem1(myfun,noCon,init);

                EndCriteria::Type endType = lm.minimize(problem1,endCrit);

                Real x0 = problem1.currentValue()[0];

                testVal = problem1.functionValue();

}

//------------------------------------------------------------------------

 

Despite all possible initial guess I’ve input, the method just converges to the same non-optimized point every time, namely (0,0) in this case.

 

Can anyone help me out of this problem? Thanks in advance.

 

Ben

******************************************************************
This communication is intended solely for the addressee and is confidential. If you
are not the intended recipient, any disclosure, copying, distribution or any action taken
or omitted to be taken in reliance on it, is prohibited and may be unlawful. Unless
indicated to the contrary: it does not constitute professional advice or opinions upon
which reliance may be made by the addressee or any other party, and it should be
considered to be a work in progress.
******************************************************************


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users