Parameter Constraints

The constraints module provides utilities for specifying relationships between parameters.

Constraint utilities for parameter relationships and bounds.

This module provides utilities for specifying and handling parameter constraints, including inequality constraints and parameter relationships.

class ezfit.constraints.Constraint(constraint_func: Callable[[ndarray], float], constraint_type: str = 'ineq', name: str | None = None)[source]

Bases: object

Base class for parameter constraints.

A constraint represents a relationship between parameters that must be satisfied during optimization. Constraints can be linear or nonlinear.

__call__(params: ndarray) float[source]

Evaluate the constraint function.

to_scipy_constraint(param_names: list[str], param_indices: dict[str, int]) NonlinearConstraint[source]

Convert to scipy NonlinearConstraint.

Args:

param_names: List of parameter names in order. param_indices: Mapping from parameter name to index.

Return type:

scipy NonlinearConstraint object.

ezfit.constraints.extract_constraints_from_model(model: Any) list[Callable[[ndarray], float]][source]

Extract constraint functions from a Model’s parameters.

Args:

model: Model object with parameters that may have constraints.

Returns:

  • List of constraint functions that take parameter array

  • and return constraint value.

ezfit.constraints.greater_than(param1: str, param2: str) Callable[[dict[str, float]], bool][source]

Create a constraint function: param1 > param2.

Args:

param1: Name of first parameter. param2: Name of second parameter.

Return type:

Constraint function that returns True if param1 > param2.

ezfit.constraints.less_than(param1: str, param2: str) Callable[[dict[str, float]], bool][source]

Create a constraint function: param1 < param2.

Args:

param1: Name of first parameter. param2: Name of second parameter.

Return type:

Constraint function that returns True if param1 < param2.

ezfit.constraints.parse_constraint_string(constraint_str: str, param_names: list[str]) Callable[[dict[str, float]], bool][source]

Parse a string constraint into a constraint function.

Supports simple constraints like: - “param1 < param2” - “param1 + param2 < 1.0” - “param1 * param2 == 2.0”

Args:

constraint_str: String representation of constraint. param_names: List of valid parameter names.

Return type:

Constraint function.

Raises:

ValueError – If constraint string cannot be parsed.:

ezfit.constraints.product_equals(params: list[str], value: float, tolerance: float = 1e-06) Callable[[dict[str, float]], bool][source]

Create a constraint function: product(params) == value.

Args:

params: List of parameter names to multiply. value: Target value for the product. tolerance: Tolerance for equality check.

Return type:

Constraint function that returns True if product ≈ value.

ezfit.constraints.sum_greater_than(params: list[str], value: float) Callable[[dict[str, float]], bool][source]

Create a constraint function: sum(params) > value.

Args:

params: List of parameter names to sum. value: Minimum value for the sum.

Return type:

Constraint function that returns True if sum > value.

ezfit.constraints.sum_less_than(params: list[str], value: float) Callable[[dict[str, float]], bool][source]

Create a constraint function: sum(params) < value.

Args:

params: List of parameter names to sum. value: Maximum value for the sum.

Return type:

Constraint function that returns True if sum < value.

ezfit.constraints.validate_constraints(model: Any, initial_values: dict[str, float] | None = None) tuple[bool, str | None][source]

Validate that constraints are satisfiable with given parameter values.

Parameters:
  • model (Model) – Model with parameters and constraints.

  • initial_values (dict[str, float] | None, optional) – Initial parameter values to test. If None, uses parameter values.

Returns:

Tuple of (is_valid, error_message). is_valid is True if constraints are satisfiable.

Return type:

tuple[bool, str | None]

Constraint Functions

ezfit.constraints.less_than(param1: str, param2: str) Callable[[dict[str, float]], bool][source]

Create a constraint function: param1 < param2.

Args:

param1: Name of first parameter. param2: Name of second parameter.

Return type:

Constraint function that returns True if param1 < param2.

ezfit.constraints.greater_than(param1: str, param2: str) Callable[[dict[str, float]], bool][source]

Create a constraint function: param1 > param2.

Args:

param1: Name of first parameter. param2: Name of second parameter.

Return type:

Constraint function that returns True if param1 > param2.

ezfit.constraints.sum_less_than(params: list[str], value: float) Callable[[dict[str, float]], bool][source]

Create a constraint function: sum(params) < value.

Args:

params: List of parameter names to sum. value: Maximum value for the sum.

Return type:

Constraint function that returns True if sum < value.

ezfit.constraints.sum_greater_than(params: list[str], value: float) Callable[[dict[str, float]], bool][source]

Create a constraint function: sum(params) > value.

Args:

params: List of parameter names to sum. value: Minimum value for the sum.

Return type:

Constraint function that returns True if sum > value.

ezfit.constraints.product_equals(params: list[str], value: float, tolerance: float = 1e-06) Callable[[dict[str, float]], bool][source]

Create a constraint function: product(params) == value.

Args:

params: List of parameter names to multiply. value: Target value for the product. tolerance: Tolerance for equality check.

Return type:

Constraint function that returns True if product ≈ value.

ezfit.constraints.parse_constraint_string(constraint_str: str, param_names: list[str]) Callable[[dict[str, float]], bool][source]

Parse a string constraint into a constraint function.

Supports simple constraints like: - “param1 < param2” - “param1 + param2 < 1.0” - “param1 * param2 == 2.0”

Args:

constraint_str: String representation of constraint. param_names: List of valid parameter names.

Return type:

Constraint function.

Raises:

ValueError – If constraint string cannot be parsed.:

Examples

String Constraints

# Simple string constraint
model, ax, _ = df.fit(
    model_func, "x", "y", "yerr",
    param1={"value": 1.0, "constraint": "param1 < param2"},
    param2={"value": 2.0}
)

Function Constraints

from ezfit import sum_less_than

model, ax, _ = df.fit(
    model_func, "x", "y", "yerr",
    A1={
        "value": 5.0,
        "constraint": sum_less_than(["A1", "A2"], 10.0)
    },
    A2={"value": 4.0}
)

Lambda Constraints

model, ax, _ = df.fit(
    model_func, "x", "y", "yerr",
    param1={
        "value": 1.0,
        "constraint": lambda p: p["param1"] + p["param2"] < 5.0
    },
    param2={"value": 2.0}
)