Xreg

Exogenous variables manipulation functions.

This module provides functions for transforming and expanding exogenous variables for use in regression models.

greybox.xreg.B(x: ndarray, k: int, gaps: str = 'auto') ndarray[source]

Backshift operator: lag (k>0) or lead (k<0) of x.

Positive k creates lag-k (past values); negative k creates lead-abs(k) (future values); k=0 returns x unchanged. Gaps at boundaries are filled per the gaps strategy.

Parameters:
  • x (array-like of shape (n,))

  • k (int) – Lag order. Positive = lag (past values), negative = lead (future).

  • gaps (str, default "auto") – Boundary fill strategy passed to xreg_expander.

Return type:

np.ndarray of shape (n,)

greybox.xreg.multipliers(model, parm: str, h: int = 10) dict[source]

Compute dynamic multipliers for an ARDL model.

Combines distributed lag coefficients (B(parm, k) terms) with the ARI polynomial from the model to produce impulse-response multipliers over horizon h.

Parameters:
  • model (ALM) – Fitted ALM model containing parm (and optionally B(parm, k) columns and/or ARIMA orders).

  • parm (str) – Variable name as it appears in the design matrix (column header).

  • h (int, default 10) – Forecast horizon.

Returns:

{“h1”: m1, “h2”: m2, …, “hh”: mh} of dynamic multipliers.

Return type:

dict

Raises:

ValueError – If parm is not found in the model.

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='M')
>>> temporal_dummy(dates, freq=12)
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])
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)
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])
>>> xreg_transformer(x, functions=["log", "sqrt"])