Xreg

greybox.xreg.xreg_transformer(xreg: ndarray, functions: list[str] | None = None, silent: bool = True) ndarray[source]

Transform exogenous variables.

Function transforms each variable in the provided matrix or vector, producing non-linear values, depending on the selected pool of functions.

Parameters:
  • xreg (np.ndarray) – Vector / matrix containing variables that need to be transformed.

  • functions (list of str, optional) – Vector of names for functions used. Options: “log”, “exp”, “inv”, “sqrt”, “square”. If None, uses all functions.

  • silent (bool, default=True) – If False, then progress is printed. Otherwise the function won’t print anything.

Returns:

Matrix with the transformed and the original variables.

Return type:

np.ndarray

Examples

>>> x = np.array([1, 2, 3, 4, 5])
>>> np.round(xreg_transformer(x, functions=["log", "sqrt"]), 4)
array([[1.    , 0.    , 1.    ],
       [2.    , 0.6931, 1.4142],
       [3.    , 1.0986, 1.7321],
       [4.    , 1.3863, 2.    ],
       [5.    , 1.6094, 2.2361]])
greybox.xreg.xreg_multiplier(xreg: ndarray, silent: bool = True) ndarray[source]

Generate cross-products of exogenous variables.

Function generates the cross-products of the provided exogenous variables. This might be useful when introducing interactions between dummy and continuous variables.

Parameters:
  • xreg (np.ndarray) – Matrix containing variables that need to be expanded. Must have at least two columns.

  • silent (bool, default=True) – If False, then progress is printed. Otherwise the function won’t print anything.

Returns:

Matrix with the cross-products and the original variables.

Return type:

np.ndarray

Examples

>>> x = np.array([[1, 2], [3, 4], [5, 6]])
>>> xreg_multiplier(x)
array([[ 1.,  2.,  2.],
       [ 3.,  4., 12.],
       [ 5.,  6., 30.]])
greybox.xreg.xreg_expander(xreg: ndarray, lags: list[int] | None = None, silent: bool = True, gaps: Literal['auto', 'NAs', 'zero', 'naive', 'extrapolate'] = 'auto') ndarray[source]

Expand exogenous variables with lags and leads.

Function expands the provided matrix or vector of variables, producing values with lags and leads specified by lags parameter.

Parameters:
  • xreg (np.ndarray) – Vector / matrix containing variables that need to be expanded.

  • lags (list of int, optional) – Vector of lags / leads. Negative values mean lags, positive ones mean leads. If None, uses -1, 0, 1 (seasonal lags if ts object).

  • silent (bool, default=True) – If False, then progress is printed. Otherwise the function won’t print anything.

  • gaps (str, default="auto") – Defines how to fill in the gaps in the data: - “NAs”: leave missing values - “zero”: substitute them by zeroes - “naive”: use the last / first actual value - “extrapolate”: use linear extrapolation - “auto”: let the function select between “extrapolate” and “naive”

Returns:

Matrix with the expanded variables.

Return type:

np.ndarray

Examples

>>> x = np.array([1, 2, 3, 4, 5])
>>> xreg_expander(x, lags=[-1, 0, 1])
array([[1., 1., 2.],
       [2., 1., 3.],
       [3., 2., 4.],
       [4., 3., 5.],
       [5., 4., 5.]])
greybox.xreg.temporal_dummy(x, freq: int | None = None, h: int = 0) ndarray[source]

Generate dummy variables for temporal data.

Function generates dummy variables for months, days, hours, etc. based on the provided time index.

Parameters:
  • x (array-like) – Vector of dates or time indices.

  • freq (int, optional) – Frequency of the data. If not provided, attempts to infer.

  • h (int, default=0) – Number of future observations to generate dummies for.

Returns:

Matrix with dummy variables.

Return type:

np.ndarray

Examples

>>> import pandas as pd
>>> dates = pd.date_range('2020-01-01', periods=12, freq='ME')
>>> dummies = temporal_dummy(dates, freq=12)
>>> dummies.shape
(12, 12)
>>> dummies[0]
array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])