dosma.curve_fit

dosma.curve_fit(func, x, y, y_bounds=None, p0=None, maxfev=100, ftol=1e-05, eps=1e-08, show_pbar=False, num_workers=0, chunksize: Optional[int] = None, **kwargs)[source]

Use non-linear least squares to fit a function func to data.

Uses scipy.optimize.curve_fit() backbone.

Parameters
  • func (callable) – The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

  • x (ndarray) – The independent variable(s) where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.

  • y (ndarray) – The dependent data, a length M array - nominally func(xdata, …) - or an (M,N)-shaped array for N different sequences.

  • y_bounds (tuple, optional) – Lower and upper bound on y values. Defaults to no bounds. Sequences with observations out of this range will not be processed.

  • p0 (Number | Sequence[Number] | ndarray | Dict, optional) – Initial guess for the parameters. If sequence (e.g. list, tuple, 1d ndarray), it should have length P, which is the number of parameters. If this is a 2D numpy array, it should have a shape (N, P). If None, then the initial values will all be 1.

  • maxfev (int, optional) – Maximum number of function evaluations before the termination. If bounds argument for scipy.optimize.curve_fit is specified, this corresponds to the max_nfev in the least squares algorithm

  • ftol (float) – Tolerance for termination by the change of the cost function. See scipy.optimize.least_squares for more details.

  • eps (float, optional) – Epsilon for computing r-squared.

  • show_pbar (bool, optional) – If True, show progress bar. Note this can increase runtime slightly when using multiple workers.

  • num_workers (int, optional) – Maximum number of workers to use for fitting.

  • chunksize (int, optional) – Size of chunks sent to worker processes when num_workers > 0. When show_pbar=True, this defaults to the standard value in tqdm.concurrent.process_map().

  • kwargs – Keyword args for scipy.optimize.curve_fit.

Returns

popts (ndarray): A NxP matrix of fitted values. The last dimension (axis=-1) corresponds to the different parameters (in order).

rsquared (ndarray): A (N,) length matrix of r-squared goodness-of-fit values.

Return type

Tuple[ndarray, ndarray]