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 positionx0, using apints.Optimiser.Returns a tuple
(x_best, f_best)with the best position found, and the corresponding valuef_best = f(x_best).- 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.Boundariesobject 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(seepints.OptimisationController()).verbose – Set to
Trueto 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 settingparallelto an integer greater than 0.method – The
pints.Optimiserto use. If no method is specified,pints.CMAESis 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 ofpfor whichsum((y - f(x, *p))**2) / nis minimised (wherenis the number of entries iny).Returns a tuple
(x_best, f_best)with the best position found, and the corresponding valuef_best = f(x_best).- Parameters:
f (callable) – A function or callable class to be minimised.
x – The values of an independent variable, at which
ywas recorded.y – Measured values
y = f(x, p) + noise.p0 – An initial guess for the optimal parameters
p.boundaries – An optional
pints.Boundariesobject 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(seepints.OptimisationController()).verbose – Set to
Trueto 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 settingparallelto an integer greater than 0.method – The
pints.Optimiserto use. If no method is specified,pints.CMAESis used.
- Returns:
x_best (numpy array) – The best parameter set obtained.
f_best (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)