nlsq.algorithm_selector module

Automatic algorithm selection for NLSQ optimization.

This module analyzes problem characteristics and automatically selects the best optimization algorithm and parameters.

class nlsq.precision.algorithm_selector.AlgorithmSelector[source]

Bases: object

Automatically select best algorithm based on problem characteristics.

This class analyzes the optimization problem and recommends the best algorithm, loss function, and parameters based on: - Problem size and dimensionality - Data characteristics (outliers, noise, conditioning) - Memory constraints - Convergence requirements

__init__()[source]

Initialize algorithm selector.

analyze_problem(f, xdata, ydata, p0=None, bounds=None, memory_limit_gb=None)[source]

Analyze problem characteristics.

Parameters:
  • f (callable) – Model function to fit

  • xdata (np.ndarray) – Independent variable data

  • ydata (np.ndarray) – Dependent variable data

  • p0 (np.ndarray, optional) – Initial parameter guess

  • bounds (tuple, optional) – Parameter bounds

  • memory_limit_gb (float, optional) – Memory constraint in GB

Returns:

analysis – Problem characteristics and statistics

Return type:

dict

select_algorithm(problem_analysis, user_preferences=None)[source]

Select best algorithm based on problem analysis.

This method orchestrates algorithm selection by calling focused helper methods for each decision.

Parameters:
  • problem_analysis (dict) – Results from analyze_problem

  • user_preferences (dict, optional) – User preferences (e.g., prioritize speed vs accuracy)

Returns:

recommendations – Recommended algorithm and parameters

Return type:

dict

get_algorithm_explanation(recommendations)[source]

Get human-readable explanation of algorithm choice.

Parameters:

recommendations (dict) – Algorithm recommendations

Returns:

explanation – Explanation of the choices

Return type:

str

nlsq.precision.algorithm_selector.auto_select_algorithm(f, xdata, ydata, p0=None, bounds=None, memory_limit_gb=None, user_preferences=None)[source]

Automatically select best optimization algorithm.

Parameters:
  • f (callable) – Model function

  • xdata (np.ndarray) – Independent variable

  • ydata (np.ndarray) – Dependent variable

  • p0 (np.ndarray, optional) – Initial guess

  • bounds (tuple, optional) – Parameter bounds

  • memory_limit_gb (float, optional) – Memory limit

  • user_preferences (dict, optional) – User preferences

Returns:

recommendations – Algorithm recommendations

Return type:

dict

Overview

The algorithm_selector module provides automatic selection of the best optimization algorithm based on problem characteristics.

Key Features

  • Automatic algorithm selection based on problem analysis

  • Performance optimization with problem-specific tuning

  • Convergence analysis and parameter adjustment

  • Robustness testing with multiple initialization strategies

Supported Algorithms

  • Trust Region Reflective (TRF): Best for bounded problems

  • Levenberg-Marquardt (LM): Fast for well-conditioned problems

  • Dogleg: Efficient for trust-region problems

Classes

class nlsq.precision.algorithm_selector.AlgorithmSelector[source]

Bases: object

Automatically select best algorithm based on problem characteristics.

This class analyzes the optimization problem and recommends the best algorithm, loss function, and parameters based on: - Problem size and dimensionality - Data characteristics (outliers, noise, conditioning) - Memory constraints - Convergence requirements

__init__()[source]

Initialize algorithm selector.

analyze_problem(f, xdata, ydata, p0=None, bounds=None, memory_limit_gb=None)[source]

Analyze problem characteristics.

Parameters:
  • f (callable) – Model function to fit

  • xdata (np.ndarray) – Independent variable data

  • ydata (np.ndarray) – Dependent variable data

  • p0 (np.ndarray, optional) – Initial parameter guess

  • bounds (tuple, optional) – Parameter bounds

  • memory_limit_gb (float, optional) – Memory constraint in GB

Returns:

analysis – Problem characteristics and statistics

Return type:

dict

select_algorithm(problem_analysis, user_preferences=None)[source]

Select best algorithm based on problem analysis.

This method orchestrates algorithm selection by calling focused helper methods for each decision.

Parameters:
  • problem_analysis (dict) – Results from analyze_problem

  • user_preferences (dict, optional) – User preferences (e.g., prioritize speed vs accuracy)

Returns:

recommendations – Recommended algorithm and parameters

Return type:

dict

get_algorithm_explanation(recommendations)[source]

Get human-readable explanation of algorithm choice.

Parameters:

recommendations (dict) – Algorithm recommendations

Returns:

explanation – Explanation of the choices

Return type:

str

Functions

nlsq.precision.algorithm_selector.auto_select_algorithm(f, xdata, ydata, p0=None, bounds=None, memory_limit_gb=None, user_preferences=None)[source]

Automatically select best optimization algorithm.

Parameters:
  • f (callable) – Model function

  • xdata (np.ndarray) – Independent variable

  • ydata (np.ndarray) – Dependent variable

  • p0 (np.ndarray, optional) – Initial guess

  • bounds (tuple, optional) – Parameter bounds

  • memory_limit_gb (float, optional) – Memory limit

  • user_preferences (dict, optional) – User preferences

Returns:

recommendations – Algorithm recommendations

Return type:

dict

Example Usage

from nlsq.precision.algorithm_selector import auto_select_algorithm
from nlsq import curve_fit
import jax.numpy as jnp


# Define model
def model_nonlinear(x, a, b, c):
    return a * jnp.exp(-b * x) + c


# Generate data
x = jnp.linspace(0, 10, 100)
y = 2.5 * jnp.exp(-0.5 * x) + 1.0 + 0.1 * jnp.random.randn(100)

# Auto-select best algorithm
recommendations = auto_select_algorithm(
    f=model_nonlinear, xdata=x, ydata=y, p0=[1.0, 0.5, 0.1]
)

# Use recommended algorithm
method = recommendations.get("algorithm", "trf")
popt, pcov = curve_fit(model_nonlinear, x, y, p0=[1.0, 0.5, 0.1], method=method)

print(f"Selected algorithm: {method}")
print(f"Fitted parameters: {popt}")

Selection Criteria

The selector analyzes:

  • Problem size: Number of parameters and data points

  • Bounds: Presence of parameter bounds

  • Conditioning: Jacobian condition number

  • Nonlinearity: Degree of nonlinearity

  • Sparsity: Jacobian sparsity pattern

Advanced Usage

from nlsq.precision.algorithm_selector import AlgorithmSelector

# Create selector with custom settings
selector = AlgorithmSelector(enable_diagnostics=True, robustness_test=True)

# Analyze problem
analysis = selector.analyze_problem(
    f=model_nonlinear, xdata=x, ydata=y, p0=[1.0, 0.5, 0.1]
)

# Get detailed recommendations
recommendations = selector.recommend_algorithm(analysis)
print(f"Primary: {recommendations['primary']}")
print(f"Fallback: {recommendations['fallback']}")
print(f"Reasoning: {recommendations['reasoning']}")

See Also