Hi, I'm the user of QuantLib

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

Hi, I'm the user of QuantLib

蔡宗儒-風險管理處-銀行
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.
    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
Reply | Threaded
Open this post in threaded view
|

Re: Hi, I'm the user of QuantLib

Kim Kuen Tang

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
Reply | Threaded
Open this post in threaded view
|

Re: Hi, I'm the user of QuantLib

蔡宗儒-風險管理處-銀行
Hello,
    I deeply appreciate for your reply.(you are right about 0.6180)
 
    Actually, in E1, the root is not real and the min is (-0.5, 0.75)
                     in E2, the root is (-1.6180,0) ,(0.6180,0) and the min is (-0.5,-1.25)
 
    When using LevenbergMarquardt the result is the min in E1 but root in E2.
    When using ConjugateGradient the result is the min in E1 and also min in E2.
 
    I got a little confused about these two optimization method.
    The method ConjugateGradient in Qunatlib is actully to find the min.
    But I don't know how to read the LevenbergMarquardt result.
    Can you tell me LevenbergMarquardt  in Qunatlib is goal to find min or root?
    Thanks for you reply again!!!!
 
 
 
Here is my code in c#(sorry about not c++)
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNet;
class Program
{
    static void Main(string[] args)
    {
        Vector coefficients = new Vector();
        coefficients.Add(-1);
        coefficients.Add(1);
        coefficients.Add(1);
 
        CostFunction costFunction = new OneDimensionalPolynomialDegreeN(coefficients);
        Constraint constraint = new NoConstraint();
        Vector initialValue = new Vector(1, 2);
        Problem problem = new Problem(costFunction, constraint, initialValue);
 
        double epslon = 1.0e-10;
        OptimizationMethod optimizationMethod = new LevenbergMarquardt(epslon, epslon, epslon);
        //OptimizationMethod optimizationMethod = new ConjugateGradient();
        EndCriteria endCriteria = new EndCriteria(1000, 100, epslon, epslon, epslon);
 
        EndCriteria.Type endCriteriaType = optimizationMethod.minimize(problem, endCriteria);
 
        Vector x = problem.currentValue();
        Vector y = problem.values(x);
            
        Console.WriteLine("endCriteriaType : " + endCriteriaType.ToString());
        Console.WriteLine("x : " + x[0]);
        Console.WriteLine("y : " + y[0]);
        Console.Read();

    }
 
 
    public class OneDimensionalPolynomialDegreeN : CostFunction
    {
        private Vector coefficients_;
        private int polynomialDegree_;
 
        public OneDimensionalPolynomialDegreeN(Vector coefficients)
        {
            coefficients_ = new Vector(coefficients);
            polynomialDegree_ = coefficients.size() - 1;
        }
 
        public override double value(Vector x)
        {
            if (x.size() != 1) throw new ApplicationException("independent variable must be 1 dimensional");
            double y = 0;
            for (int i = 0; i <= polynomialDegree_; ++i)
                y += coefficients_[i] * Utils.Pow(x[0], i);
            return y;
        }
 
        public override Vector values(Vector x)
        {
            if (x.size() != 1) throw new ApplicationException("independent variable must be 1 dimensional");
            Vector y = new Vector(1);
            y[0] = value(x);
            return y;
        }
    }
}
 
    Thank you very much!!!!
 
Regards,
 
蔡宗儒   Jason Tsai
永豐銀行風險管理處
TEL:81618681
FAX:81618482
 


From: Kim Kuen Tang [mailto:[hidden email]]
Sent: Monday, March 28, 2011 3:37 AM
To: [hidden email]; 蔡宗儒-風險管理處-銀行
Subject: Re: [Quantlib-dev] Hi, I'm the user of QuantLib


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
Reply | Threaded
Open this post in threaded view
|

Re: Hi, I'm the user of QuantLib

Luigi Ballabio
On Mon, 2011-03-28 at 10:17 +0800, 蔡宗儒-風險管理處-銀行 wrote:
>     Can you tell me LevenbergMarquardt  in Qunatlib is goal to find
> min or root?

It should be the min, but I'm afraid that the MINPACK routine it calls
stops at a root because it minimizes the sum of squares.
LevenbergMarquardt is probably not the right optimizer for your problem;
I suggest you use another.

Luigi


--

fix, n.,v.
What one does when a problem has been reported too many times
to be ignored.
-- the Jargon file



------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself;
WebMatrix provides all the features you need to develop and
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Hi, I'm the user of QuantLib

蔡宗儒-風險管理處-銀行

Hi dears,
        After survey, I think LM method is goal to "Fitting".

        Hence,in E2 = -1+x+x^2 (the unexpected solution), LM method min the distance between 0 and -1+x+x^2.
        Since LM method is to min the distance instead of function value, as your words, it is not suitable for my problem.

        My conclusion is :
        1.When I want to min the math function, I should not choose LM method.
        2.When I want to do fitting, LM method can do very well.

        In testsuite->optimizers.hpp, LM method is essential different from ConjugateGradient,SteepestDescent or BFGS.
        It is just fortunate to got the min(the example in C# is 1+x+x^2 but not -1+x+x^2).

        Thanks for your help!!
       
Regards,
 
蔡宗儒   Jason Tsai
永豐銀行風險管理處
TEL:81618681
FAX:81618482

-----Original Message-----
From: Luigi Ballabio [mailto:[hidden email]]
Sent: Thursday, March 31, 2011 5:02 PM
To: 蔡宗儒-風險管理處-銀行
Cc: Kim Kuen Tang; [hidden email]
Subject: Re: [Quantlib-dev] Hi, I'm the user of QuantLib

On Mon, 2011-03-28 at 10:17 +0800, 蔡宗儒-風險管理處-銀行 wrote:
>     Can you tell me LevenbergMarquardt  in Qunatlib is goal to find
> min or root?

It should be the min, but I'm afraid that the MINPACK routine it calls stops at a root because it minimizes the sum of squares.
LevenbergMarquardt is probably not the right optimizer for your problem; I suggest you use another.

Luigi


--

fix, n.,v.
What one does when a problem has been reported too many times to be ignored.
-- the Jargon file



------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself;
WebMatrix provides all the features you need to develop and
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev