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]]
发送时间: 2013年1月8日 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.
******************************************************************
| Free forum by Nabble | Edit this page |