Forecast a univariate series using DeepNPTS
DeepNPTS (Deep Non-Parametric Time Series forecaster) is a deep learning model that learns to forecast by taking a weighted sum of the values in the lookback window. Use this guide to train the model and generate point forecasts for a single time series.
Prerequisites
- Chronax installed.
- Data must be a 1-D array (
y) of typejnp.ndarray, typicallyfloat32. - The model requires a forecast horizon (
h) to be defined upon initialization.
Steps
1. Prepare the data and initialize the model
Import DeepNPTS and define the forecast horizon (h). The model will automatically determine the lookback window (input_size) based on h if not explicitly set (default is 3*h).
import jax.numpy as jnp
from chronax.models import DeepNPTS
# Create synthetic data (100 observations)
y = jnp.sin(jnp.linspace(0, 10 * jnp.pi, 100), dtype=jnp.float32)
# Define the forecast horizon (e.g., 10 steps ahead)
H = 10
# Initialize the model. We set max_steps lower than the default (1000)
# for faster execution in this example.
model = DeepNPTS(h=H, max_steps=500, random_seed=42)
2. Train the model
Train the model using the fit method, passing the time series y. Training involves iterating over the data for the number of steps defined by max_steps.
# Fit the model to the time series y
model.fit(y)
print("DeepNPTS training complete.")
3. Generate the forecast with prediction intervals
Use the predict method to generate the forecast for the horizon h. While DeepNPTS is a point forecaster, Chronax uses conformal prediction techniques to generate prediction intervals if you specify a level.
# Generate the forecast for H steps with 90% prediction intervals
forecast_output = model.predict(h=H, level=0.90)
# The output is a dictionary. Extract the mean forecast and bounds.
mean_forecast = forecast_output["mean"]
lower_bound = forecast_output["lo-90"]
upper_bound = forecast_output["hi-90"]
print(f"Forecast shape: {mean_forecast.shape}")
print(f"Mean forecast at h=1: {mean_forecast[0]:.4f}")
print(f"90% Interval at h=1: [{lower_bound[0]:.4f}, {upper_bound[0]:.4f}]")
Full example
import jax.numpy as jnp
from chronax.models import DeepNPTS
# 1. Prepare the data and define the model
# Create synthetic data
y = jnp.sin(jnp.linspace(0, 10 * jnp.pi, 100), dtype=jnp.float32)
H = 10
# Initialize the model, setting the horizon and reducing steps for speed
model = DeepNPTS(h=H, max_steps=500, random_seed=42)
# 2. Train the model
print("Starting training...")
model.fit(y)
# 3. Generate the forecast with 95% prediction intervals
forecast_output = model.predict(h=H, level=0.95)
# 4. Display results
mean_forecast = forecast_output["mean"]
lower_bound = forecast_output["lo-95"]
upper_bound = forecast_output["hi-95"]
print("\n--- Forecast Results ---")
print(f"Horizon (H): {H}")
print(f"Mean forecast (first 3 steps): {mean_forecast[:3]}")
print(f"95% Interval (first step): [{lower_bound[0]:.4f}, {upper_bound[0]:.4f}]")
Next steps
- Customize the model architecture by setting
hidden_sizeorn_layers. - Change the loss function using the
lossparameter (e.g.,DeepNPTS(h=H, loss="mse")). - Stabilize variance by enabling Box-Cox transformation:
DeepNPTS(h=H, use_boxcox=True). - Learn how to use the
forecastmethod for rolling or recursive forecasting.