sfit_minimizer.sfit_classes module

class sfit_minimizer.sfit_classes.SFitResults(func, success=None, msg=None, iterations=None, fun=None)

Bases: object

Results of the minimization algorithm. Modeled after scipy.optimize.OptimizeResult.

Arguments:

func: SFitFunction

Keywords:
success: bool

see success

msg: str

see msg

property x

Best-fit (last calculated) value of the parameters.

property fun

final function evaluation, e.g., chi2

property sigmas

Uncertainties for x

property success

True = algorithm completed successfully. False = algorithm ran into a limit or failure mode.

property msg

If success == False, some information about the failure mode.

property nit

Total number of iterations executed.

class sfit_minimizer.sfit_classes.SFitFunction(data=None, theta=None)

Bases: object

Main class for sfit minimization routine. Establishes all the necessary functions for executing the A. Gould algorithm. At a minimum, the user should specify:

Arguments:
data: np.array with shape (N, 3)

an array of the data to be fitted with columns (x, y, yerr).

theta: list of length M

trial values for the parameters of the function to be fit.

reset_all()
property theta

list or np.array

Parameters of the function being fit to the data. Setting this parameter will reset all other parameters to None. Use update_all() to update everything.

update_all(theta=None, verbose=False)

Recalculate all of the data properties with respect to the new model parameters.

Keywords:
theta0: list of length M, optional

new trial values for the parameters of the function to be fit. If not provided, recalculate using the current value of theta.

verbose: bool, optional

Default is False. If True, prints output after each stage for debugging.

property ymod

np.array of shape (N), where each element k is the value of the model evaluated at data[k, 0].

calc_model()

FUNCTION SPECIFIC: Calculate expected values of the model. May be explicitly defined for a specific class that inherits SFitFunction. Only used to calculate ymod, so calc_residuals() may be defined instead.

sets ymod

property residuals

np.array of shape (N), where each element k = :py:attr: data[k, 1] - ~ymod [k]

calc_residuals()

Difference between the data and the model. Either this OR calc_model() should be explicitly defined for a specific class that inherits SFitFunction.

sets residuals

property chi2

float

chi2 of the model with respect to the data. chi2 = Sum_k (residuals[k]^2) / data[k, 2]^2

calc_chi2()

Calculate the chi2 of the data relative to the model.

sets chi2

get_chi2()

Calculate and return the chi2 of the data w.r.t. the model. See calc_chi2().

property df

np.array of shape (M, N), where df[i, k] returns the partial derivative of the fitting function with respect to parameter i evaluated at point k. shape = (len(theta), len(data))

calc_df()

FUNCTION SPECIFIC: Calculate the derivatives of the fitting function relative to each parameter theta and store as self.df. Should be explicitly defined for a specific class that inherits SFitFunction.

sets df

property sigmas

np.array of length M

Uncertainty in each parameter theta: sigma_theta_i = sqrt(c_ii)

calc_sigmas()

Calculate the uncertainty in each parameter theta.

get_sigmas()

Calculate and return the uncertainty in each parameter theta. see calc_sigmas()

property step

np.array of shape (M)

Full proposed step size for each parameter theta_i:

theta_new_i = theta_old_i + step_i where step_i = sum_j c_ij * d_j.

calc_step()

Calculate step.

get_step()

Calculate and return the full step for each parameter theta_i. See calc_step().

property dchi2

np.array of shape (M, N)

Partial derivatives of the chi2 with respect to the parameters, calculated at each data point:

dchi2_i,k = -2 * residuals * df / data[k, 2]**2

shape = (len(theta), len(data))

calc_dchi2()

Calculate the gradient of chi2 (dchi2) at each datapoint.

get_dchi2()

Calculate and return the partial derivatives of the fitting function. See calc_dchi2().

get_partials()

Alternative to get_dchi2().

property dvec

np.array of shape (M)

d vector from sfit, used to calculate the step size:

d_i = - Sum_k (partial chi2 / partial theta_i) / 2

shape = ( len(theta) )

calc_dvec()

Calculate the d vector and store it as dvec.

get_dvec()

Calculate and return the d vector. See calc_dvec().

property bmat

np.array of shape (M, M)

b matrix from sfit. invert to find c matrix (cmat) used for stepping and finding sigmas:’

b_ij = Sum_k ( (partial F / partial theta_i)(partial F/partial theta_j) / data[k, 2]^2 )

where F is the fitting function and data[k, 2] are the errors. shape = ( len(theta), len(theta) ).

calc_bmat()

Calculate the b matrix (bmat).

get_bmat()

Calculate and return the b matrix. See calc_bmat().

property cmat

np.array of shape (M, M)

c matrix from sfit:

c = inv(bmat)

shape = ( len(theta), len(theta) )

calc_cmat()

Invert the b matrix (bmat) to find the c matrix (cmat).

get_cmat()

Calculate and return the c matrix. See calc_cmat().