Class BracketFinder
- java.lang.Object
-
- org.apache.commons.statistics.inference.BracketFinder
-
class BracketFinder extends java.lang.Object
Provide an interval that brackets a local minimum of a function. This code is based on a Python implementation (from SciPy, moduleoptimize.py
v0.5).This class has been extracted from
o.a.c.math4.optim.univariate
and modified to: remove support for bracketing a maximum; support bounds on the bracket; correct the sign of the denominator when the magnitude is small; and return true/false if there is a minimum strictly inside the bounds.- Since:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description private static double
EPS_MIN
Tolerance to avoid division by zero.private int
evaluations
Number of function evaluations performed in the last search.private double
fHi
Function value athi
.private double
fLo
Function value atlo
.private double
fMid
Function value atmid
.private static double
GOLD
Golden section.private double
growLimit
Factor for expanding the interval.private double
hi
Higher bound of the bracket.private double
lo
Lower bound of the bracket.private int
maxEvaluations
Number of allowed function evaluations.private double
mid
Point inside the bracket.
-
Constructor Summary
Constructors Constructor Description BracketFinder()
Constructor with default values100, 100000
(see theother constructor
).BracketFinder(double growLimit, int maxEvaluations)
Create a bracketing interval finder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) int
getEvaluations()
(package private) double
getFHi()
Get function value atgetHi()
.(package private) double
getFLo()
Get function value atgetLo()
.(package private) double
getFMid()
Get function value atgetMid()
.(package private) double
getHi()
(package private) double
getLo()
(package private) double
getMid()
(package private) boolean
search(java.util.function.DoubleUnaryOperator func, double a, double b, double min, double max)
Search downhill from the initial points to obtain new points that bracket a local minimum of the function.private double
value(java.util.function.DoubleUnaryOperator func, double x)
Get the value of the function.
-
-
-
Field Detail
-
EPS_MIN
private static final double EPS_MIN
Tolerance to avoid division by zero.- See Also:
- Constant Field Values
-
GOLD
private static final double GOLD
Golden section.- See Also:
- Constant Field Values
-
growLimit
private final double growLimit
Factor for expanding the interval.
-
maxEvaluations
private final int maxEvaluations
Number of allowed function evaluations.
-
evaluations
private int evaluations
Number of function evaluations performed in the last search.
-
lo
private double lo
Lower bound of the bracket.
-
hi
private double hi
Higher bound of the bracket.
-
mid
private double mid
Point inside the bracket.
-
fLo
private double fLo
Function value atlo
.
-
fHi
private double fHi
Function value athi
.
-
fMid
private double fMid
Function value atmid
.
-
-
Constructor Detail
-
BracketFinder
BracketFinder()
Constructor with default values100, 100000
(see theother constructor
).
-
BracketFinder
BracketFinder(double growLimit, int maxEvaluations)
Create a bracketing interval finder.- Parameters:
growLimit
- Expanding factor.maxEvaluations
- Maximum number of evaluations allowed for finding a bracketing interval.- Throws:
java.lang.IllegalArgumentException
- if thegrowLimit
ormaxEvalutations
are not strictly positive.
-
-
Method Detail
-
search
boolean search(java.util.function.DoubleUnaryOperator func, double a, double b, double min, double max)
Search downhill from the initial points to obtain new points that bracket a local minimum of the function. Note that the initial points do not have to bracket a minimum. An exception is raised if a minimum cannot be found within the configured number of function evaluations.The bracket is limited to the provided bounds if they create a positive interval
min < max
. It is possible that the middle of the bracket is at the bounds as the final bracket isf(mid) <= min(f(lo), f(hi))
andlo <= mid <= hi
.No exception is raised if the initial points are not within the bounds; the points are updated to be within the bounds.
No exception is raised if the initial points are equal; the bracket will be returned as a single point
lo == mid == hi
.- Parameters:
func
- Function whose optimum should be bracketed.a
- Initial point.b
- Initial point.min
- Minimum bound of the bracket (inclusive).max
- Maximum bound of the bracket (inclusive).- Returns:
- true if the mid-point is strictly within the final bracket
[lo, hi]
; false if there is no local minima. - Throws:
java.lang.IllegalStateException
- if the maximum number of evaluations is exceeded.
-
getEvaluations
int getEvaluations()
- Returns:
- the number of evaluations.
-
getLo
double getLo()
- Returns:
- the lower bound of the bracket.
- See Also:
getFLo()
-
getHi
double getHi()
- Returns:
- the higher bound of the bracket.
- See Also:
getFHi()
-
getMid
double getMid()
- Returns:
- a point in the middle of the bracket.
- See Also:
getFMid()
-
value
private double value(java.util.function.DoubleUnaryOperator func, double x)
Get the value of the function.- Parameters:
func
- Function.x
- Point.- Returns:
- the value
- Throws:
java.lang.IllegalStateException
- if the maximal number of evaluations is exceeded.
-
-