gbnet.models.forecasting
Classes
A forecasting model class that implements a trend + seasonality + changepoints |
|
PyTorch module for time series forecasting. |
Functions
|
|
|
Calculate a piecewise linear function based on changepoints evaluated at timepoints. |
|
|
|
MLE for G in Var(r_i)=S2 + G*h_i with known S2. |
Module Contents
- class gbnet.models.forecasting.Forecast(nrounds=50, params={}, module_type='XGBModule', linear_params={}, changepoint_params={}, estimate_uncertainty=True)[source]
Bases:
sklearn.base.BaseEstimator,sklearn.base.RegressorMixinA forecasting model class that implements a trend + seasonality + changepoints model using either XGBModule or LGBModule (but defaulting to XGBModule).
- Parameters:
nrounds (int, default=50) – Number of training iterations (epochs) for the model.
params (dict, optional) – Dictionary of additional parameters to be passed to the underlying forecast model. Defaults to {“eta”: 0.17, “max_depth”: 3, “lambda”: 1, “alpha”: 8}
module_type (str, default="XGBModule") – Type of gradient boosting module to use, either “XGBModule” or “LGBModule”.
linear_params (dict, default={}) – Parameters to pass to GBLinear. Defaults to {“min_hess”: 0.0, “lambd”: 0.1, “lr”: 0.9}
changepoint_params (dict, default={}) –
Parameters for changepoint detection and modeling. Defaults to: {
”n_changepoints”: 32, # how many changepoints to consider “eta”: 0.9, “max_depth”: 9, “lambda”: 6.5, “alpha”: 3.8, “cp_gap”: 0.5, # portion of time series allowing changepoints “cp_train_gap”: 4 # how many training rounds to NOT update the periodic component
}
- Variables:
nrounds (int) – Number of training rounds for the model.
params (dict) – Additional parameters passed to the forecast model.
model (ForecastModule or None) – Trained forecast model instance. Set after fitting.
losses (list) – List of loss values recorded at each training iteration.
- fit(X, y)[source]
Trains the forecast model using the input features X and target variable y. X must contain the datetime column ‘ds’.
- predict(X, components=False)[source]
Predicts target values based on the input features X. If components=True, returns tuple of (trend, periodic, changepoint) components.
Notes
The model uses a linear trend + periodic function + changepoints via XGBModule or LGBModule. The loss function used is Mean Squared Error (MSE). The trend component uses GBLinear. Changepoints allow the model to capture changes in the time series trend.
- class gbnet.models.forecasting.ForecastModule(n, params=None, module_type='XGBModule', trend_type='GBLinear', linear_params={}, changepoint_params={})[source]
Bases:
torch.nn.ModulePyTorch module for time series forecasting.
This module combines a linear trend component with a periodic function and changepoints learned through gradient boosting to model time series data. The trend is modeled using GBLinear layer, while the periodic patterns and changepoints are captured by either XGBoost or LightGBM.
- Parameters:
n (int) – Number of samples in training data
params (dict, optional) – Parameters passed to the gradient boosting model. Defaults to None.
module_type (str, optional) – Type of gradient boosting module to use, either “XGBModule” or “LGBModule”. Defaults to “XGBModule”.
trend_type (str, optional) – Type of trend model to use, either “PyTorch” or “GBLinear”. Defaults to “GBLinear”.
linear_params (dict, optional) – Parameters passed to GBLinear trend model if trend_type=”GBLinear”. Defaults to {}.
changepoint_params (dict, optional) –
Parameters for changepoint detection and modeling. Defaults to: {
”n_changepoints”: 100, “gbmodule”: “XGBModule”, “cp_gap”: 0.9, “cp_train_gap”: 10
}
- Variables:
trend (Union[torch.nn.Linear, GBLinear]) – Linear layer for modeling trend component, either PyTorch Linear or GBLinear
bn (torch.nn.BatchNorm1d) – Batch normalization layer (only used with PyTorch trend)
periodic_fn (XGBModule or LGBModule) – Gradient boosting module for modeling periodic patterns
trend_fn (XGBModule or LGBModule) – Gradient boosting module for modeling changepoints
initialized (bool) – Whether the model has been initialized with initial trend estimates
trend_type (str) – Type of trend model being used
- gbnet.models.forecasting.piecewise_linear_function(changepoints, timepoints)[source]
Calculate a piecewise linear function based on changepoints evaluated at timepoints.
For every tp in timepoints, compute: Sum_{(cp1, cp2) such that cp1 < tp} cp2 * (tp - cp1)
- Parameters:
changepoints – torch.Tensor of shape [N, 2] where: - First column (cp1) contains the positions of changepoints - Second column (cp2) contains the slopes at those changepoints
timepoints – torch.Tensor of shape [M] containing points to evaluate the function
- Returns:
torch.Tensor of shape [M] containing the evaluated function at each timepoint