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 ****************************************************************** ------------------------------------------------------------------------------ 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 |
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]] 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 ****************************************************************** ------------------------------------------------------------------------------ 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 |
Free forum by Nabble | Edit this page |