merlion.models.automl package

Contains all AutoML layers.

base

Base class/mixin for AutoML hyperparameter search.

seasonality

Automatic seasonality detection.

autoets

Automatic hyperparamter selection for ETS.

autoprophet

Automatic hyperparameter selection for Facebook's Prophet.

autosarima

Automatic hyperparameter selection for SARIMA.

Submodules

merlion.models.automl.base module

Base class/mixin for AutoML hyperparameter search.

class merlion.models.automl.base.AutoMLMixIn(config=None, model=None, **kwargs)

Bases: LayeredModel

Abstract base class which converts LayeredModel into an AutoML model.

train_model(train_data, train_config=None, **kwargs)

Generates a set of candidate models and picks the best one.

Parameters
  • train_data (TimeSeries) – the data to train on.

  • train_config – the train config of the underlying model (optional).

abstract generate_theta(train_data)
Parameters

train_data (TimeSeries) – Pre-processed training data to use for generation of hyperparameters \(\theta\)

Returns an iterator of hyperparameter candidates for consideration with th underlying model.

Return type

Iterator

abstract evaluate_theta(thetas, train_data, train_config=None, **kwargs)
Parameters
  • thetas (Iterator) – Iterator of the hyperparameter candidates

  • train_data (TimeSeries) – Pre-processed training data

  • train_config – Training configuration

Return the optimal hyperparameter, as well as optionally a model and result of the training procedure.

Return type

Tuple[Any, Optional[ModelBase], Optional[Tuple[TimeSeries, Optional[TimeSeries]]]]

abstract set_theta(model, theta, train_data=None)
Parameters
  • model – Underlying base model to which the new theta is applied

  • theta – Hyperparameter to apply

  • train_data (Optional[TimeSeries]) – Pre-processed training data (Optional)

Sets the hyperparameter to the provided model. This is used to apply the \(\theta\) to the model, since this behavior is custom to every model. Oftentimes in internal implementations, model is the optimal model.

class merlion.models.automl.base.InformationCriterion(value)

Bases: Enum

An enumeration.

AIC = 1

Akaike information criterion. Computed as

\[\mathrm{AIC} = 2k - 2\mathrm{ln}(L)\]

where k is the number of parameters, and L is the model’s likelihood.

BIC = 2

Bayesian information criterion. Computed as

\[k \mathrm{ln}(n) - 2 \mathrm{ln}(L)\]

where n is the sample size, k is the number of parameters, and L is the model’s likelihood.

AICc = 3

Akaike information criterion with correction for small sample size. Computed as

\[\mathrm{AICc} = \mathrm{AIC} + \frac{2k^2 + 2k}{n - k - 1}\]

where n is the sample size, and k is the number of paramters.

class merlion.models.automl.base.ICConfig(information_criterion=InformationCriterion.AIC, transform=None, **kwargs)

Bases: Config

Mix-in to add an information criterion parameter to a model config.

Parameters
  • information_criterion (InformationCriterion) – information criterion to select the best model.

  • transform – Transformation to pre-process input time series.

property information_criterion
class merlion.models.automl.base.ICAutoMLForecaster(config=None, model=None, **kwargs)

Bases: AutoMLMixIn, ForecasterBase

AutoML model which uses an information criterion to determine which model paramters are best.

config_class

alias of ICConfig

property information_criterion
abstract get_ic(model, train_data, train_result)

Returns the information criterion of the model based on the given training data & the model’s train result.

Parameters
  • model – One of the models being tried. Must be trained.

  • train_data (DataFrame) – The target sequence of the training data as a pandas.DataFrame.

  • train_result (Tuple[DataFrame, Optional[DataFrame]]) – The result of calling model._train().

Return type

float

Returns

The information criterion evaluating the model’s goodness of fit.

evaluate_theta(thetas, train_data, train_config=None, **kwargs)
Parameters
  • thetas (Iterator) – Iterator of the hyperparameter candidates

  • train_data (TimeSeries) – Pre-processed training data

  • train_config – Training configuration

Return the optimal hyperparameter, as well as optionally a model and result of the training procedure.

Return type

