Convenience methods

pints.fmin(f, x0, args=None, boundaries=None, threshold=None, max_iter=None, max_unchanged=200, verbose=False, parallel=False, method=None)[source]

Minimises a callable function f, starting from position x0, using a pints.Optimiser.

Returns a tuple (xbest, fbest) with the best position found, and the corresponding value fbest = f(xbest).

Parameters:
  • f – A function or callable class to be minimised.
  • x0 – The initial point to search at. Must be a 1-dimensional sequence (e.g. a list or a numpy array).
  • args – An optional tuple of extra arguments for f.
  • boundaries – An optional pints.Boundaries object or a tuple (lower, upper) specifying lower and upper boundaries for the search. If no boundaries are provided an unbounded search is run.
  • threshold – An optional absolute threshold stopping criterium.
  • max_iter – An optional maximum number of iterations stopping criterium.
  • max_unchanged – A stopping criterion based on the maximum number of successive iterations without a signficant change in f (see pints.OptimisationController()).
  • verbose – Set to True to print progress messages to the screen.
  • parallel – Allows parallelisation to be enabled. If set to True, the evaluations will happen in parallel using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by setting parallel to an integer greater than 0.
  • method – The pints.Optimiser to use. If no method is specified, pints.CMAES is used.

Example

import pints

def f(x):
    return (x[0] - 3) ** 2 + (x[1] + 5) ** 2

xopt, fopt = pints.fmin(f, [1, 1])
pints.curve_fit(f, x, y, p0, boundaries=None, threshold=None, max_iter=None, max_unchanged=200, verbose=False, parallel=False, method=None)[source]

Fits a function f(x, *p) to a dataset (x, y) by finding the value of p for which sum((y - f(x, *p))**2) / n is minimised (where n is the number of entries in y).

Returns a tuple (xbest, fbest) with the best position found, and the corresponding value fbest = f(xbest).

Parameters:
  • f (callable) – A function or callable class to be minimised.
  • x – The values of an independent variable, at which y was recorded.
  • y – Measured values y = f(x, p) + noise.
  • p0 – An initial guess for the optimal parameters p.
  • boundaries – An optional pints.Boundaries object or a tuple (lower, upper) specifying lower and upper boundaries for the search. If no boundaries are provided an unbounded search is run.
  • threshold – An optional absolute threshold stopping criterium.
  • max_iter – An optional maximum number of iterations stopping criterium.
  • max_unchanged – A stopping criterion based on the maximum number of successive iterations without a signficant change in f (see pints.OptimisationController()).
  • verbose – Set to True to print progress messages to the screen.
  • parallel – Allows parallelisation to be enabled. If set to True, the evaluations will happen in parallel using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by setting parallel to an integer greater than 0.
  • method – The pints.Optimiser to use. If no method is specified, pints.CMAES is used.
Returns:

  • xbest (numpy array) – The best parameter set obtained.
  • fbest (float) – The corresponding score.

Example

import numpy as np
import pints

def f(x, a, b, c):
    return a + b * x + c * x ** 2

x = np.linspace(-5, 5, 100)
y = f(x, 1, 2, 3) + np.random.normal(0, 1)

p0 = [0, 0, 0]
popt = pints.curve_fit(f, x, y, p0)