Login  Register

Re: Hi, I'm the user of QuantLib

Posted by Kim Kuen Tang on Mar 27, 2011; 7:37pm
URL: http://quantlib.414.s1.nabble.com/Hi-I-m-the-user-of-QuantLib-tp13330p13331.html


Hi Jason,

can you provide a standalone example the next time?

Am 25.03.2011 05:09, schrieb 蔡宗儒-風險管理處-銀行:
Hello,
    I'm the user of QuantLib.
    I have some question about optimization.
 
    I use the optimization method LevenbergMarquardt and the cost function OneDimensionalPolynomialDegreeN in optimizers.cpp
    I solve the following eqtions seperately(I did not solve together) 
        E1 = 1+x+x^2
        E2 = -1+x+x^2
    In E1, I get the right solution, (x ,E1)=(-0.5 , 0.75)
    In E2, I get the wrong solution, (x,E2)=(-0.6180 , 0)
 
    I think the reason why E2 get the wrong answear is that LevenbergMarquardt method use "MINPACK.lmdif".The MINPACK.lmdif seems to minize the sum of square.

This is true according to the documentation for minpack subroutine lmdif. So the procedure will terminate when especially a root is founded. This is also the reason why you got 0.6180 ( you really mean 0.6180 and not -0.6180) as a result since this is a root from E2.

Below a standalone example is provided:


# include <iostream>
# include <ql/math/optimization/levenbergmarquardt.hpp>
# include <ql/math/optimization/constraint.hpp>
# include <ql/math/optimization/costfunction.hpp>
# include <boost/foreach.hpp>


using namespace QuantLib;

class OneDimensionalPolynomialDegreeN : public CostFunction {
      public:
        OneDimensionalPolynomialDegreeN(const Array& coefficients)
        : coefficients_(coefficients),
          polynomialDegree_(coefficients.size()-1) {}

        Real value(const Array& x) const {
            QL_REQUIRE(x.size()==1,"independent variable must be 1 dimensional");
            Real y = 0;
            for (Size i=0; i<=polynomialDegree_; ++i) y += coefficients_[i]*std::pow(x[0],static_cast<int>(i));
            return y;
        }

        Disposable<Array> values(const Array& x) const{
            QL_REQUIRE(x.size()==1,"independent variable must be 1 dimensional");
            Array y(1);
            y[0] = value(x);
            return y;
        }

      private:
        Array const coefficients_;
        Size const polynomialDegree_;
    };


int main()
{

    Array c(3, 1.0); c[0]=-1.0;
    OneDimensionalPolynomialDegreeN p(c);
    NoConstraint n;

    Real ps[] = { -3.0,-2.0,-1.5,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0,3.0 };

    std::cout<< "init" << "\t" << "result" << "\t" << "value" << "\t" << "ec" << "\n";
    BOOST_FOREACH(Real init,ps)
    {
        Array i(1, init);
        LevenbergMarquardt o;
        EndCriteria e(1000, 100, 1e-5, 1e-5, 1e-5);
        Problem pp(p, n,i);
        EndCriteria::Type ec = o.minimize(pp, e);
        Array xM = pp.currentValue();
        Array yM = pp.values(xM);

        std::cout<< init << "\t" << xM[0] << "\t" << yM[0] << "\t" << ec << "\n";
    }


    return 1;
}

    That means it dose not minize the equation but MSE.
 
    Therefore,I think LevenbergMarquardt in QuantLib seems to min the value (E1-0)^2 in E1 and  min the value (E2-0)^2 in E2.Hence I got the wrong solution.
 
    So, did I use the method wrong ?
    Or QuantLib use the wrong methd "MINPACK.lmdif" ?
 
 
Regards,
 
蔡宗 儒   Jason Tsai
永豐銀行 風險管理處
TEL:81618681
FAX:81618482
 
------------------------------------------------------------------------------ Enable your software for Intel(R) Active Management Technology to meet the growing manageability and security demands of your customers. Businesses are taking advantage of Intel(R) vPro (TM) technology - will your software be a part of the solution? Download the Intel(R) Manageability Checker today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev


------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software
be a part of the solution? Download the Intel(R) Manageability Checker
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev