Blame doc/min.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: optimization, see minimization
Packit 67cb25
   single: maximization, see minimization
Packit 67cb25
   single: minimization, one-dimensional
Packit 67cb25
   single: finding minima
Packit 67cb25
   single: nonlinear functions, minimization
Packit 67cb25
Packit 67cb25
****************************
Packit 67cb25
One Dimensional Minimization
Packit 67cb25
****************************
Packit 67cb25
Packit 67cb25
This chapter describes routines for finding minima of arbitrary
Packit 67cb25
one-dimensional functions.  The library provides low level components
Packit 67cb25
for a variety of iterative minimizers and convergence tests.  These can be
Packit 67cb25
combined by the user to achieve the desired solution, with full access
Packit 67cb25
to the intermediate steps of the algorithms.  Each class of methods uses
Packit 67cb25
the same framework, so that you can switch between minimizers at runtime
Packit 67cb25
without needing to recompile your program.  Each instance of a minimizer
Packit 67cb25
keeps track of its own state, allowing the minimizers to be used in
Packit 67cb25
multi-threaded programs.
Packit 67cb25
Packit 67cb25
The header file :file:`gsl_min.h` contains prototypes for the
Packit 67cb25
minimization functions and related declarations.  To use the minimization
Packit 67cb25
algorithms to find the maximum of a function simply invert its sign.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: minimization, overview
Packit 67cb25
Packit 67cb25
Overview
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The minimization algorithms begin with a bounded region known to contain
Packit 67cb25
a minimum.  The region is described by a lower bound :math:`a` and an
Packit 67cb25
upper bound :math:`b`, with an estimate of the location of the minimum
Packit 67cb25
:math:`x`, as shown in :numref:`fig_min-interval`.
Packit 67cb25
Packit 67cb25
.. _fig_min-interval:
Packit 67cb25
Packit 67cb25
.. figure:: /images/min-interval.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Function with lower and upper bounds with an estimate of the minimum.
Packit 67cb25
Packit 67cb25
The value of the function at :math:`x` must be less than the value of the
Packit 67cb25
function at the ends of the interval,
Packit 67cb25
Packit 67cb25
.. math:: f(a) > f(x) < f(b)
Packit 67cb25
Packit 67cb25
This condition guarantees that a minimum is contained somewhere within
Packit 67cb25
the interval.  On each iteration a new point :math:`x'` is selected using
Packit 67cb25
one of the available algorithms.  If the new point is a better estimate
Packit 67cb25
of the minimum, i.e.: where :math:`f(x') < f(x)`, then the current
Packit 67cb25
estimate of the minimum :math:`x` is updated.  The new point also allows
Packit 67cb25
the size of the bounded interval to be reduced, by choosing the most
Packit 67cb25
compact set of points which satisfies the constraint :math:`f(a) > f(x) < f(b)`.
Packit 67cb25
The interval is reduced until it encloses the true minimum to a
Packit 67cb25
desired tolerance.  This provides a best estimate of the location of the
Packit 67cb25
minimum and a rigorous error estimate.
Packit 67cb25
Packit 67cb25
Several bracketing algorithms are available within a single framework.
Packit 67cb25
The user provides a high-level driver for the algorithm, and the
Packit 67cb25
library provides the individual functions necessary for each of the
Packit 67cb25
steps.  There are three main phases of the iteration.  The steps are,
Packit 67cb25
Packit 67cb25
* initialize minimizer state, :data:`s`, for algorithm :data:`T`
Packit 67cb25
* update :data:`s` using the iteration :data:`T`
Packit 67cb25
* test :data:`s` for convergence, and repeat iteration if necessary
Packit 67cb25
Packit 67cb25
The state for the minimizers is held in a :type:`gsl_min_fminimizer`
Packit 67cb25
struct.  The updating procedure uses only function evaluations (not
Packit 67cb25
derivatives).
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: minimization, caveats
Packit 67cb25
Packit 67cb25
Caveats
Packit 67cb25
=======
Packit 67cb25
Packit 67cb25
Note that minimization functions can only search for one minimum at a
Packit 67cb25
time.  When there are several minima in the search area, the first
Packit 67cb25
minimum to be found will be returned; however it is difficult to predict
Packit 67cb25
which of the minima this will be. *In most cases, no error will be
Packit 67cb25
reported if you try to find a minimum in an area where there is more
Packit 67cb25
than one.*
Packit 67cb25
Packit 67cb25
With all minimization algorithms it can be difficult to determine the
Packit 67cb25
location of the minimum to full numerical precision.  The behavior of the
Packit 67cb25
function in the region of the minimum :math:`x^*` can be approximated by
Packit 67cb25
a Taylor expansion,
Packit 67cb25
Packit 67cb25
.. only:: not texinfo
Packit 67cb25
Packit 67cb25
   .. math:: y = f(x^*) + {1 \over 2} f''(x^*) (x - x^*)^2
