bfit.greedy

Greedy Fitting Module.

Module Contents

Classes

GreedyLeastSquares

Optimize Least-Squares using Greedy and ScipyFit methods.

GreedyKLSCF

Optimize Kullback-Leibler using the Greedy method and self-consistent method.

class bfit.greedy.GreedyLeastSquares(grid, density, choice='pick-one', local_tol=1e-05, global_tol=1e-08, method='SLSQP', normalize=False, integral_dens=None, with_constraint=False, maxiter=1000, spherical=False)[source]

Bases: GreedyStrategy

Optimize Least-Squares using Greedy and ScipyFit methods.

Construct the GreedyLestSquares object.

Parameters
  • grid ((_BaseRadialGrid, CubicGrid)) – Grid class that contains the grid points and integration methods on them.

  • density (ndarray) – The true density evaluated on the grid points.

  • choice (str, optional) –

    Determines how the next set of basis-functions are chosen. Can be either:

    • pick-one : add a new basis-function by taking the average between every two s-type or p-type exponents.

    • pick-two : add two new basis-functions by recursion of “pick-one” over each guess from pick-one.

    • pick-two-lose-one : add new basis-function by iterating though each guess in pick-two and removing one basis-function, generating a new guess each time.

  • local_tol (float, optional) – The tolerance for convergence of scipy.optimize method for optimizing each local guess. Should be larger than global_tol.

  • global_tol (float, optional) – The tolerance for convergence of scipy.optimize method for further refining/optimizing the best local guess found out of all choices. Should be smaller than local_tol.

  • method (str, optional) – The method used for optimizing parameters. Default is “slsqp”. See “scipy.optimize.minimize” for options.

  • normalize (bool, optional) – Whether to fit with a normalized s-type and p-type Gaussian model.

  • integral_dens (float, optional) – If this is provided, then the model is constrained to integrate to this value. If not, then the model is constrained to the numerical integration of the density. Useful when one knows the actual integration value of the density.

  • with_constraint (bool) – If true, then adds the constraint that the integration of the model density must be equal to the constraint of true density. The default is True.

  • maxiter (int, optional) – Maximum number of iterations when optimizing an initial guess in the scipy.optimize method.

  • spherical (bool) – Whether to perform spherical integration by adding \(4 \pi r^2\) term to the integrand when calculating the objective function. Only used when grid is one-dimensional and positive (radial grid).

property local_tol(self)[source]

Return local tolerance for convergence in scipy.optimize.

property global_tol(self)[source]

Return global tolerance for convergence in scipy.optimize.

property maxiter(self)[source]

Return maximum number of iterations in optimization routine.

property with_constraint(self)[source]

Return constraint that integral of model should equal integral of density.

get_best_one_function_solution(self)[source]

Obtain the best s-type function solution to least-squares using different weights.

static optimize_using_nnls(true_dens, cofactor_matrix)[source]

Solve for the coefficients using non-linear least squares.

get_optimization_routine(self, params, local=False)[source]

Optimize least-squares using nnls and scipy.optimize from ScipyFit.

property numb_func_increase(self)[source]

Return number of basis-functions to add in each iteration.

property density(self)[source]

Return density that is fitted to.

property grid(self)[source]

Return grid class object.

property model(self)[source]

Return model class.

property integral_dens(self)[source]

Return the integral of the density.

eval_obj_function(self, params)[source]

Return evaluation the objective function.

store_errors(self, params)[source]

Store errors inside the attribute err_arr.

run(self, factor, d_threshold=1e-08, max_numb_funcs=30, add_extra_choices=None, disp=False)[source]

Add new Gaussians to fit to a density until convergence is achieved.

Initially, the algorithm solves for the best one-function basis-function. Then it generates a group of initial guesses of size (1 + C) based on the previous optimal fit. It then optimizes each initial guess from that group, with a small threshold for convergence. It takes the best found initial guess from that set and optimizes it further. Then this process repeats for (1 + 2C) basis-functions until convergence or termination criteria is met.

Parameters
  • factor (float) – The factor that is used to generate new initial guess.

  • d_threshold (float) – The convergence threshold for the objective function being minimized.

  • max_numb_funcs (int) – Maximum number of basis-functions to have.

  • add_extra_choices (callable(List, List[List])) – Function that returns extra initial guesses to add. Input must be the model parameters and the output should be a list of initial guesses that should match attribute numb_func_increase.

  • disp (bool) – Whether to display the output.

Returns

result – The optimization results presented as a dictionary containing:

”coeffs”ndarray

The optimized coefficients of Gaussian model.

”exps”ndarray

The optimized exponents of Gaussian model.

”num_s”int

Number of s-type Gaussian functions.

”num_p”int

Number of p-type Gaussian functions.

”success”: bool

Whether or not the optimization exited successfully.

”fun”float

Objective function at the last iteration.

”performance”ndarray

Values of various performance measures of modeled density at each iteration, as computed by goodness_of_fit() method.

”parameters_iteration”: List

List of the optimal parameters of each iteration.

”exit_information”: str

Information about termination of the greedy algorithm.

Return type

dict

class bfit.greedy.GreedyKLSCF(grid, density, choice='pick-one', g_eps_coeff=0.0001, g_eps_exp=1e-05, g_eps_obj=1e-10, l_eps_coeff=0.01, l_eps_exp=0.001, l_eps_obj=1e-08, mask_value=1e-12, integral_dens=None, maxiter=1000, spherical=False)[source]

Bases: GreedyStrategy

Optimize Kullback-Leibler using the Greedy method and self-consistent method.

Construct the GreedyKLSCF object.

Parameters
  • grid ((_BaseRadialGrid, CubicGrid)) – Grid class that contains the grid points and integration methods on them.

  • density (ndarray) – The true density evaluated on the grid points.

  • choice (str, optional) –

    Determines how the next set of basis-functions are chosen. Can be either:

    • pick-one : add a new basis-function by taking the average between every two s-type or p-type exponents.

    • pick-two : add two new basis-functions by recursion of “pick-one” over each guess from pick-one.

    • pick-two-lose-one : add new basis-function by iterating though each guess in pick-two and removing one basis-function, generating a new guess each time.

  • g_eps_coeff (float, optional) – The tolerance for convergence of coefficients in KL-SCF method for further refining and optimizing the best found local guess.

  • g_eps_exp (float, optional) – The tolerance for convergence of exponents in KL-SCF method for further refining/optimizing the best local guess found out of all choices.

  • g_eps_obj (float, optional) – The tolerance for convergence of objective function in KL-SCF method for further refining/optimizing the best local guess found out of all choices.

  • l_eps_coeff (float, optional) – The tolerance for convergence of coefficients in KL-SCF method for optimizing each local initial guess. Should be larger than g_eps_coeff.

  • l_eps_exp (float, optional) – The tolerance for convergence of exponents in KL-SCF method for optimizing each local initial guess. Should be larger than g_eps_exp.

  • l_eps_obj (float, optional) – The tolerance for convergence of objective function in KL-SCF method for optimizing each local initial guess. Should be larger than g_eps_obj.

  • mask_value (str, optional) – The method used for optimizing parameters. Default is “slsqp”. See “scipy.optimize.minimize” for options.

  • integral_dens (float, optional) – If this is provided, then the model is constrained to integrate to this value. If not, then the model is constrained to the numerical integration of the density. Useful when one knows the actual integration value of the density.

  • maxiter (int, optional) – Maximum number of iterations when optimizing an initial guess in the KL-SCF method.

  • spherical (bool) – Whether to perform spherical integration by adding \(4 \pi r^2\) term to the integrand when calculating objective function. Only used when grid is one-dimensional and positive (radial grid).

property l_threshold_coeff(self)[source]

Return local threshold for KL-SCF method for convergence of coefficients.

property l_threshold_exp(self)[source]

Return local threshold for KL-SCF method for convergence of exponents.

property l_threshold_obj(self)[source]

Return local threshold for KL-SCF method for convergence of objective function.

property g_threshold_coeff(self)[source]

Return global threshold for KL-SCF method for convergence of coefficients.

property g_threshold_exp(self)[source]

Return global threshold for KL-SCF method for convergence of exponents.

property g_threshold_obj(self)[source]

Return global threshold for KL-SCF method for convergence of objective function.

property maxiter(self)[source]

Return maximum iteration for KL-SCF method.

get_best_one_function_solution(self)[source]

Obtain the best one s-type function to Kullback-Leibler.

get_optimization_routine(self, params, local=False)[source]

Optimize KL using KL-SCF method.

property numb_func_increase(self)[source]

Return number of basis-functions to add in each iteration.

property density(self)[source]

Return density that is fitted to.

property grid(self)[source]

Return grid class object.

property model(self)[source]

Return model class.

property integral_dens(self)[source]

Return the integral of the density.

eval_obj_function(self, params)[source]

Return evaluation the objective function.

store_errors(self, params)[source]

Store errors inside the attribute err_arr.

run(self, factor, d_threshold=1e-08, max_numb_funcs=30, add_extra_choices=None, disp=False)[source]

Add new Gaussians to fit to a density until convergence is achieved.

Initially, the algorithm solves for the best one-function basis-function. Then it generates a group of initial guesses of size (1 + C) based on the previous optimal fit. It then optimizes each initial guess from that group, with a small threshold for convergence. It takes the best found initial guess from that set and optimizes it further. Then this process repeats for (1 + 2C) basis-functions until convergence or termination criteria is met.

Parameters
  • factor (float) – The factor that is used to generate new initial guess.

  • d_threshold (float) – The convergence threshold for the objective function being minimized.

  • max_numb_funcs (int) – Maximum number of basis-functions to have.

  • add_extra_choices (callable(List, List[List])) – Function that returns extra initial guesses to add. Input must be the model parameters and the output should be a list of initial guesses that should match attribute numb_func_increase.

  • disp (bool) – Whether to display the output.

Returns

result – The optimization results presented as a dictionary containing:

”coeffs”ndarray

The optimized coefficients of Gaussian model.

”exps”ndarray

The optimized exponents of Gaussian model.

”num_s”int

Number of s-type Gaussian functions.

”num_p”int

Number of p-type Gaussian functions.

”success”: bool

Whether or not the optimization exited successfully.

”fun”float

Objective function at the last iteration.

”performance”ndarray

Values of various performance measures of modeled density at each iteration, as computed by goodness_of_fit() method.

”parameters_iteration”: List

List of the optimal parameters of each iteration.

”exit_information”: str

Information about termination of the greedy algorithm.

Return type

dict