Forecasting API¶
tirex2.ForecastModel ¶
High-level, batched forecasting interface around a TiRex2 backbone.
The wrapper takes ownership of the model only as a delegate: it batches the
TimeseriesType it is given (building them from a GluonTS
dataset in forecast_gluon), feeds them to TiRex2.predict, and formats the
per-series quantile forecasts into the requested output type. Attribute access falls
through to the wrapped model, so the backbone's own methods (e.g. predict) remain
reachable on the wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TiRex2
|
An instantiated, ready-for-inference backbone exposing
|
required |
Source code in src/tirex2/api_adapter/forecast.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
forecast ¶
forecast(
timeseries: list[TimeseriesType],
prediction_length: int,
*,
output_type: ForecastOutputType = "torch",
batch_size: int = 512,
yield_per_batch: bool = False,
**predict_kwargs,
)
Forecast a list of TimeseriesType objects, each with a target and optional covariates.
Extra predict_kwargs are forwarded verbatim to TiRex2.predict.
In particular tta_sign_flip controls sign-flip test-time augmentation
(roughly doubles inference cost), and tta_diff controls postprocessor
differencing; when omitted, the checkpoint's configured defaults
(model-config.yaml) are used. Pass True/False to override.
Examples:
>>> import torch
>>> from tirex2 import TimeseriesType, load_model
>>> model = load_model("NX-AI/TiRex-2", device="cpu")
>>> ts = TimeseriesType(target=torch.randn(1, 128), past_covariates=None, future_covariates=None)
>>> forecasts = model.forecast([ts], prediction_length=32, output_type="numpy")
>>> forecasts[0].shape
(1, 9, 32)
Source code in src/tirex2/api_adapter/forecast.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
forecast_gluon ¶
forecast_gluon(
gluonDataset,
prediction_length: int,
*,
output_type: ForecastOutputType = "torch",
batch_size: int = 512,
yield_per_batch: bool = False,
multivariate: bool = False,
data_kwargs: dict | None = None,
**predict_kwargs,
)
Forecast every entry of a GluonTS dataset, carrying its covariates and metadata through.
With multivariate=False (default) each target variate is rendered as its own
univariate QuantileForecast; with multivariate=True each series yields a single
forecast retaining the variate axis, so a multivariate dataset is scored jointly rather
than channel-by-channel. The flag only affects output_type="gluonts" formatting.
Extra predict_kwargs are forwarded verbatim to TiRex2.predict.
In particular tta_sign_flip controls sign-flip test-time augmentation
(roughly doubles inference cost), and tta_diff controls postprocessor
differencing; when omitted, the checkpoint's configured defaults
(model-config.yaml) are used. Pass True/False to override.
Source code in src/tirex2/api_adapter/forecast.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
forecast_df ¶
forecast_df(
df: IntoDataFrame,
prediction_length: int,
*,
id_column: str | None = None,
timestamp_column: str | None = None,
target: str | Sequence[str] | None = None,
past_covariates: str | Sequence[str] | None = None,
future_covariates: str | Sequence[str] | None = None,
future_df: IntoDataFrame | None = None,
output_type: Literal[
"dataframe", "pandas"
] = "dataframe",
batch_size: int = 512,
yield_per_batch: bool = False,
**predict_kwargs,
)
Forecast one or more series from an eager dataframe.
df can be any eager dataframe supported by
narwhals. Use id_column for multiple
series and timestamp_column or a pandas DatetimeIndex for the time axis.
By default, all numeric columns except ids, timestamps and covariates are targets.
Joint Forecasting: Targets within a series are forecast jointly. To forecast columns
independently, reshape them into long format and identify each series with id_column.
Future Covariates: Supply known future covariate values in future_df, using the same
layout as df and timestamps that match the forecast steps. Past values of future
covariates are taken from df.
Output: The result has one row per series, target and forecast step, with a median
prediction and columns for each quantile. By default, it uses the same dataframe library
as df; set output_type="pandas" to get pandas instead.
Batching: batch_size counts series, not rows. Set yield_per_batch=True to yield
one result per batch. Extra predict_kwargs are passed to TiRex2.predict.
Examples:
Forecast monthly sales from a pandas dataframe:
>>> import pandas as pd
>>> from tirex2 import load_model
>>> df = pd.DataFrame({
... "timestamp": pd.date_range("2020-01-01", periods=24, freq="MS"),
... "sales": range(24),
... })
>>> model = load_model("NX-AI/TiRex-2", device="cpu")
>>> forecast = model.forecast_df(df, 4, target="sales", timestamp_column="timestamp")
>>> forecast
timestamp target prediction ... 0.7 0.8 0.9
0 2022-01-01 sales 23.985064 ... 24.005989 24.018473 24.036558
1 2022-02-01 sales 24.975126 ... 25.003880 25.020775 25.046469
2 2022-03-01 sales 25.964767 ... 26.000420 26.020782 26.051968
3 2022-04-01 sales 26.956121 ... 26.995924 27.018373 27.053928
[4 rows x 12 columns]
Add a calendar feature whose future values are already known:
>>> df["month"] = df["timestamp"].dt.month.astype("float32")
>>> future_df = pd.DataFrame({"timestamp": pd.date_range("2022-01-01", periods=4, freq="MS")})
>>> future_df["month"] = future_df["timestamp"].dt.month.astype("float32")
>>> forecast = model.forecast_df(
... df, 4, target="sales", timestamp_column="timestamp",
... future_covariates="month", future_df=future_df,
... )
>>> forecast
timestamp target prediction ... 0.7 0.8 0.9
0 2022-01-01 sales 23.984356 ... 24.001610 24.012691 24.030060
1 2022-02-01 sales 24.972900 ... 24.998695 25.014589 25.039955
2 2022-03-01 sales 25.958410 ... 25.990089 26.009071 26.039722
3 2022-04-01 sales 26.944197 ... 26.980700 27.002466 27.038290
[4 rows x 12 columns]
See the dataframe how-to guide for more examples.
Calendar-aware inference
Without pandas installed, the forecast time step is estimated from the most common
gap between input timestamps. Calendar schedules such as month starts and local times
across daylight-saving changes may drift. Install pandas for calendar-aware inference.
Source code in src/tirex2/api_adapter/forecast.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
forecast_fev ¶
forecast_fev(
window: EvaluationWindow,
prediction_length: int,
*,
output_type: ForecastOutputType = "torch",
batch_size: int = 512,
yield_per_batch: bool = False,
data_kwargs: dict | None = None,
quantile_levels: list[float] | None = None,
return_inference_time: bool = False,
**predict_kwargs,
)
Forecast a single FEV evaluation window.
The call mirrors forecast_gluon: convert the external dataset
representation into TimeseriesType, then delegate batching,
prediction and output rendering to the common forecast path. Use
output_type="fev" to return predictions in the format accepted by
fev.Task.evaluation_summary. Pass return_inference_time=True to
also return the model-only prediction time, excluding FEV input
conversion and final DatasetDict construction.
Source code in src/tirex2/api_adapter/forecast.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |