Forecast a univariate series using Chronax
This guide shows you how to initialize a model, fit it to historical data, and generate future predictions for a single time series. Use this approach when you have a standard time series forecasting problem without exogenous variables.
Prerequisites
- Chronax installed (
pip install chronax). import jax.numpy as jnpfor array handling.- Your historical data
ymust be a 1-Djnp.ndarrayof typefloat32.
Steps
1. Prepare the data
Define your historical time series data y. Chronax models expect JAX arrays.
import jax.numpy as jnp
# Example: 100 historical observations
y = jnp.array([
10.1, 10.5, 10.3, 10.8, 11.0, 11.2, 11.5, 11.3, 11.7, 12.0
], dtype=jnp.float32)
# In a real scenario, y would contain hundreds or thousands of points.
2. Initialize the forecasting model
Import and instantiate a Chronax model. We will use AutoARIMA as a robust baseline model.
from chronax.models import AutoARIMA
# Initialize the model.
# TODO: confirm AutoARIMA constructor arguments against the API
model = AutoARIMA()
3. Fit the model to the historical data
Use the fit method, passing only the historical series y.
# Fit the model. The model learns the parameters from the data.
# The fit method returns the fitted model instance.
fitted_model = model.fit(y)
4. Generate the forecast
Use the predict method on the fitted model, specifying the forecast horizon h (the number of steps into the future you want to predict).
# Forecast 5 steps into the future (h=5)
h = 5
out = fitted_model.predict(h=h)
# The result is a dictionary. Access the mean forecast array:
forecast_mean = out["mean"]
print(f"Forecasted values for the next {h} steps:")
print(forecast_mean)
Full example
import jax.numpy as jnp
from chronax.models import AutoARIMA
# 1. Prepare the data
y = jnp.array([
10.1, 10.5, 10.3, 10.8, 11.0, 11.2, 11.5, 11.3, 11.7, 12.0,
12.2, 12.5, 12.3, 12.8, 13.0, 13.2, 13.5, 13.3, 13.7, 14.0
], dtype=jnp.float32)
# 2. Initialize the forecasting model
# TODO: confirm AutoARIMA constructor arguments against the API
model = AutoARIMA()
# 3. Fit the model to the historical data
fitted_model = model.fit(y)
# 4. Generate the forecast
h = 5
out = fitted_model.predict(h=h)
forecast_mean = out["mean"]
print(f"Historical data length: {len(y)}")
print(f"Forecast horizon (h): {h}")
print(f"Forecast mean: {forecast_mean}")
Next steps
- Learn how to add prediction intervals using the
levelargument inpredict. - Explore other models in
chronax.models, such asETSorTFT. - Understand the
model.forecast()method for combined fitting and prediction. - See how to incorporate exogenous features using the
Xargument infit.