How to do it...

We present the main functions and parameters:

The parameters are:

  • f: 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.
  • xdata: An M-length sequence or an (k,M)-shaped array for functions with k predictors. The independent variable where the data is measured.
  • ydata: M-length sequence. The dependent data—nominally f(xdata, ...).
  • p0: None, scalar, or N-length sequence, optional. Initial guess for the parameters. If none, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).
  • sigma: None or M-length sequence or M x M array, optional. Determines the uncertainty in ydata. If we define residuals as r = ydata - f(xdata, *popt), then the interpretation of sigma depends on its number of dimensions:
  • A 1D sigma should contain values of standard deviations of errors in ydata. In this case, the optimized function is chisq = sum((r / sigma) ** 2).A 2D sigma should contain the covariance matrix of errors in ydata. In this case, the optimized function is chisq = r.T @ inv(sigma) @ r
  • New in version 0.19—None (default) is equivalent of 1D sigma filled with ones.
  • absolute_sigma: bool, optional. If True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values.
  • If False, only the relative magnitudes of the sigma values matter. The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor. This constant is set by demanding that the reduced chisq for the optimal parameters popt when using the scaled sigma equals unity. In other words, sigma is scaled to match the sample variance of the residuals after the fit.
  • Mathematically, pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N).
  • check_finite: bool, optional. If True, check that the input arrays do not contain nans of infs, and raise a ValueError if they do. Setting this parameter to False may silently produce nonsensical results if the input arrays do contain nans. The default is True.
  • bounds: Two-tuple of array_like, optional. Lower and upper bounds on independent variables. Defaults to no bounds. Each element of the tuple must be either an array with the length equal to the number of parameters, or a scalar (in which case the bound is taken to be the same for all parameters). Use np.inf with an appropriate sign to disable bounds on all or some parameters.
  • New in version 0.17—method: {'lm', 'trf', 'dogbox'}, optional. Method to use for optimization. See least_squares for more details. Default is lm for unconstrained problems and trf if bounds are provided. The method lm won't work when the number of observations is less than the number of variables, use trf or dogbox in this case.
  • New in version 0.17—jac : Callable, string or None, optional. Function with signature jac(x, ...) which computes the Jacobian matrix of the model function with respect to parameters as a dense array_like structure. It will be scaled according to provided sigma. If None (default), the Jacobian will be estimated numerically. String keywords for the trf and dogbox methods can be used to select a finite difference scheme, see least_squaresNew in version 0.18—kwargsKeyword arguments passed to leastsqfor method='lm' or least_squares otherwise.

The returns are:

  • popt: Array. Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized.
  • pcov: 2D array. The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).
  • How the sigma parameter affects the estimated covariance depends on the absolute_sigma argument, as described.
  • If the Jacobian matrix of the solution doesn't have a full rank, then the lm method returns a matrix filled with np.inf. On the other hand, trf and dogbox methods use Moore-Penrose pseudoinverse to compute the covariance matrix.

The raises are:

  • ValueError: If either ydata or xdata contain NaN values, or if incompatible options are used.
  • RuntimeErrorIf the least-squares minimization fails.
  • OptimizeWarningIf covariance of the parameters cannot be estimated.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset