Search code examples
scala-breeze

QuadraticMinimizer always returns a non-sensical answer


I'm having trouble with the QuadraticMinimizer- every time I execute it I get errors that are non-sensical. I've looked at the scaladoc and the code and I am just not sure what I am doing incorrectly.

I have the following code:

    val lBounds:DenseVector[Double] = DenseVector(Array(Double.NegativeInfinity, 5.0, 0.1, 50.0, 50.0))
  val uBounds:DenseVector[Double] = DenseVector(Array(Double.PositiveInfinity, 500.0, 100.0, 99000.0, 99000.0))

  val inputMatrix:DenseMatrix[Double] = breeze.linalg.csvread(file=new java.io.File(getClass.getResource("/input/zip06latlong.csv").toURI), skipLines=1)

  val y = inputMatrix(::, 0).toDenseVector
  val X = inputMatrix(::, Seq(1,2,3,4,5)).copy.toDenseMatrix
  val T = X.toDenseMatrix.t

  val gram = (T * X)
  val b:Transpose[DenseVector[Double]] = y.t * X

  println(gram)
  println(b.inner)
  breeze.linalg.csvwrite(new java.io.File("grammatrix.csv"), gram)
  val minimizer:QuadraticMinimizer = new QuadraticMinimizer(rank(gram), ProjectBox(lBounds,uBounds))


  val coeffs =  minimizer.minimize(gram, b.inner)
  println(coeffs)

here's what the gram looks like:

    279.0      628207.0       1461245.0        1024.0     729.5      
628207.0   1.569309427E9  3.414471724E9    2449533.0  1755536.5  
1461245.0  3.414471724E9  1.2324155401E10  5511816.0  3846583.0  
1024.0     2449533.0      5511816.0        3980.0     2786.0     
729.5      1755536.5      3846583.0        2786.0     2092.75

and the b vector is:

2.917264069193999E8, 7.294450468242601E11, 1.585917338779061E12, 1.131888709844E9, 8.260072757806E8

when I execute the code I get:

DenseVector(-12171.118011368422, -424.79971124882286, -9.565028484748783, 49.3827769217138, 49.536905925364195)

Which is below the lower bounds specified.


Solution

  • Could you please add the H (gram matrix) and y (linear term) as well so that I can test it out ? Also the initialize pattern is for advanced users (like Spark ALS for which I wrote the solver) but you can use the simple pattern as follows (I used your bounds):

    val lb = DenseVector(Array(Double.NegativeInfinity, 5.0, 0.1, 50.0, 50.0))
    val ub = DenseVector(Array(Double.PositiveInfinity, 500.0, 100.0, 99000.0, 99000.0))
    
    val n = 5
    
    val ata = new DenseMatrix[Double](5, 5,
      Array(4.377, -3.531, -1.306, -0.139, 3.418,
        -3.531, 4.344, 0.934, 0.305, -2.140,
        -1.306, 0.934, 2.644, -0.203, -0.170,
        -0.139, 0.305, -0.203, 5.883, 1.428,
        3.418, -2.140, -0.170, 1.428, 4.684))
    
    val atb = DenseVector(-1.632, 2.115, 1.094, -1.025, -0.636)
    
    val qpSolverBounds = new QuadraticMinimizer(n, ProjectBox(lb, ub))
    val result = qpSolverBounds.minimize(ata, atb)
    println(s"Bounds test $result")
    

    I am getting result as DenseVector(-33.02749751949581, 5.0, 0.1, 50.0, 50.0) which does not look non-sensical.

    Please don't change the default maxIter since this control is given for flows where absolute convergence is not necessary. Let the solver choose the maxIter.