Parameter Optimization

This module provides tools for optimizing trend labelling parameters.

Optimizer

class tstrends.optimization.Optimizer(returns_estimator, initial_points=10, nb_iter=1000, random_state=None)[source]

Bayesian optimization for trend labelling parameters.

This class implements Bayesian optimization to find the optimal parameters for trend labelling algorithms. It uses the returns from a returns estimator as the objective function to maximize.

Parameters:
returns_estimator

The returns estimator instance to use.

Type:

BaseReturnEstimator

initial_points

Number of initial random points for optimization.

Type:

int

nb_iter

Number of optimization iterations.

Type:

int

random_state

Random seed for reproducibility.

Type:

int | None

_optimizer

Internal optimizer instance.

Type:

BayesianOptimization | None

Example

>>> from returns_estimation import SimpleReturnEstimator
>>> optimizer = Optimizer(SimpleReturnEstimator(), initial_points=5, nb_iter=100)
>>> result = optimizer.optimize(BinaryCTL, prices)
>>> print(result['params'])  # {'omega': 0.005}

Note

The optimization process uses Bayesian optimization with Gaussian processes, which is particularly effective for expensive-to-evaluate objective functions with a small number of parameters.

__init__(returns_estimator, initial_points=10, nb_iter=1000, random_state=None)[source]
Parameters:
Return type:

None

get_optimizer()[source]

Get the underlying BayesianOptimization object if you need access to the full optimization history.

Returns:

The optimizer instance with optimization results.

Return type:

BayesianOptimization

Raises:

ValueError – If optimize() hasn’t been called yet.

optimize(labeller_class, time_series_list, bounds=None, acquisition_function=None, verbose=0)[source]

Optimize the trend labelling parameters.

Parameters:
  • labeller_class (type[BaseLabeller]) – The trend labeller class to optimize.

  • time_series_list (list[float] | list[list[float]]) – Either a single time series list or a list of time series lists to optimize the trend labelling parameters on.

  • bounds (dict[str, tuple[float, float]] | None, optional) – The bounds of the parameters to optimize. If not provided, the bounds will be the default bounds. Defaults to None.

  • acquisition_function (acquisition.AcquisitionFunction | None, optional) – The acquisition function to use. If not provided, the default acquisition function UpperConfidenceBound(kappa=2) will be used.

  • verbose (int | None, optional) – Verbosity level for optimization output. Defaults to 0.

Returns:

A dictionary containing:
  • ’params’: Dictionary of optimal parameters

  • ’target’: The maximum target value achieved

Return type:

dict[str, dict[str, float] | float]

OptimizationBounds

class tstrends.optimization.OptimizationBounds[source]

Class to provide default bounds for optimization parameters.

This class provides a centralized way to get the default parameter bounds for different trend labeller implementations. These bounds are used in the optimization process to constrain the search space.

implemented_labellers

List of supported labeller classes.

Type:

list[Type[BaseLabeller]]

Example

>>> bounds = OptimizationBounds()
>>> binary_bounds = bounds.get_bounds(BinaryCTL)
>>> print(binary_bounds)  # {'omega': (0.0, 0.01)}

Note

The bounds are carefully chosen based on empirical testing and the theoretical constraints of each labeller implementation.

implemented_labellers: list[type[BaseLabeller]] = [<class 'tstrends.trend_labelling.binary_CTL.BinaryCTL'>, <class 'tstrends.trend_labelling.ternary_CTL.TernaryCTL'>, <class 'tstrends.trend_labelling.oracle_labeller.OracleBinaryTrendLabeller'>, <class 'tstrends.trend_labelling.oracle_labeller.OracleTernaryTrendLabeller'>]
get_bounds(labeller_class)[source]

Get the default bounds for a given labeller class.

Parameters:

labeller_class (type[BaseLabeller]) – The labeller class to get bounds for.

Returns:

A dictionary mapping parameter names to their bounds.

Return type:

dict[str, tuple[float, float]]

Raises:

ValueError – If the labeller class is not supported.