Fitting Module
The fitting module contains all functions and utilities for performing curve fitting.
Fitting Functions
Main fitting functions package (re-exports from fitting.functions).
Fitting functions and operations for curve fitting.
This package re-exports all model functions and fitting wrappers from fitting.functions so that existing code using getattr(fitting_functions, name) continues to work.
- Provides:
Mathematical functions for curve fitting (linear_function, sin_function, etc.)
High-level fitting wrappers (fit_linear_function, fit_sin_function, etc.)
Factory functions to generate custom fitting functions dynamically
Mathematical Functions
The fitting.functions subpackage contains mathematical model functions organized by family.
Base Types
Shared types and imports for fitting functions.
Polynomial Functions
Polynomial and linear fitting functions.
- fitting.functions.polynomials.generate_polynomial_function(parameters)[source]
Generate a polynomial function dynamically based on which parameters are enabled.
- Parameters:
parameters – List of booleans indicating which polynomial terms to include. Index i corresponds to the coefficient of t^i.
- Returns:
A function that takes t and the enabled coefficients as arguments.
- fitting.functions.polynomials.linear_function_with_n(t, *coeffs)
- fitting.functions.polynomials.linear_function(t, *coeffs)
- fitting.functions.polynomials.quadratic_function_complete(t, *coeffs)
- fitting.functions.polynomials.quadratic_function(t, *coeffs)
- fitting.functions.polynomials.fourth_power(t, *coeffs)
- fitting.functions.polynomials.fit_linear_function_with_n(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a linear model with intercept, \(y = m x + n\).
- Parameters:
data – Data source (dictionary or DataFrame) containing
x,yand their uncertainties (ux,uy).x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional list overriding the automatically estimated
[n, m];Noneentries keep the estimate.bounds_override – Optional pair
(lower, upper)with bounds for[n, m];Noneentries keep the estimator bounds.
- Returns:
Tuple
(text, y_fitted, equation)as produced byfitting.fitting_utils.generic_fit().
- fitting.functions.polynomials.fit_linear_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a linear model through the origin, \(y = m x\).
- Parameters:
data – Data source with columns for
x,yand their uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional list overriding the estimated slope
[m];Nonekeeps the estimate.bounds_override – Optional pair
(lower, upper)with bounds for[m].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.polynomials.fit_quadratic_function_complete(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a full quadratic model \(y = c x^2 + b x + a\).
- Parameters:
data – Data source with
x,yand their uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b, c].bounds_override – Optional bounds for
[a, b, c].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.polynomials.fit_quadratic_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a pure quadratic model through the origin, \(y = a x^2\).
- Parameters:
data – Data source with
x,yand their uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional override for
[a].bounds_override – Optional bounds for
[a].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.polynomials.fit_fourth_power(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a fourth‑power model through the origin, \(y = a x^4\).
- Parameters:
data – Data source with
x,yand their uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional override for
[a].bounds_override – Optional bounds for
[a].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
Trigonometric Functions
Trigonometric and hyperbolic fitting functions.
- fitting.functions.trigonometric.generate_trigonometric_function(func_type, with_phase=False)[source]
Generate a trigonometric function dynamically based on the function type.
- Parameters:
func_type – Type of function (‘sin’, ‘cos’, ‘sinh’, ‘cosh’).
with_phase – Whether to include a phase shift parameter c.
- Returns:
A function that takes t and parameters (a, b, [c]) as arguments.
- fitting.functions.trigonometric.sin_function(t, a, b)
- fitting.functions.trigonometric.sin_function_with_c(t, a, b, c)
- fitting.functions.trigonometric.cos_function(t, a, b)
- fitting.functions.trigonometric.cos_function_with_c(t, a, b, c)
- fitting.functions.trigonometric.sinh_function(t, a, b)
- fitting.functions.trigonometric.cosh_function(t, a, b)
- fitting.functions.trigonometric.tan_function_with_c(t, a, b, c)[source]
Tangent function with phase: y = a * tan(b*t + c)
- fitting.functions.trigonometric.fit_sin_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a sine model without phase, \(y = a \sin(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_sin_function_with_c(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a sine model with phase, \(y = a \sin(b x + c)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b, c].bounds_override – Optional bounds for
[a, b, c].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_cos_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a cosine model without phase, \(y = a \cos(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_cos_function_with_c(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a cosine model with phase, \(y = a \cos(b x + c)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b, c].bounds_override – Optional bounds for
[a, b, c].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_sinh_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a hyperbolic sine model, \(y = a \sinh(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_cosh_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a hyperbolic cosine model, \(y = a \cosh(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_tan_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a tangent model without phase, \(y = a \tan(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.trigonometric.fit_tan_function_with_c(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a tangent model with phase, \(y = a \tan(b x + c)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b, c].bounds_override – Optional bounds for
[a, b, c].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
Inverse Functions
Inverse and logarithmic fitting functions.
- fitting.functions.inverse.generate_inverse_function(power)[source]
Generate an inverse power function dynamically based on the power.
- Parameters:
power – The power of t in the denominator (e.g., 1 for 1/t, 2 for 1/t^2).
- Returns:
A function that takes t and coefficient a as arguments.
- fitting.functions.inverse.inverse_function(t, a)
- fitting.functions.inverse.inverse_square_function(t, a)
- fitting.functions.inverse.fit_ln_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a logarithmic model \(y = a \ln(x)\).
- Parameters:
data – Data source with positive
xvalues,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional override for the coefficient
[a].bounds_override – Optional bounds for
[a].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.inverse.fit_inverse_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit an inverse model \(y = a / x\).
- Parameters:
data – Data source with non‑zero
xvalues,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional override for
[a].bounds_override – Optional bounds for
[a].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.inverse.fit_inverse_square_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit an inverse‑square model \(y = a / x^2\).
- Parameters:
data – Data source with non‑zero
xvalues,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional override for
[a].bounds_override – Optional bounds for
[a].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
Special Functions
Gaussian, exponential, binomial, square pulse, and Hermite fitting functions.
- fitting.functions.special.fit_gaussian_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a Gaussian (normal) model \(y = A \exp(-(x-\mu)^2 / (2\sigma^2))\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[A, μ, σ].bounds_override – Optional bounds for
[A, μ, σ].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.special.fit_exponential_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit an exponential model \(y = a \exp(b x)\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b].bounds_override – Optional bounds for
[a, b].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.special.fit_binomial_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a logistic (binomial‑type) model \(y = a / (1 + \exp(-b (x - c)))\).
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[a, b, c].bounds_override – Optional bounds for
[a, b, c].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.special.fit_square_pulse_function(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a smooth square‑pulse model in time.
The underlying function approximates a finite‑width pulse centered at
t0with widthwand amplitudeAusing hyperbolic tangents.- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[A, t0, w].bounds_override – Optional bounds for
[A, t0, w].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.special.fit_hermite_polynomial_3(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a Hermite polynomial expansion up to degree 3.
The model is \(y = c_0 H_0(x) + c_1 H_1(x) + c_2 H_2(x) + c_3 H_3(x)\), where \(H_k\) are physicists’ Hermite polynomials.
- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[c0, c1, c2, c3].bounds_override – Optional bounds for
[c0, c1, c2, c3].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
- fitting.functions.special.fit_hermite_polynomial_4(data, x_name, y_name, initial_guess_override=None, bounds_override=None)[source]
Fit a Hermite polynomial expansion up to degree 4.
The model extends
fit_hermite_polynomial_3()with an additional \(c_4 H_4(x)\) term.- Parameters:
data – Data source with
x,yand uncertainties.x_name – Name of the independent variable column.
y_name – Name of the dependent variable column.
initial_guess_override – Optional overrides for
[c0, c1, c2, c3, c4].bounds_override – Optional bounds for
[c0, c1, c2, c3, c4].
- Returns:
Tuple
(text, y_fitted, equation)fromgeneric_fit().
Fitting Utilities
Fitting utilities for RegressionLab.
This module provides parameter formatting, equation resolution, and integration with fitting functions (initial guesses, bounds, fit execution).
- fitting.fitting_utils.format_scientific(value, fmt='.4g')[source]
Format a number using superscript notation instead of E/e.
- fitting.fitting_utils.format_parameter(value, sigma)[source]
Format parameter value and uncertainty with robust fallback behavior.
- fitting.fitting_utils.generic_fit(data, x_name, y_name, fit_func, param_names, equation_template, initial_guess=None, bounds=None, equation_formula=None)[source]
Generic fitting function that performs curve fitting with any model.
- fitting.fitting_utils.get_equation_param_info(equation_name)[source]
Get parameter names and display formula for a given equation type.
- fitting.fitting_utils.get_equation_format_for_function(function_name)[source]
Return the format template for the given fit function name/path.
- fitting.fitting_utils.get_equation_param_names_for_function(function_name)[source]
Return parameter names for the given fit function from equations config.
- fitting.fitting_utils.merge_initial_guess(computed, override)[source]
Merge computed initial guesses with user overrides.
Custom Function Evaluator
Custom Function Evaluator Module.
This module provides safe runtime evaluation of custom mathematical functions for curve fitting.
- class fitting.custom_function_evaluator.CustomFunctionEvaluator(equation_str, parameter_names, num_independent_vars=1)[source]
Bases:
objectEvaluates custom mathematical functions safely at runtime.
Workflow Controller
Workflow controller for fitting operations. Contains coordination functions and workflow patterns for the fitting application.
- fitting.workflow_controller.reload_data_by_type(file_path, file_type)[source]
Reload data from a file based on its type.
This function is used in loop mode to reload updated data from the same file. Useful when the user is modifying data in real-time and wants to see the updated fit after each modification.
- Parameters:
file_path – Path to the data file
file_type – Type of file (‘csv’, ‘xlsx’, ‘txt’)
- Returns:
Loaded data as DataFrame
- Raises:
DataLoadError – If file_type is not supported or loading fails
- fitting.workflow_controller.single_fit_with_loop(fitter_function, data, x_name, y_name, plot_name, data_file_path, data_file_type)[source]
Execute a single fitting operation with optional loop mode.
This function performs an initial fit and then optionally loops, reloading data and refitting each iteration. This is useful for iterative data analysis where the user modifies the data file between fits to explore different scenarios.
Workflow: 1. Perform initial fit with current data 2. Show results and ask if user wants to continue 3. If yes: reload data from file and repeat 4. If no: exit
- Parameters:
fitter_function – Fitting function to call (must accept data, x_name, y_name, plot_name)
data – Initial dataset (pandas DataFrame)
x_name – X variable column name(s) - string for single variable, list for multiple
y_name – Y variable column name
plot_name – Plot name for window titles and filename
data_file_path – Path to data file for reloading
data_file_type – File type (‘csv’, ‘xlsx’, ‘txt’)
- fitting.workflow_controller.multiple_fit_with_loop(fitter_function, datasets)[source]
Execute multiple fitting operations with optional loop mode.
Performs fitting on multiple datasets sequentially, with the option to reload and refit each dataset in a loop. Each dataset can be independently continued or stopped.
Workflow: 1. Fit all datasets once 2. Ask user for each dataset if they want to continue 3. For datasets marked to continue: reload and refit 4. Repeat until no datasets are marked to continue
- Parameters:
fitter_function – Fitting function to call (must accept data, x_name, y_name, plot_name).
datasets – List of dictionaries, each containing: - ‘data’: dataset (pandas DataFrame) - ‘x_name’: X variable column name - ‘y_name’: Y variable column name - ‘plot_name’: plot name for display and filename - ‘data_file_path’: path to data file for reloading - ‘data_file_type’: file type (‘csv’, ‘xlsx’, ‘txt’)
- fitting.workflow_controller.apply_all_equations(equation_setter, get_fitter, equation_types, data, x_name, y_name, plot_name=None)[source]
Apply all available equation types to a dataset.
This function automatically tests all predefined equation types on a single dataset, displaying results for each. Useful for exploratory data analysis to determine which mathematical model best fits the data.
The function iterates through all equations and displays the fit results one by one, allowing the user to compare goodness of fit visually.
- Parameters:
equation_setter – Function to set the current equation type (e.g., ‘linear_function’)
get_fitter – Function to retrieve the fitter for the currently set equation type
equation_types – List of equation type identifiers to test (e.g., from config.AVAILABLE_EQUATION_TYPES)
data – Dataset to fit (pandas DataFrame)
x_name – Independent variable column name
y_name – Dependent variable column name
plot_name – Plot name for display and filename (optional).
Examples
>>> from config import AVAILABLE_EQUATION_TYPES >>> apply_all_equations( ... equation_setter=my_setter, ... get_fitter=my_getter, ... equation_types=AVAILABLE_EQUATION_TYPES, ... data=df, ... x_name='x', ... y_name='y', ... plot_name='my_plot' ... )
- fitting.workflow_controller.coordinate_data_loading(parent_window, open_load_func, ask_variables_func)[source]
Coordinate the complete data loading workflow.
This function orchestrates the entire data loading process: 1. Open native file dialog to select a data file 2. Load the data 3. Ask user for variables to use
- Parameters:
parent_window – Parent Tkinter window.
open_load_func – Function that opens native file dialog, returns (path, file_type) or (None, None) on cancel. E.g. open_load_dialog from frontend.ui_dialogs.
ask_variables_func – Function to ask for variables.
- Returns:
Tuple (data, x_name, y_name, plot_name, file_path, file_type). On user cancel, data is empty string and other fields are empty strings.
- fitting.workflow_controller.coordinate_data_viewing(parent_window, open_load_func, show_data_func)[source]
Coordinate the data viewing workflow.
This function orchestrates the process of selecting and displaying data from files without performing any fitting operations.
- Parameters:
parent_window – Parent Tkinter window.
open_load_func – Function that opens native file dialog, returns (path, file_type) or (None, None) on cancel.
show_data_func – Function to display data.
- fitting.workflow_controller.coordinate_equation_selection(parent_window, ask_equation_type_func, ask_num_parameters_func, ask_parameter_names_func, ask_custom_formula_func, get_fitting_function_func)[source]
Coordinate the equation selection workflow.
Handles the complete process of equation selection, including both predefined equations and custom user-defined equations.
- Parameters:
parent_window – Parent Tkinter window
ask_equation_type_func – Function to ask for equation type
ask_num_parameters_func – Function to ask for number of parameters
ask_parameter_names_func – Function to ask for parameter names
ask_custom_formula_func – Function to ask for custom formula
get_fitting_function_func – Function to retrieve fitting function by name
- Returns:
Tuple of (equation_name, fitter_function)
- fitting.workflow_controller.coordinate_custom_equation(parent_window, ask_num_parameters_func, ask_parameter_names_func, ask_custom_formula_func)[source]
Coordinate the custom equation creation workflow.
Handles the process of creating a user-defined custom fitting equation by collecting parameter information and the formula. Returns only the backend fitting function (without visualization), maintaining separation of concerns.
- Parameters:
parent_window – Parent Tkinter window
ask_num_parameters_func – Function to ask for number of parameters and independent variables
ask_parameter_names_func – Function to ask for parameter names
ask_custom_formula_func – Function to ask for custom formula
- Returns:
<formula>’, backend_fit_function) or (EXIT_SIGNAL, None) if cancelled
- Return type:
Tuple of (‘custom
Estimators
Initial parameter estimators for curve fitting.
Inputs (x, y) are assumed to be finite and already validated/cleaned by the caller. This module provides functions to estimate initial parameter values and ranges for various model types (linear, polynomial, trigonometric, gaussian, logistic, exponential, etc.) to improve fitting convergence.
- fitting.estimators.estimate_trigonometric_parameters(x, y)[source]
Estimate initial parameters for trigonometric functions (sin/cos).
Estimates amplitude (a) and angular frequency (b) for y = a * sin(b*x) or y = a * cos(b*x). Uses peak detection first, with autocorrelation fallback when peaks are insufficient.
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
Tuple (amplitude, frequency).
- fitting.estimators.estimate_phase_shift(x, y, amplitude, frequency)[source]
Estimate initial phase shift for y = a * sin(b*x + c) or y = a * cos(b*x + c).
- Parameters:
x – Independent variable array
y – Dependent variable array
amplitude – Estimated amplitude (a)
frequency – Estimated angular frequency (b)
- Returns:
Estimated phase shift (c).
- fitting.estimators.estimate_linear_parameters(x, y)[source]
Estimate initial parameters for linear fit y = m*x + n (intercept n, slope m).
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
intercept and slope.
- Return type:
Tuple (n, m)
- fitting.estimators.estimate_polynomial_parameters(x, y, degree)[source]
Estimate initial parameters for polynomial y = c0 + c1*x + … + c_degree*x^degree.
- Parameters:
x – Independent variable array.
y – Dependent variable array.
degree – Polynomial degree.
- Returns:
List of coefficients [c0, c1, …, c_degree] (constant term first).
- fitting.estimators.estimate_single_power_parameter(x, y, power)[source]
Estimate coefficient a for y = a * x^power (no constant term). Least-squares: a = sum(y * x^power) / sum(x^(2*power)).
- Parameters:
x – Independent variable array
y – Dependent variable array
power – Exponent (e.g. 2 for quadratic through origin)
- Returns:
Estimated coefficient a.
- fitting.estimators.estimate_ln_parameter(x, y)[source]
Estimate initial parameter a for y = a * ln(x). Least squares with no intercept; positive x only.
- Parameters:
x – Independent variable array (positive)
y – Dependent variable array
- Returns:
Estimated coefficient a.
- fitting.estimators.estimate_inverse_parameter(x, y, power)[source]
Estimate coefficient a for y = a / x^power. Uses median of (y * x^power) for robustness to outliers.
- Parameters:
x – Independent variable array (avoid zeros)
y – Dependent variable array
power – Power in denominator (1 or 2)
- Returns:
Estimated coefficient a.
- fitting.estimators.estimate_gaussian_parameters(x, y)[source]
Estimate initial (A, mu, sigma) for y = A * exp(-(x-mu)^2 / (2*sigma^2)). A from max(y), mu from x at max, sigma from FWHM (half-max crossings).
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
Tuple (A, mu, sigma).
- fitting.estimators.estimate_binomial_parameters(x, y)[source]
Estimate (a, b, c) for logistic y = a / (1 + exp(-b*(x-c))). a = range, c = midpoint of transition, b from inverse of transition width.
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
Tuple (a, b, c).
- fitting.estimators.estimate_exponential_parameters(x, y)[source]
Estimate (a, b) for y = a * exp(b*x). Uses log(y) = log(a) + b*x when y > 0; otherwise fallback from endpoints.
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
Tuple (a, b).
- fitting.estimators.estimate_square_pulse_parameters(x, y)[source]
Estimate (A, t0, w) for a smooth square pulse: amplitude, center time, width. A from peak-to-peak, t0 from center of mass of
y(absolute value), w from support of elevated region.- Parameters:
x – Independent variable array (e.g. time)
y – Dependent variable array
- Returns:
Tuple (A, t0, w).
- fitting.estimators.estimate_hyperbolic_parameters(x, y)[source]
Estimate initial parameters for hyperbolic functions (sinh/cosh).
For y = a * sinh(b*x) or y = a * cosh(b*x): amplitude (a) from half the y range, frequency (b) from inverse of x range.
- Parameters:
x – Independent variable array
y – Dependent variable array
- Returns:
Tuple (amplitude, frequency).
- fitting.estimators.estimate_hyperbolic_bounds(x)[source]
Return parameter bounds for hyperbolic fits (sinh/cosh) to avoid overflow.
For y = a * sinh(b*x) or y = a * cosh(b*x): a can be any real, b must be bounded so that b*max(abs(x)) does not cause overflow in exp.
- Parameters:
x – Independent variable array
- Returns:
Pair (lower_bounds, upper_bounds), each of length 2 for (a, b).