• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Help with OPT++ constraints

Joined
3/22/10
Messages
1
Points
11
I am using open source (Sandia Labs) OPT++ with newmat to model a GARCH(1,1) process. I need to estimate the parameters (omega, alpha, beta) which give the maximum likelihood. I have convinced myself that the likelihood function is correct. When I minimize (the negative) of the likelihood function without constraints it quickly goes to minus infinity as expected. I wish to implement the constraints
C++:
0 < omega
0 < alpha + beta < 1
This requires modification
C++:
eps = 1.e-6
eps < omega < 1./eps
eps < alpha + beta < 1. - eps
OPT++ has the capability to represent a constraint
C++:
    double eps = 1.0e-6;
    Matrix A(2,3);
    A << 0. << 1. << 1.
      << 1. << 0. << 0.;
    ColumnVector lower(2);
    lower << eps << eps;
    ColumnVector upper(2);
    double upp1 = 1.0 - eps;
    double upp2 = 1.0/eps;
    upper << upp1 << upp2;
    Constraint ineq = new LinearInequality(A, lower, upper);
    CompoundConstraint cc(ineq);
    
    // Nonlinear likelihood function
    // with 3 params, and (linear? constraint)
    NLF0 nlp(ndim, like, init_like, &cc);

    // The search algorithm used in minimization of -likelihood
    OptPDS objfcn(&nlp);
.
.
 .
 try {
  objfcn.optimize();
  }
   // catch exceptions thrown by newmat
   catch(BaseException) { cout << BaseException::what() << endl; }

   catch(...) { cout << "exception caught in main program" << endl; }
The code throws the exception
C++:
An exception has been thrown
Logic error:- detected by Newmat: index error: requested index = 3

MatrixType = Rect   # Rows = 2; # Cols = 1
It seems (from looking in the debugger) that a simplex search is being
set up and it's checking whether lower bounds are lower that upper bounds.
I suspect its a long shot that anyone has experienced this particular problem,
but I'm stuck trying to figure this out
Thanks and Cheers
K
 
I coded up a garch class in C# not too long ago that used either a Simplex or a heuristic search method, unfortunately I'm unfamiliar with OPT++.

But if it helps here is my negative log likelihood (no mean eq):

C++:
//NEGATIVE LOG LIKELIHOOD FUNCTION
        static public double F(double[] theta)
        {
            int n = LnReturns.Length;
            double LLsum = 0;

            for (int i = 1; i < n; ++i)
            {
                sigma2[i] = theta[2] + theta[0] * sigma2[i - 1] +
                            theta[1] * LnReturns[i - 1] * LnReturns[i - 1];
            }

            for (int i = 1; i < n; ++i)
            {
                LL[i] = 0.5 * Math.Log(2 * Math.PI) -
                            0.5 * ((LnReturns[i] * LnReturns[i]) / sigma2[i]) -
                            0.5 * Math.Log(sigma2[i]);

                LLsum += LL[i];
            }

            return -LLsum;
        }
and for the simplex I had to do some fiddling but found this,

C++:
double[,] simplex = {   {0.65, 0.02, 0.00001}, 
                                        {0.85, 0.08, 0.00001},
                                        {0.95, 0.10, 0.00001},
                                        {0.70, 0.20, 0.00002}   };
                Param = Solve.minimize(F, simplex);
worked well.

Hope that is at least of some use
 
Back
Top