Skip to main content

module statsforecast.mfles


function calc_mse

calc_mse(y_true, y_pred)

function calc_mae

calc_mae(y_true, y_pred)

function calc_mape

calc_mape(y_true, y_pred)

function calc_smape

calc_smape(y_true, y_pred)

function cross_validation

cross_validation(
    y,
    X,
    test_size,
    n_splits,
    model_obj,
    metric,
    step_size=1,
    **kwargs
)

function logic_check

logic_check(keys_to_check, keys)

function logic_layer

logic_layer(param_dict)

function default_configs

default_configs(seasonal_period, configs=None)

function cap_outliers

cap_outliers(series, outlier_cap=3)

function set_fourier

set_fourier(period)

function calc_trend_strength

calc_trend_strength(resids, deseasonalized)

function calc_seas_strength

calc_seas_strength(resids, detrended)

function calc_rsq

calc_rsq(y, fitted)

function calc_cov

calc_cov(y, mult=1)

function get_seasonality_weights

get_seasonality_weights(y, seasonal_period)

function get_fourier_series

get_fourier_series(length, seasonal_period, fourier_order)

function get_future_basis

get_future_basis(basis_functions, forecast_horizon)

function lasso_nb

lasso_nb(X, y, alpha, tol=0.001, maxiter=10000)

function ses_ensemble

ses_ensemble(y, min_alpha=0.05, max_alpha=1.0, smooth=False, order=1)

function fast_ols

fast_ols(x, y)
Simple OLS for two data sets.

function median

median(y, seasonal_period)

function ols

ols(X, y)

function wls

wls(X, y, weights)

class OLS


method fit

fit(X, y)

method predict

predict(X)

class Zeros


method predict

predict(X)

class MFLES

method __init__

__init__(verbose=1, robust=None)

method fit

fit(
    y,
    seasonal_period=None,
    X=None,
    fourier_order=None,
    ma=None,
    alpha=1.0,
    decay=-1,
    n_changepoints=0.25,
    seasonal_lr=0.9,
    rs_lr=1,
    exogenous_lr=1,
    exogenous_estimator=<class 'statsforecast.mfles.OLS'>,
    exogenous_params={},
    linear_lr=0.9,
    cov_threshold=0.7,
    moving_medians=False,
    max_rounds=50,
    min_alpha=0.05,
    max_alpha=1.0,
    round_penalty=0.0001,
    trend_penalty=True,
    multiplicative=None,
    changepoints=True,
    smoother=False,
    seasonality_weights=False
)
Fit the MFLES model. Args:
  • y (np.array): The time series as a numpy array.
  • seasonal_period (int, optional): Seasonal period. Defaults to None.
  • X (np.array, optional): Exogenous variables. Defaults to None.
  • fourier_order (int, optional): How many fourier sin/cos pairs to create, the larger the number the more complex of a seasonal pattern can be fitted. A lower number leads to smoother results. This is auto-set based on seasonal_period. Defaults to None.
  • ma (int, optional): The moving average order to use, this is auto-set based on internal logic. Passing 4 would fit a 4 period moving average on the residual component. Defaults to None.
  • alpha (float, optional): The alpha which is used in fitting the underlying LASSO when using piecewise functions. Defaults to 1.0.
  • decay (float, optional): Effects the slopes of the piecewise-linear basis function. Defaults to -1.
  • n_changepoints (float, optional): The number of changepoint knots to place, a default of .25 with place .25 * series length number of knots. Defaults to 0.25.
  • seasonal_lr (float, optional): A shrinkage parameter (0 < seasonal_lr <= 1) which penalizes the seasonal fit, a .9 will flatly multiply the seasonal fit by .9 each boosting round, this can be used to allow more signal to the exogenous component. Defaults to 0.9.
  • rs_lr (float, optional): A shrinkage parameter (0 < rs_lr <= 1) which penalizes the residual smoothing, a .9 will flatly multiply the residual fit by .9 each boosting round, this can be used to allow more signal to the seasonality or linear components. Defaults to 1.
  • exogenous_lr (float, optional): Shrinkage parameter for exogenous variables. Defaults to 1.
  • exogenous_estimator (class, optional): Estimator class for exogenous variables. Defaults to OLS.
  • exogenous_params (dict, optional): Parameters for exogenous estimator. Defaults to .
  • linear_lr (float, optional): A shrinkage parameter (0 < linear_lr <= 1) which penalizes the linear trend fit, a .9 will flatly multiply the linear fit by .9 each boosting round, this can be used to allow more signal to the seasonality or exogenous components. Defaults to 0.9.
  • cov_threshold (float, optional): The deseasonalized cov is used to auto-set some logic, lowering the cov_threshold will result in simpler and less complex residual smoothing. If you pass something like 1000 then there will be no safeguards applied. Defaults to 0.7.
  • moving_medians (bool, optional): The default behavior is to fit an initial median to the time series, if you pass True to moving_medians then it will fit a median per seasonal period. Defaults to False.
  • max_rounds (int, optional): The max number of boosting rounds. The boosting will auto-stop but depending on other parameters such as rs_lr you may want more rounds. Generally, the more rounds => the more smooth your fit. Defaults to 50.
  • min_alpha (float, optional): The min alpha in the SES ensemble. Defaults to 0.05.
  • max_alpha (float, optional): The max alpha used in the SES ensemble. Defaults to 1.0.
  • round_penalty (float, optional): Round penalty parameter. Defaults to 0.0001.
  • trend_penalty (bool, optional): Whether to apply a simple penalty to the lienar trend component, very useful for dealing with the potentially dangerous piecewise trend. Defaults to True.
  • multiplicative (bool, optional): Auto-set based on internal logic, but if given True it will simply take the log of the time series. Defaults to None.
  • changepoints (bool, optional): Whether to fit for changepoints if all other logic allows for it, by setting False then MFLES will not ever fit a piecewise trend. Defaults to True.
  • smoother (bool, optional): If True then a simple exponential ensemble will be used rather than auto settings. Defaults to False.
  • seasonality_weights (bool, optional): Whether to use seasonality weights. Defaults to False.
Returns:
  • np.array: Fitted values.

method optimize

optimize(
    y,
    test_size,
    n_steps,
    step_size=1,
    seasonal_period=None,
    metric='smape',
    X=None,
    params=None
)
Optimization method for MFLES. Args:
  • y (np.array): Your time series as a numpy array.
  • test_size (int): Length of the test set to hold out to calculate test error.
  • n_steps (int): Number of train and test sets to create.
  • step_size (int, optional): How many periods to move after each step. Defaults to 1.
  • seasonal_period (int or list, optional): The seasonal period to calculate for. Defaults to None.
  • metric (str, optional): Supported metrics are smape, mape, mse, mae. Defaults to ‘smape’.
  • X (np.array, optional): Exogenous variables. Defaults to None.
  • params (dict, optional): A user provided dictionary of params to try. Defaults to None.
Returns:
  • dict: Optimal parameters configuration.

method predict

predict(forecast_horizon, X=None)

method seasonal_decompose

seasonal_decompose(y, **kwargs)