Optimization - abertout/breeze GitHub Wiki
Optimization
Breeze comes with several optimization methods, located in breeze.optimize
module.
Root Finding
breeze.optimize.RootFinding
object comes with the find method (similar to fzero from Matlab or root from python scipy) to find a root of a given function (should be Double => Double
) from an initial estimate:
import breeze.optimize.RootFinding
val f = (x: Double) => math.sin(x)
scala> val x = RootFinding.find(f,3)
x: Double = 3.1415926535897936
A optional second estimate that should bracket the root with the first one can be given (otherwise this is adjusted automatically):
Rootfinding.find(f, 3, Some(3.5))
the find method actually implements the Brent's algorithm[1].
Brent's method together with seminal root finding methods may be called directly from breeze.optimize.RootFinding
object:
Brent's method
Rootfinding.brent(f, 0, 3.5)
A bracketing interval ([0,3.5] in the above example) of the root must be provided to the Brent's method.
Newton-Raphson method
val fd = (x: Double) => math.cos(x)
Rootfinding.newtonRaphson(f, fd, 3, maxIter = 100)
Notice that the NR method requires the derivative of f. As the method may diverge, an optional maximum number of iterations can be defined (otherwise a large default one is set)
Secant method
The secant method is similar to the NR method but replaces the derivative by a linear approximation.
Rootfinding.secant(f, 0, 3.5)
Similarly to the Newton-Raphson method, an optional maximum number of iterations can be defined. Here, the two estimates given in parameter should not necessarily bracket the root.
Bisection method
Rootfinding.bisection(f, 3.3, 3.5)
The two estimates given must bracket the root.
[1] Brent, R., Algorithms for Minimization Without Derivatives, Prentice-Hall, 1973.