lib5c.util.optimization module

Module containing utility functions for curve optimization and root finding.

lib5c.util.optimization.array_newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None, failure_idx_flag=None)[source]

This function is deprecated, with scipy>=1.2.0, you can call scipy.optimize.newton() instead.

Finds roots of a scalar function func given a vector of initial guesses x0 and parallel vectors of additional arguments to func (passed in args) in a vectorized fashion. Similar to calling scipy.optimize.newton() in a for loop, but more performant and more legible. Bootlegged from https://github.com/scipy/scipy/pull/8357 preceeding its official release.

Parameters
  • func (function) – The scalar function to minimize. Should be vectorized (when a vector of independent inputs is passed it should return a vector of independent outputs). Signature should be func(x, *args) where x0 contains initial guesses for x and args represents the additional arguments.

  • x0 (np.ndarray) – Initial guesses.

  • fprime (function, optional) – The derivative of func. If not passed, this will be estimated with the secant method.

  • args (tuple) – Extra arguments to be passed to func.

  • tol (float) – The allowable error of the zero value.

  • maxiter (int) – Maximal number of iterations.

  • fprime2 (function, optional) – The second derivative of func. If passed, Halley’s method will be used. If not passed, the normal Newton-Raphson method or the secant method is used.

  • failure_idx_flag (bool, optional) – Pass True to return two extra boolean arrays specifying which optimizations failed or encountered zero derivatives, respectively.

Returns

  • root (np.ndarray) – The identified zeros of func.

  • failures (np.ndarray of bool, optional) – Only returned if failure_idx_flag is True. Indicates which elements failed to converge.

  • zero_der (np.ndarray of bool, optional) – Only returned if failure_idx_flag is True. Indicates which elements had a zero derivative.

lib5c.util.optimization.quadratic_log_log_fit(x, y)[source]

Fit a pure-quadratic function y = a * x**2 using a loss function in log-log space.

Parameters
  • x (np.ndarray) – Flat vector of x values to fit.

  • y (np.ndarray) – Flat vector of y values to fit.

Returns

The fitted function.

Return type

np.poly1d