sluyspy.fit module
Fitting functions for the sluyspy package
- sluyspy.fit.correlation_matrix_from_variance_covariance_matrix(var_cov)[source]
Compute the normalised correlation matrix from a variance-covariance matrix.
- Parameters:
var_cov (float) – 2D NumPy array containing the variance-covariance matrix.
- Returns:
2D NumPy array containing the normalised-correlation matrix.
- Return type:
(float)
- sluyspy.fit.np_polyfit_chi2(xvals, yvals, order, ysigmas=None, verbosity=0)[source]
- Wrapper for NumPy polyfit, that returns the fitting coefficients and reduced chi squared.
Print details if desired.
Note: does not compute the uncertainties in the coefficients; use scipy_curvefit_chi2() for that.
- Parameters:
xvals (float) – X values.
yvals (float) – Y values.
order (int) – Polynomial order for fit.
ysigmas (float) – Uncertainties (errors, standard deviations) in the y values; defaults to None -> all sigmas=1.
verbosity (int) – Verbosity: 0: quiet, 1: print red.chi2, 2: print more, 3: print table, 4: print all; defaults to 0.
- Returns:
Tuple consisting of (coefs, red_chi2):
coefs (float): Array of fitting coefficients, with length (order+1).
red_chi2 (float): Reduced chi squared (chi^2 / (n-m)) for the fit.
- Return type:
(tuple)
- sluyspy.fit.polynomial(x, *coefs)[source]
Return a polynomial y = Sum_i=0^N-1 coef_i x^i.
- Parameters:
x (float) – X value(s).
coefs (float) – Array of polynomial coefficients. The polynomial order is defined by the number of coefficients in the array.
- Returns:
Polynomial value.
- Return type:
(float)
- sluyspy.fit.print_fit_details(fittype, coefs, xvals, yvals, ysigmas, dcoefs=None, var_cov=None, fit_fun=None, yfit=None, verbosity=2, abs_diff=True, rel_diff=True, sigdig=6, coef_names=None, coef_facs=None, rev_coefs=None, yprintfac=1, rel_as_pct=False)[source]
Compute and return the reduced chi^2, and print other fit details if desired.
Calculations are done without fitting, so that this function can be called after a fit. The fit values will be computed from coefs, xvals using fit_fun if specified. In fact, two series of yvals (‘true’ and ‘fit’) can be specified without any fitting ever, by setting fittype=coefs=xvals=None and specifying yfit. This will print statistics on the comparison of the two data sets, similar to those after a fit. If xvals is specified, a bit more detail can be printed.
- Parameters:
fittype (str) – Type of fit: ‘np_polyfit’, ‘scipy_curvefit’ or None.
coefs (float) – Array with fit coefficients.
xvals (float) – Array with x values.
yvals (float) – Array with y values.
ysigmas (float) – Array with y sigmas.
dcoefs (float) – Uncertainties in coefficients from scipy_curvefit; optional.
var_cov (float) – Variance-covariance matrix in 2D NumPy array; optional.
fit_fun (fun) – Fit function used for scipy_curvefit; optional.
yfit (float) – “Fit” values for Y for fittype=None; optional.
verbosity (int) – Verbosity (0-3); optional - defaults to 2.
abs_diff (bool) – Print absolute differences (optional; defaults to True).
rel_diff (bool) – Print relative differences (optional; defaults to True).
sigdig (int) – Number of significant digits to print (optional, defaults to 6).
coef_names (str) – Array of coefficient names for printing (optional).
coef_facs (float) – Array of coefficient multiplication factors for printing (optional; defaults to 1). Useful for e.g. printing degrees when radians are fitted.
rev_coefs (bool) – Reverse printing order in coefficient table: last coefficient at top. Optional, defaults to True for fittype np_polyfit, to False otherwise.
yprintfac (float) – Multiplication factor when printing absolute differences in y (optional, defaults to 1).
rel_as_pct (bool) – Print relative difference as percentage rather than fraction (optional, defaults to False).
- Returns:
Reduced Chi^2.
- Return type:
(float)
Note
If fittype = ‘scipy_curvefit’, fit_fun must be specified.
If fittype is None, yfit values must be specified, since they will not be computed from xvals and coefs. In that case, coefs=xvals=None can be specified.
- sluyspy.fit.scipy_curvefit_chi2(fit_fun, xvals, yvals, coefs0, ysigmas=None, verbosity=0)[source]
- Wrapper for SciPy’s curve_fit, that returns the fitting coefficients and reduced chi^2.
Print details if desired.
- Parameters:
fit_fun (function) – Fitting function, should look like def fit_fun(x, a,b,c): and return y=f(x, a,b,c).
xvals (float) – Array containing x values to fit.
yvals (float) – Array containing y values to fit.
coefs0 (float) – Array with initial guess for fitting coefficients.
ysigmas (float) – Array containing y sigmas/uncertainties.
verbosity (int) – Verbosity to stdout (0-4).
- Returns:
Tuple containing (coefs, dcoefs, red_chi2, var_cov, ier):
coefs (float): Array containing the final fitting coefficients.
dcoefs (float): Array containing uncertainties on the fitting coefficients.
red_chi2 (float): Reduced chi squared (chi2/(n-m)).
var_cov (float): 2D array containing the variance-covariance matrix.
ier (int): Return value, >0 if the fit succeeded.
- Return type:
(tuple)