BayesicFitting

Model Fitting and Evidence Calculation

View project on GitHub



class AnnealingAmoeba( object )Source

Simulated annealing simplex finding minimum.

AnnealingAmoeba can be used in two modes: with simulated annealing on or off. The simulated annealing mode is invoked by setting the temperature to some value. By default it is off: temperature at zero.

When the temperature is set at zero (default), AnnealingAmoeba acts as a simple Nelder-Mead downhill simplex method. With two advantages and one disadvantage. The pro's are that it is reasonably fast and that it does not need partial derivatives. The con is that it will fall into the first local minimum it encounters. No guarantee that this minimum has anything to do with the absolute minimum. This T=0 modus can only be used in mono-modal problems.

In the other modus, when the temperature is set at some value the simplex sometimes takes a uphill step, depending on the temperature at that moment. Steps downhill are always taken. In that way it is possible to climb out of local minima to find better ones. Meanwhile the temperature is steadily lowered, concentrating the search on the by now hopefully found absolute minimum. Of course this takes much more iterations and still there is no guarantee that the best value is found. But a better chance. The initial temperature which suggests itself is of the order of the humps found in the minimizable lanscape.

At each temperature level a number of moves is made. This number is set by the keyword steps=10, by default. After these steps the temperature is lowered with a factor set by cooling=0.95, by default.

Iteration continues until the relative difference between the low and high points within the simplex is less than reltol
     |yhi - ylo| / ( |yhi| + |ylo| ) < reltol and/or the absolute difference is less than abstol
     |yhi - ylo| < abstol.

AnnealingAmoeba can be used with limits set to one or more of the input values.

The original version stems from Numerical Recipes with some additions of my own.

Author Do Kester

Attributes

  • func : callable
         function to be minimized of form : y = func( x )

  • lolimits : array_like
         lower limits on x. -inf is allowed to indicate no lower limit

  • hilimits : array_like
         upper limits on x. +inf is allowed to indicate no upper limit

  • fopt : float
         the best of the above values

  • xopt : ndarray
         copy of the simplex point that has the best value (nx)

  • rng : RandomState
         random number generator

  • seed : int
         seed of rng

  • reltol : float
         Relative tolerance. Program stops when ( |yhi-ylo| / (|yhi|+|ylo|) ) < reltol

  • abstol : float
         Absolute tolerance. Program stops when |yhi-ylo| < abstol

  • maxiter : int
         maximum number of iterations

  • iter : int (read only)
         iteration counter

  • ncalls : int (read only)
         numbers of calls to func

  • temp : float
         annealing temperature (default: 0)

  • cooling : float (non existent when temp=0)
         cooling factor (default: 0.95)

  • steps : int (non existent when temp=0)
         number of steps per cooling cycle (default: 10)

  • verbose : int
         0 : silent
         1 : print results to output
         2 : print some info every 100 iterations and plot results
         3 : print some info every iteration

  • callback : callable
         function to be called every iteration of form
         xopt = callback( xopt )

  • simplex : ndarray
         the simplex has shape = (nx+1, nx); nx is the size of x

  • values : ndarray
         the values of the function attained at the simplex points (nx+1).

  • sum : ndarray
         sum over the corners of the simplex (nx)

AnnealingAmoeba( func, xini, size=1, seed=4567, temp=0, limits=None, maxiter=1000, reltol=0.0001, abstol=0.0001, cooling=0.95, steps=10, verbose=0, callback=None )

Create a new AnnealingAmoeba class to minimize the function

Parameters

  • func : callable
         the function to be minimized
  • xini : array_like
         initial values of the function
  • size : float or array_like
         step size of the simplex
  • seed : int
         for random number generator
  • temp : float
         temperature of annealing (0 is no annealing)
  • limits : None or list of 2 floats or list of 2 array_like
         None : no limits applied
         [lo,hi] : low and high limits for all values
         [la,ha] : low array and high array limits for the values
  • maxiter : int
         max number of iterations
  • reltol : float, None
         Relative tolerance. Program stops when ( |hi-lo| / (|hi|+|lo|) ) < reltol
  • abstol : float, None
         Absolute tolerance. Program stops when |hi-lo| < abstol
         when abstol has a (float) value, reltol might be None.
  • cooling : float
         cooling factor when annealing
  • steps : int
         number of cycles in each cooling step.
  • verbose : int
         0 : silent
         1 : print results to output
         2 : print some info every 100 iterations and plot results
         3 : print some info every iteration
  • callback : callable
         is called each iteration as
         val = callback( val )
         where val is the minimizable array

Raises

ValueError
     1. When func is not callable
     2. When both tolerances are None
     3. When callback is not callable

makeSimplex( xini, step )
Make a simplex for the given set of parameters.

Parameters

  • xini : array_like
         initial (parameter) array
  • step : float
         size of the simplex

hasLowLimits( k )
Return True if it has low limits > -inf.

hasHighLimits( k )
Return True if it has high limits < inf.

stayInLimits( oldpar, trypar )
Keep the parameters within the limits.

Parameters

  • oldpar : array_like
         previous set of parameters (assumed to be within limits)
  • trypar : array_like
         new parameters, possibly out of limits

Returns

  • newpar : array_like
         new parameters, within limits

checkSimplex( simplex )
Check for degeneracy: all points on same location.

Parameters

  • simplex : matrix
         the simplex of amoeba

setValues( )
Calculate the function values a simplex's corners

minimize( )
Converge the simplex.

Returns

  • ndarray : the optimal x values.

Raises

ConvergenceError when too many iterations are needed.

temperatureStep( )
Perform simplex moves in the right direction.

Returns

  • int : number of transforms.

doVerbose( name, chisq, par, verbose=0 )

randomRange( factor )

inflateSimplex( ilo, factor )
Inflate/deflate simplex around the (lowest) point (ilo).

inflate if factor > 1 deflate if factor < 1 mirror if factor < 0

Parameters

  • ilo : int
         lowest point in the simplex
  • factor : float
         inflation factor

trialStep( ihi, yhi, factor )
Do a trial step to improve the worst (highest) point.

Parameters

  • ihi : int
         index of the high point
  • yhi : int
         value at the high point
  • factor : int
         step size

logRanTemp( )