Tuple[Any, ModelBase, Tuple[TimeSeries, Optional[TimeSeries]]]

merlion.models.automl.autosarima module

Automatic hyperparameter selection for SARIMA.

class merlion.models.automl.autosarima.AutoSarimaConfig(model=None, auto_seasonality=True, periodicity_strategy=PeriodicityStrategy.ACF, auto_pqPQ=True, auto_d=True, auto_D=True, maxiter=None, max_k=100, max_dur=3600, approximation=None, approx_iter=None, pval=0.05, max_lag=None, model_kwargs=None, transform=None, **kwargs)

Bases: SeasonalityConfig

Configuration class for AutoSarima. Acts as a wrapper around a Sarima model, which automatically detects the seasonality, (seasonal) differencing order, and (seasonal) AR/MA orders. If a non-numeric value is specified for any of the relevant parameters in the order or seasonal order, we assume that the user wishes to detect that parameter automatically.

Note

The automatic selection of AR, MA, seasonal AR, and seasonal MA parameters is implemented in a coupled way. The user must specify all of these parameters explicitly to avoid automatic selection.

Parameters
  • model (Union[Sarima, dict, None]) – The model being wrapped, or a dict representing it.

  • auto_seasonality (bool) – Whether to automatically detect the seasonality.

  • periodicity_strategy (PeriodicityStrategy) – Periodicity Detection Strategy.

  • auto_pqPQ (bool) – Whether to automatically choose AR/MA orders p, q and seasonal AR/MA orders P, Q.

  • auto_d (bool) – Whether to automatically choose the difference order d.

  • auto_D (bool) – Whether to automatically choose the seasonal difference order D.

  • maxiter (Optional[int]) – The maximum number of iterations to perform

  • max_k (int) – Maximum number of models considered in the stepwise search

  • max_dur (float) – Maximum training time considered in the stepwise search

  • approximation (Optional[bool]) – Whether to use approx_iter iterations (instead of maxiter) to speed up computation. If None, we use approximation mode when the training data is too long (>150), or when the length off the period is too high (periodicity > 12).

  • approx_iter (Optional[int]) – The number of iterations to perform in approximation mode

  • pval – p-value for deciding whether a detected seasonality is statistically significant.

  • max_lag – max lag considered for seasonality detection.

  • model_kwargs – Keyword arguments used specifically to initialize the underlying model. Only used if model is a dict. Will override keys in the model dict if specified.

  • transform – Transformation to pre-process input time series.

  • kwargs – Any other keyword arguments (e.g. for initializing a base class). If model is a dict, we will also try to pass these arguments when creating the actual underlying model. However, they will not override arguments in either the model dict or model_kwargs dict.

property order
property seasonal_order
class merlion.models.automl.autosarima.AutoSarima(config=None, model=None, **kwargs)

Bases: SeasonalityLayer

config_class

alias of AutoSarimaConfig

generate_theta(train_data)

generate [action, theta]. action is an indicator for stepwise seach (stepwsie) of p, q, P, Q, trend parameters or use a predefined parameter combination (pqPQ) theta is a list of parameter combination [order, seasonal_order, trend]

Return type

Iterator

evaluate_theta(thetas, train_data, train_config=None, **kwargs)
Parameters
  • thetas (Iterator) – Iterator of the hyperparameter candidates

  • train_data (TimeSeries) – Pre-processed training data

  • train_config – Training configuration

Return the optimal hyperparameter, as well as optionally a model and result of the training procedure.

Return type

Tuple[Any, Optional[Sarima], Optional[Tuple[TimeSeries, Optional[TimeSeries]]]]

set_theta(model, theta, train_data=None)
Parameters
  • model – Underlying base model to which the new theta is applied

  • theta – Hyperparameter to apply

  • train_data (Optional[TimeSeries]) – Pre-processed training data (Optional)

Sets the hyperparameter to the provided model. This is used to apply the \(\theta\) to the model, since this behavior is custom to every model. Oftentimes in internal implementations, model is the optimal model.

merlion.models.automl.seasonality module

Automatic seasonality detection.

class merlion.models.automl.seasonality.PeriodicityStrategy(value)

Bases: Enum

Strategy to choose the seasonality if multiple candidates are detected.

ACF = 1

Select the seasonality value with the highest autocorrelation.

Min = 2

Select the minimum seasonality.

Max = 3

Select the maximum seasonality.

All = 4

Use all seasonalities. Only valid for models which support multiple seasonalities.

class merlion.models.automl.seasonality.SeasonalityModel

Bases: object

Class provides simple implementation to set the seasonality in a model. Extend this class to implement custom behavior for seasonality processing.

abstract set_seasonality(theta, train_data)

Implement this method to do any model-specific adjustments on the seasonality that was provided by SeasonalityLayer.

Parameters
  • theta – Seasonality processed by SeasonalityLayer.

  • train_data (UnivariateTimeSeries) – Training data (or numpy array representing the target univariate) for any model-specific adjustments you might want to make.

class merlion.models.automl.seasonality.SeasonalityConfig(model, periodicity_strategy=PeriodicityStrategy.ACF, pval=0.05, max_lag=None, model_kwargs=None, transform=None, **kwargs)

Bases: LayeredModelConfig

Config object for an automatic seasonality detection layer.

Parameters
  • model – The model being wrapped, or a dict representing it.

  • periodicity_strategy – Strategy to choose the seasonality if multiple candidates are detected.

  • pval (float) – p-value for deciding whether a detected seasonality is statistically significant.

  • max_lag – max lag considered for seasonality detection.

  • model_kwargs – Keyword arguments used specifically to initialize the underlying model. Only used if model is a dict. Will override keys in the model dict if specified.

  • transform – Transformation to pre-process input time series.

  • kwargs – Any other keyword arguments (e.g. for initializing a base class). If model is a dict, we will also try to pass these arguments when creating the actual underlying model. However, they will not override arguments in either the model dict or model_kwargs dict.

property multi_seasonality
Returns

Whether the model supports multiple seasonalities. False unless explicitly overridden.

property periodicity_strategy: PeriodicityStrategy
Return type

PeriodicityStrategy

Returns

Strategy to choose the seasonality if multiple candidates are detected.

class merlion.models.automl.seasonality.SeasonalityLayer(config=None, model=None, **kwargs)

Bases: AutoMLMixIn

Seasonality Layer that uses automatically determines the seasonality of your data. Can be used directly on any model that implements SeasonalityModel class. The algorithmic idea is from the theta method. We find a set of multiple candidate seasonalites, and we return the best one(s) based on the PeriodicityStrategy.

config_class

alias of SeasonalityConfig

property require_even_sampling: bool

Whether the model assumes that training data is sampled at a fixed frequency

Return type

bool

property require_univariate

Whether the model only works with univariate time series.

property multi_seasonality
Returns

Whether the model supports multiple seasonalities.

property periodicity_strategy
Returns

Strategy to choose the seasonality if multiple candidates are detected.

property pval
Returns

p-value for deciding whether a detected seasonality is statistically significant.

property max_lag
Returns

max_lag for seasonality detection

set_theta(model, theta, train_data=None)
Parameters
  • model – Underlying base model to which the new theta is applied

  • theta – Hyperparameter to apply

  • train_data (Optional[TimeSeries]) – Pre-processed training data (Optional)

Sets the hyperparameter to the provided model. This is used to apply the \(\theta\) to the model, since this behavior is custom to every model. Oftentimes in internal implementations, model is the optimal model.

evaluate_theta(thetas, train_data, train_config=None, **kwargs)
Parameters
  • thetas (Iterator) – Iterator of the hyperparameter candidates

  • train_data (TimeSeries) – Pre-processed training data

  • train_config – Training configuration

Return the optimal hyperparameter, as well as optionally a model and result of the training procedure.

Return type

Tuple[Any, Optional[ModelBase], Optional[Tuple[TimeSeries, Optional[TimeSeries]]]]

generate_theta(train_data)
Parameters

train_data (TimeSeries) – Pre-processed training data to use for generation of hyperparameters \(\theta\)

Returns an iterator of hyperparameter candidates for consideration with th underlying model.

Return type

Iterator