Packit 67cb25
Packit 67cb25
.. only:: texinfo
Packit 67cb25
Packit 67cb25
   ::
Packit 67cb25
Packit 67cb25
      y = f(x^*) + (1/2) f''(x^*) (x - x^*)^2
Packit 67cb25
Packit 67cb25
and the second term of this expansion can be lost when added to the
Packit 67cb25
first term at finite precision.  This magnifies the error in locating
Packit 67cb25
:math:`x^*`, making it proportional to :math:`\sqrt \epsilon` (where
Packit 67cb25
:math:`\epsilon` is the relative accuracy of the floating point numbers).
Packit 67cb25
For functions with higher order minima, such as :math:`x^4`, the
Packit 67cb25
magnification of the error is correspondingly worse.  The best that can
Packit 67cb25
be achieved is to converge to the limit of numerical accuracy in the
Packit 67cb25
function values, rather than the location of the minimum itself.
Packit 67cb25
Packit 67cb25
Initializing the Minimizer
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
.. type:: gsl_min_fminimizer
Packit 67cb25
Packit 67cb25
   This is a workspace for minimizing functions.
Packit 67cb25
Packit 67cb25
.. function:: gsl_min_fminimizer * gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly allocated instance of a
Packit 67cb25
   minimizer of type :data:`T`.  For example, the following code
Packit 67cb25
   creates an instance of a golden section minimizer::
Packit 67cb25
Packit 67cb25
      const gsl_min_fminimizer_type * T = gsl_min_fminimizer_goldensection;
Packit 67cb25
      gsl_min_fminimizer * s = gsl_min_fminimizer_alloc (T);
Packit 67cb25
Packit 67cb25
   If there is insufficient memory to create the minimizer then the function
Packit 67cb25
   returns a null pointer and the error handler is invoked with an error
Packit 67cb25
   code of :macro:`GSL_ENOMEM`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_min_fminimizer_set (gsl_min_fminimizer * s, gsl_function * f, double x_minimum, double x_lower, double x_upper)
Packit 67cb25
Packit 67cb25
   This function sets, or resets, an existing minimizer :data:`s` to use the
Packit 67cb25
   function :data:`f` and the initial search interval [:data:`x_lower`,
Packit 67cb25
   :data:`x_upper`], with a guess for the location of the minimum
Packit 67cb25
   :data:`x_minimum`.
Packit 67cb25
Packit 67cb25
   If the interval given does not contain a minimum, then the function
Packit 67cb25
   returns an error code of :macro:`GSL_EINVAL`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
Packit 67cb25
Packit 67cb25
   This function is equivalent to :func:`gsl_min_fminimizer_set` but uses
Packit 67cb25
   the values :data:`f_minimum`, :data:`f_lower` and :data:`f_upper` instead of
Packit 67cb25
   computing :code:`f(x_minimum)`, :code:`f(x_lower)` and :code:`f(x_upper)`.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_min_fminimizer_free (gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   This function frees all the memory associated with the minimizer
Packit 67cb25
   :data:`s`.
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_min_fminimizer_name (const gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to the name of the minimizer.  For example::
Packit 67cb25
Packit 67cb25
      printf ("s is a '%s' minimizer\n", gsl_min_fminimizer_name (s));
Packit 67cb25
Packit 67cb25
   would print something like :code:`s is a 'brent' minimizer`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: minimization, providing a function to minimize
Packit 67cb25
Packit 67cb25
Providing the function to minimize
Packit 67cb25
==================================
Packit 67cb25
Packit 67cb25
You must provide a continuous function of one variable for the
Packit 67cb25
minimizers to operate on.  In order to allow for general parameters the
Packit 67cb25
functions are defined by a :type:`gsl_function` data type
Packit 67cb25
(:ref:`providing-function-to-solve`).
Packit 67cb25
Packit 67cb25
Iteration
Packit 67cb25
=========
Packit 67cb25
Packit 67cb25
The following functions drive the iteration of each algorithm.  Each
Packit 67cb25
function performs one iteration to update the state of any minimizer of the
Packit 67cb25
corresponding type.  The same functions work for all minimizers so that
Packit 67cb25
different methods can be substituted at runtime without modifications to
Packit 67cb25
the code.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_min_fminimizer_iterate (gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   This function performs a single iteration of the minimizer :data:`s`.  If the
Packit 67cb25
   iteration encounters an unexpected problem then an error code will be
Packit 67cb25
   returned,
Packit 67cb25
Packit 67cb25
   :macro:`GSL_EBADFUNC`
Packit 67cb25
Packit 67cb25
      the iteration encountered a singular point where the function evaluated
Packit 67cb25
      to :code:`Inf` or :code:`NaN`.
Packit 67cb25
Packit 67cb25
   :macro:`GSL_FAILURE`
Packit 67cb25
Packit 67cb25
      the algorithm could not improve the current best approximation or
Packit 67cb25
      bounding interval.
Packit 67cb25
Packit 67cb25
The minimizer maintains a current best estimate of the position of the
Packit 67cb25
minimum at all times, and the current interval bounding the minimum.
Packit 67cb25
This information can be accessed with the following auxiliary functions,
Packit 67cb25
Packit 67cb25
.. function:: double gsl_min_fminimizer_x_minimum (const gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   This function returns the current estimate of the position of the
Packit 67cb25
   minimum for the minimizer :data:`s`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_min_fminimizer_x_upper (const gsl_min_fminimizer * s)
Packit 67cb25
              double gsl_min_fminimizer_x_lower (const gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   These functions return the current upper and lower bound of the interval
Packit 67cb25
   for the minimizer :data:`s`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_min_fminimizer_f_minimum (const gsl_min_fminimizer * s)
Packit 67cb25
              double gsl_min_fminimizer_f_upper (const gsl_min_fminimizer * s)
Packit 67cb25
              double gsl_min_fminimizer_f_lower (const gsl_min_fminimizer * s)
Packit 67cb25
Packit 67cb25
   These functions return the value of the function at the current estimate
Packit 67cb25
   of the minimum and at the upper and lower bounds of the interval for the
Packit 67cb25
   minimizer :data:`s`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: minimization, stopping parameters
Packit 67cb25
Packit 67cb25
Stopping Parameters
Packit 67cb25
===================
Packit 67cb25
Packit 67cb25
A minimization procedure should stop when one of the following
Packit 67cb25
conditions is true:
Packit 67cb25
Packit 67cb25
* A minimum has been found to within the user-specified precision.
Packit 67cb25
* A user-specified maximum number of iterations has been reached.
Packit 67cb25
* An error has occurred.
Packit 67cb25
Packit 67cb25
The handling of these conditions is under user control.  The function
Packit 67cb25
below allows the user to test the precision of the current result.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_min_test_interval (double x_lower, double x_upper,  double epsabs, double epsrel)
Packit 67cb25
Packit 67cb25
   This function tests for the convergence of the interval [:data:`x_lower`,
Packit 67cb25
   :data:`x_upper`] with absolute error :data:`epsabs` and relative error
Packit 67cb25
   :data:`epsrel`.  The test returns :macro:`GSL_SUCCESS` if the following
Packit 67cb25
   condition is achieved,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: |a - b| < \hbox{\it epsabs} + \hbox{\it epsrel\/}\, \min(|a|,|b|)
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         |a - b| < epsabs + epsrel min(|a|,|b|) 
Packit 67cb25
Packit 67cb25
   when the interval :math:`x = [a,b]` does not include the origin.  If the
Packit 67cb25
   interval includes the origin then :math:`\min(|a|,|b|)` is replaced by
Packit 67cb25
   zero (which is the minimum value of :math:`|x|` over the interval).  This
Packit 67cb25
   ensures that the relative error is accurately estimated for minima close
Packit 67cb25
   to the origin.
Packit 67cb25
Packit 67cb25
   This condition on the interval also implies that any estimate of the
Packit 67cb25
   minimum :math:`x_m` in the interval satisfies the same condition with respect
Packit 67cb25
   to the true minimum :math:`x_m^*`,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: |x_m - x_m^*| < \hbox{\it epsabs} + \hbox{\it epsrel\/}\, x_m^*
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         |x_m - x_m^*| < epsabs + epsrel x_m^*
Packit 67cb25
Packit 67cb25
   assuming that the true minimum :math:`x_m^*` is contained within the interval.
Packit 67cb25
Packit 67cb25
Minimization Algorithms
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
The minimization algorithms described in this section require an initial
Packit 67cb25
interval which is guaranteed to contain a minimum---if :math:`a` and
Packit 67cb25
:math:`b` are the endpoints of the interval and :math:`x` is an estimate
Packit 67cb25
of the minimum then :math:`f(a) > f(x) < f(b)`.  This ensures that the
Packit 67cb25
function has at least one minimum somewhere in the interval.  If a valid
Packit 67cb25
initial interval is used then these algorithm cannot fail, provided the
Packit 67cb25
function is well-behaved.
Packit 67cb25
Packit 67cb25
.. type:: gsl_min_fminimizer_type
Packit 67cb25
Packit 67cb25
   .. index::
Packit 67cb25
      single: golden section algorithm for finding minima
Packit 67cb25
      single: minimum finding, golden section algorithm
Packit 67cb25
Packit 67cb25
   .. var:: gsl_min_fminimizer_goldensection
Packit 67cb25
Packit 67cb25
      The *golden section algorithm* is the simplest method of bracketing
Packit 67cb25
      the minimum of a function.  It is the slowest algorithm provided by the
Packit 67cb25
      library, with linear convergence.
Packit 67cb25
Packit 67cb25
      On each iteration, the algorithm first compares the subintervals from
Packit 67cb25
      the endpoints to the current minimum.  The larger subinterval is divided
Packit 67cb25
      in a golden section (using the famous ratio :math:`(3-\sqrt 5)/2 \approx 0.3819660`
Packit 67cb25
      and the value of the function at this new point is
Packit 67cb25
      calculated.  The new value is used with the constraint :math:`f(a') > f(x') < f(b')`
Packit 67cb25
      to a select new interval containing the minimum, by
Packit 67cb25
      discarding the least useful point.  This procedure can be continued
Packit 67cb25
      indefinitely until the interval is sufficiently small.  Choosing the
Packit 67cb25
      golden section as the bisection ratio can be shown to provide the
Packit 67cb25
      fastest convergence for this type of algorithm.
Packit 67cb25
Packit 67cb25
   .. index::
Packit 67cb25
      single: Brent's method for finding minima
Packit 67cb25
      single: minimum finding, Brent's method
Packit 67cb25
Packit 67cb25
   .. var:: gsl_min_fminimizer_brent
Packit 67cb25
Packit 67cb25
      The *Brent minimization algorithm* combines a parabolic
Packit 67cb25
      interpolation with the golden section algorithm.  This produces a fast
Packit 67cb25
      algorithm which is still robust.
Packit 67cb25
Packit 67cb25
      The outline of the algorithm can be summarized as follows: on each
Packit 67cb25
      iteration Brent's method approximates the function using an
Packit 67cb25
      interpolating parabola through three existing points.  The minimum of the
Packit 67cb25
      parabola is taken as a guess for the minimum.  If it lies within the
Packit 67cb25
      bounds of the current interval then the interpolating point is accepted,
Packit 67cb25
      and used to generate a smaller interval.  If the interpolating point is
Packit 67cb25
      not accepted then the algorithm falls back to an ordinary golden section
Packit 67cb25
      step.  The full details of Brent's method include some additional checks
Packit 67cb25
      to improve convergence.
Packit 67cb25
Packit 67cb25
   .. index:: safeguarded step-length algorithm
Packit 67cb25
Packit 67cb25
   .. var:: gsl_min_fminimizer_quad_golden
Packit 67cb25
Packit 67cb25
      This is a variant of Brent's algorithm which uses the safeguarded
Packit 67cb25
      step-length algorithm of Gill and Murray.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The following program uses the Brent algorithm to find the minimum of
Packit 67cb25
the function :math:`f(x) = \cos(x) + 1`, which occurs at :math:`x = \pi`.
Packit 67cb25
The starting interval is :math:`(0,6)`, with an initial guess for the
Packit 67cb25
minimum of :math:`2`.
Packit 67cb25
Packit 67cb25
.. include:: examples/min.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
Here are the results of the minimization procedure.
Packit 67cb25
Packit 67cb25
.. include:: examples/min.txt
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
References and Further Reading
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
Further information on Brent's algorithm is available in the following
Packit 67cb25
book,
Packit 67cb25
Packit 67cb25
* Richard Brent, *Algorithms for minimization without derivatives*,
Packit 67cb25
  Prentice-Hall (1973), republished by Dover in paperback (2002), ISBN
Packit 67cb25
  0-486-41998-3.