xtft 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. xtft-0.1.0/LICENSE +28 -0
  2. xtft-0.1.0/PKG-INFO +310 -0
  3. xtft-0.1.0/README.md +251 -0
  4. xtft-0.1.0/pyproject.toml +191 -0
  5. xtft-0.1.0/setup.cfg +4 -0
  6. xtft-0.1.0/src/xtft/__init__.py +80 -0
  7. xtft-0.1.0/src/xtft/_configs.py +220 -0
  8. xtft-0.1.0/src/xtft/_deps.py +145 -0
  9. xtft-0.1.0/src/xtft/_dummies.py +119 -0
  10. xtft-0.1.0/src/xtft/_log.py +137 -0
  11. xtft-0.1.0/src/xtft/_log.yml +59 -0
  12. xtft-0.1.0/src/xtft/_optdeps.py +162 -0
  13. xtft-0.1.0/src/xtft/_util.py +44 -0
  14. xtft-0.1.0/src/xtft/api/bunch.py +721 -0
  15. xtft-0.1.0/src/xtft/api/docs.py +2690 -0
  16. xtft-0.1.0/src/xtft/api/property.py +2256 -0
  17. xtft-0.1.0/src/xtft/api/summary.py +180 -0
  18. xtft-0.1.0/src/xtft/api/types.py +256 -0
  19. xtft-0.1.0/src/xtft/api/util.py +5175 -0
  20. xtft-0.1.0/src/xtft/backends/__init__.py +31 -0
  21. xtft-0.1.0/src/xtft/backends/_framework.py +263 -0
  22. xtft-0.1.0/src/xtft/backends/array/base.py +138 -0
  23. xtft-0.1.0/src/xtft/backends/array/cupy.py +303 -0
  24. xtft-0.1.0/src/xtft/backends/array/dask.py +466 -0
  25. xtft-0.1.0/src/xtft/backends/array/devices.py +360 -0
  26. xtft-0.1.0/src/xtft/backends/array/nn.py +1270 -0
  27. xtft-0.1.0/src/xtft/backends/array/numpy.py +261 -0
  28. xtft-0.1.0/src/xtft/backends/array/scipy.py +245 -0
  29. xtft-0.1.0/src/xtft/backends/array/selector.py +1017 -0
  30. xtft-0.1.0/src/xtft/compat/__init__.py +41 -0
  31. xtft-0.1.0/src/xtft/compat/_config.py +199 -0
  32. xtft-0.1.0/src/xtft/compat/keras.py +925 -0
  33. xtft-0.1.0/src/xtft/compat/keras_fit.py +475 -0
  34. xtft-0.1.0/src/xtft/compat/kt.py +125 -0
  35. xtft-0.1.0/src/xtft/compat/numpy.py +98 -0
  36. xtft-0.1.0/src/xtft/compat/pandas.py +660 -0
  37. xtft-0.1.0/src/xtft/compat/scipy.py +393 -0
  38. xtft-0.1.0/src/xtft/compat/sklearn.py +1150 -0
  39. xtft-0.1.0/src/xtft/compat/tf.py +1182 -0
  40. xtft-0.1.0/src/xtft/compat/torch.py +99 -0
  41. xtft-0.1.0/src/xtft/compat/types.py +85 -0
  42. xtft-0.1.0/src/xtft/core/array_manager.py +4649 -0
  43. xtft-0.1.0/src/xtft/core/checks.py +7479 -0
  44. xtft-0.1.0/src/xtft/core/diagnose_q.py +2237 -0
  45. xtft-0.1.0/src/xtft/core/handlers.py +2685 -0
  46. xtft-0.1.0/src/xtft/core/io.py +2705 -0
  47. xtft-0.1.0/src/xtft/core/utils.py +3128 -0
  48. xtft-0.1.0/src/xtft/data/__init__.py +19 -0
  49. xtft-0.1.0/src/xtft/data/panel.py +549 -0
  50. xtft-0.1.0/src/xtft/data/seasonality.py +203 -0
  51. xtft-0.1.0/src/xtft/decorators.py +5154 -0
  52. xtft-0.1.0/src/xtft/evaluation/__init__.py +57 -0
  53. xtft-0.1.0/src/xtft/evaluation/bands.py +489 -0
  54. xtft-0.1.0/src/xtft/evaluation/conformal.py +637 -0
  55. xtft-0.1.0/src/xtft/evaluation/metrics.py +559 -0
  56. xtft-0.1.0/src/xtft/evaluation/stats.py +326 -0
  57. xtft-0.1.0/src/xtft/exceptions.py +336 -0
  58. xtft-0.1.0/src/xtft/metrics/__init__.py +32 -0
  59. xtft-0.1.0/src/xtft/metrics/_cas.py +470 -0
  60. xtft-0.1.0/src/xtft/metrics/_metrics.py +3500 -0
  61. xtft-0.1.0/src/xtft/metrics/_registry.py +217 -0
  62. xtft-0.1.0/src/xtft/nn/__init__.py +124 -0
  63. xtft-0.1.0/src/xtft/nn/tf/__init__.py +60 -0
  64. xtft-0.1.0/src/xtft/nn/tf/_shapes.py +1124 -0
  65. xtft-0.1.0/src/xtft/nn/tf/_tensor_validation.py +3845 -0
  66. xtft-0.1.0/src/xtft/nn/tf/_utils.py +7549 -0
  67. xtft-0.1.0/src/xtft/nn/tf/anomaly_detection.py +1065 -0
  68. xtft-0.1.0/src/xtft/nn/tf/calibration.py +312 -0
  69. xtft-0.1.0/src/xtft/nn/tf/callbacks.py +1317 -0
  70. xtft-0.1.0/src/xtft/nn/tf/components/__init__.py +154 -0
  71. xtft-0.1.0/src/xtft/nn/tf/components/_attention_utils.py +160 -0
  72. xtft-0.1.0/src/xtft/nn/tf/components/_config.py +207 -0
  73. xtft-0.1.0/src/xtft/nn/tf/components/_loss_utils.py +215 -0
  74. xtft-0.1.0/src/xtft/nn/tf/components/_temporal_utils.py +469 -0
  75. xtft-0.1.0/src/xtft/nn/tf/components/attention.py +1195 -0
  76. xtft-0.1.0/src/xtft/nn/tf/components/encoder_decoder.py +616 -0
  77. xtft-0.1.0/src/xtft/nn/tf/components/gating_norm.py +1081 -0
  78. xtft-0.1.0/src/xtft/nn/tf/components/heads.py +694 -0
  79. xtft-0.1.0/src/xtft/nn/tf/components/layer_utils.py +360 -0
  80. xtft-0.1.0/src/xtft/nn/tf/components/losses.py +940 -0
  81. xtft-0.1.0/src/xtft/nn/tf/components/masks.py +130 -0
  82. xtft-0.1.0/src/xtft/nn/tf/components/misc.py +1066 -0
  83. xtft-0.1.0/src/xtft/nn/tf/components/temporal.py +357 -0
  84. xtft-0.1.0/src/xtft/nn/tf/components/utils.py +243 -0
  85. xtft-0.1.0/src/xtft/nn/tf/custom_metrics.py +211 -0
  86. xtft-0.1.0/src/xtft/nn/tf/hybrid/__init__.py +11 -0
  87. xtft-0.1.0/src/xtft/nn/tf/hybrid/_base_extreme.py +636 -0
  88. xtft-0.1.0/src/xtft/nn/tf/hybrid/_super_xtft.py +677 -0
  89. xtft-0.1.0/src/xtft/nn/tf/hybrid/_xtft.py +607 -0
  90. xtft-0.1.0/src/xtft/nn/tf/hybrid/utils.py +852 -0
  91. xtft-0.1.0/src/xtft/nn/tf/keras_metrics.py +1313 -0
  92. xtft-0.1.0/src/xtft/nn/tf/keras_validator.py +643 -0
  93. xtft-0.1.0/src/xtft/nn/tf/losses.py +1139 -0
  94. xtft-0.1.0/src/xtft/nn/tf/transformers/__init__.py +10 -0
  95. xtft-0.1.0/src/xtft/nn/tf/transformers/_adj_tft.py +777 -0
  96. xtft-0.1.0/src/xtft/nn/tf/transformers/_tft.py +1060 -0
  97. xtft-0.1.0/src/xtft/nn/torch/__init__.py +16 -0
  98. xtft-0.1.0/src/xtft/nn/torch/layers/__init__.py +24 -0
  99. xtft-0.1.0/src/xtft/nn/torch/layers/attention.py +332 -0
  100. xtft-0.1.0/src/xtft/nn/torch/layers/grn.py +215 -0
  101. xtft-0.1.0/src/xtft/nn/torch/layers/missing.py +226 -0
  102. xtft-0.1.0/src/xtft/nn/torch/layers/multiresolution.py +348 -0
  103. xtft-0.1.0/src/xtft/nn/torch/layers/variable_selection.py +193 -0
  104. xtft-0.1.0/src/xtft/nn/torch/models/__init__.py +5 -0
  105. xtft-0.1.0/src/xtft/nn/torch/models/xtft.py +638 -0
  106. xtft-0.1.0/src/xtft/nn/torch/probabilistic/__init__.py +11 -0
  107. xtft-0.1.0/src/xtft/nn/torch/probabilistic/losses.py +139 -0
  108. xtft-0.1.0/src/xtft/nn/torch/probabilistic/quantile_head.py +154 -0
  109. xtft-0.1.0/src/xtft/nn/torch/trainer.py +357 -0
  110. xtft-0.1.0/src/xtft/utils/_array_api.py +183 -0
  111. xtft-0.1.0/src/xtft/utils/_dependency.py +187 -0
  112. xtft-0.1.0/src/xtft/utils/base_utils.py +1532 -0
  113. xtft-0.1.0/src/xtft/utils/calibrate.py +950 -0
  114. xtft-0.1.0/src/xtft/utils/data_utils.py +1278 -0
  115. xtft-0.1.0/src/xtft/utils/deps_utils.py +1401 -0
  116. xtft-0.1.0/src/xtft/utils/forecast_utils.py +4507 -0
  117. xtft-0.1.0/src/xtft/utils/generic_utils.py +4018 -0
  118. xtft-0.1.0/src/xtft/utils/io_utils.py +3465 -0
  119. xtft-0.1.0/src/xtft/utils/sequence_utils.py +1701 -0
  120. xtft-0.1.0/src/xtft/utils/shapes.py +1183 -0
  121. xtft-0.1.0/src/xtft/utils/sys_utils.py +3161 -0
  122. xtft-0.1.0/src/xtft/utils/ts_utils.py +4975 -0
  123. xtft-0.1.0/src/xtft/utils/validator.py +7647 -0
  124. xtft-0.1.0/src/xtft/utils/version.py +581 -0
  125. xtft-0.1.0/src/xtft.egg-info/PKG-INFO +310 -0
  126. xtft-0.1.0/src/xtft.egg-info/SOURCES.txt +136 -0
  127. xtft-0.1.0/src/xtft.egg-info/dependency_links.txt +1 -0
  128. xtft-0.1.0/src/xtft.egg-info/requires.txt +40 -0
  129. xtft-0.1.0/src/xtft.egg-info/top_level.txt +1 -0
  130. xtft-0.1.0/tests/test_adaptive_conformal.py +152 -0
  131. xtft-0.1.0/tests/test_backends.py +157 -0
  132. xtft-0.1.0/tests/test_bands.py +169 -0
  133. xtft-0.1.0/tests/test_data.py +137 -0
  134. xtft-0.1.0/tests/test_evaluation.py +177 -0
  135. xtft-0.1.0/tests/test_imports.py +113 -0
  136. xtft-0.1.0/tests/test_seasonality.py +55 -0
  137. xtft-0.1.0/tests/test_stats.py +95 -0
  138. xtft-0.1.0/tests/test_tf_models_smoke.py +61 -0
xtft-0.1.0/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, earthai-tech
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
xtft-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,310 @@
1
+ Metadata-Version: 2.4
2
+ Name: xtft
3
+ Version: 0.1.0
4
+ Summary: Covariate-aware probabilistic forecasting with calibrated, shift-robust uncertainty (PyTorch and TensorFlow backends)
5
+ Author-email: earthai-tech <etanoyau@gmail.com>
6
+ License-Expression: BSD-3-Clause
7
+ Project-URL: Homepage, https://github.com/earthai-tech/xtft
8
+ Project-URL: Documentation, https://xtft.readthedocs.io
9
+ Project-URL: Source, https://github.com/earthai-tech/xtft
10
+ Project-URL: Issues, https://github.com/earthai-tech/xtft/issues
11
+ Keywords: forecasting,probabilistic forecasting,time series,temporal fusion transformer,conformal prediction,deep learning
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: numpy<2.3,>=1.26
25
+ Requires-Dist: pandas>=2.2
26
+ Requires-Dist: scipy<2.0,>=1.13
27
+ Requires-Dist: scikit-learn>=1.4
28
+ Requires-Dist: statsmodels>=0.14
29
+ Requires-Dist: matplotlib>=3.6
30
+ Requires-Dist: joblib>=1.3
31
+ Requires-Dist: tqdm>=4.65
32
+ Requires-Dist: PyYAML>=6.0
33
+ Requires-Dist: packaging>=23
34
+ Requires-Dist: psutil>=5.9
35
+ Provides-Extra: torch
36
+ Requires-Dist: torch>=2.1; extra == "torch"
37
+ Provides-Extra: tf
38
+ Requires-Dist: tensorflow<3.0,>=2.20; extra == "tf"
39
+ Provides-Extra: bench
40
+ Requires-Dist: neuralforecast>=1.7; extra == "bench"
41
+ Requires-Dist: statsforecast>=1.7; extra == "bench"
42
+ Requires-Dist: datasetsforecast>=1.0; extra == "bench"
43
+ Requires-Dist: lightgbm>=4.0; extra == "bench"
44
+ Requires-Dist: xgboost>=2.0; extra == "bench"
45
+ Provides-Extra: dev
46
+ Requires-Dist: pytest>=8; extra == "dev"
47
+ Requires-Dist: ruff>=0.5; extra == "dev"
48
+ Provides-Extra: test
49
+ Requires-Dist: pytest>=8; extra == "test"
50
+ Requires-Dist: pytest-cov>=5; extra == "test"
51
+ Provides-Extra: docs
52
+ Requires-Dist: sphinx>=7; extra == "docs"
53
+ Requires-Dist: pydata-sphinx-theme>=0.15; extra == "docs"
54
+ Requires-Dist: numpydoc>=1.6; extra == "docs"
55
+ Requires-Dist: sphinx-design>=0.5; extra == "docs"
56
+ Requires-Dist: sphinx-copybutton>=0.5; extra == "docs"
57
+ Requires-Dist: sphinx-gallery>=0.15; extra == "docs"
58
+ Dynamic: license-file
59
+
60
+ <div align="center">
61
+ <picture>
62
+ <source media="(prefers-color-scheme: dark)" srcset="docs/_static/logo/xtft-logo-dark.svg">
63
+ <img src="docs/_static/logo/xtft-logo.svg" alt="XTFT: covariate-aware probabilistic forecasting" width="560"/>
64
+ </picture>
65
+ <br/>
66
+ <em>Forecast many related time series at once, with static, observed and known-future inputs, and with intervals that keep the coverage they claim.</em>
67
+ <br/><br/>
68
+ <a href="https://pypi.org/project/xtft/"><img alt="PyPI" src="https://img.shields.io/pypi/v/xtft?label=PyPI&color=orange&logo=pypi&logoColor=white"/></a>
69
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/release.yml"><img alt="Release" src="https://github.com/earthai-tech/xtft/actions/workflows/release.yml/badge.svg"/></a>
70
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/tests.yml"><img alt="Tests" src="https://github.com/earthai-tech/xtft/actions/workflows/tests.yml/badge.svg?branch=master"/></a>
71
+ <a href="https://codecov.io/gh/earthai-tech/xtft"><img alt="Coverage" src="https://codecov.io/gh/earthai-tech/xtft/branch/master/graph/badge.svg"/></a>
72
+ <a href="https://xtft.readthedocs.io/en/latest/"><img alt="Docs" src="https://github.com/earthai-tech/xtft/actions/workflows/docs.yml/badge.svg?branch=master"/></a>
73
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/lint.yml"><img alt="Lint" src="https://github.com/earthai-tech/xtft/actions/workflows/lint.yml/badge.svg?branch=master"/></a>
74
+ <br/>
75
+ <img alt="Python" src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-3776ab?logo=python&logoColor=white"/>
76
+ <img alt="Backends: PyTorch (default) | TensorFlow/Keras 3" src="https://img.shields.io/badge/backend-PyTorch%20%7C%20TensorFlow-ee4c2c?logo=pytorch&logoColor=white"/>
77
+ <img alt="Devices" src="https://img.shields.io/badge/devices-CPU%20%7C%20CUDA%20%7C%20MPS-76b900?logo=nvidia&logoColor=white"/>
78
+ <img alt="Platform" src="https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20Windows-lightgrey"/>
79
+ <br/>
80
+ <a href="https://github.com/astral-sh/ruff"><img alt="Code style: ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json"/></a>
81
+ <a href="https://numpydoc.readthedocs.io/"><img alt="Docstrings: numpydoc" src="https://img.shields.io/badge/docstrings-numpydoc-4dabcf"/></a>
82
+ <a href="https://docs.pytest.org/"><img alt="Tested with pytest" src="https://img.shields.io/badge/tested%20with-pytest-0a9edc?logo=pytest&logoColor=white"/></a>
83
+ <a href="https://opensource.org/licenses/BSD-3-Clause"><img alt="License: BSD-3-Clause" src="https://img.shields.io/badge/license-BSD--3--Clause-blue"/></a>
84
+ <a href="CONTRIBUTING.md"><img alt="PRs welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen"/></a>
85
+ <br/><br/>
86
+
87
+ **[Documentation](https://xtft.readthedocs.io)** ·
88
+ [Quick start](#-quick-start) ·
89
+ [Examples](https://xtft.readthedocs.io/en/latest/auto_examples/index.html) ·
90
+ [API](https://xtft.readthedocs.io/en/latest/api/index.html) ·
91
+ [Contributing](CONTRIBUTING.md)
92
+
93
+ </div>
94
+
95
+ ---
96
+
97
+ ## 🧭 Why XTFT?
98
+
99
+ Panels of related series often share covariates: sales of many
100
+ products with prices and promotions, electricity prices with load
101
+ forecasts, river flow in many basins with weather. **XTFT**, the
102
+ eXogenous-aware Temporal Fusion Transformer, is built for them. It
103
+ extends the Temporal Fusion Transformer where such panels break
104
+ it, and ships a calibration layer that works for **any** model.
105
+
106
+ | | |
107
+ |---|---|
108
+ | 🧩 **Three kinds of input** | static attributes, observed past covariates and inputs known in advance, weighed by variable selection at every step |
109
+ | 🌊 **Level shift** | reversible instance normalisation of each window, so forecasts follow a level never seen in training |
110
+ | 🕳️ **Missing inputs** | pass gaps as `NaN`; masks, a learned decay and missingness-aware embeddings handle them inside the model |
111
+ | 📐 **Non-crossing quantiles** | a monotone head returns ordered quantiles by construction |
112
+ | 🛰️ **Calibration** | split-conformal and adaptive conformal intervals for the forecasts of any model |
113
+ | 🛤️ **Whole-path bands** | bands that contain the entire forecast path with the stated probability |
114
+
115
+ ## 🚀 Installation
116
+
117
+ ```bash
118
+ pip install "xtft[torch]"
119
+ ```
120
+
121
+ Requires **Python 3.10+**. Until the first release is on PyPI,
122
+ install from the repository (below).
123
+
124
+ <details>
125
+ <summary><b>Optional extras, GPUs &amp; source install</b></summary>
126
+
127
+ | Extra | Installs | For |
128
+ |---|---|---|
129
+ | `torch` | PyTorch ≥ 2.1 | the XTFT model, training and inference |
130
+ | `tf` | TensorFlow 2.20 / Keras 3 | the Keras XTFT and TFT |
131
+ | `bench` | neuralforecast, statsforecast, LightGBM, … | the benchmark scripts |
132
+ | `test`, `docs` | pytest, Sphinx and extensions | tests and documentation |
133
+
134
+ For a GPU, install the PyTorch build matching your CUDA driver from
135
+ [pytorch.org](https://pytorch.org/get-started/locally/) first.
136
+
137
+ From the repository:
138
+
139
+ ```bash
140
+ git clone https://github.com/earthai-tech/xtft.git
141
+ cd xtft
142
+ pip install -e ".[torch,dev,test]"
143
+ pytest
144
+ ```
145
+
146
+ </details>
147
+
148
+ ## ⚡ Quick start
149
+
150
+ ```python
151
+ from xtft.nn import XTFT
152
+
153
+ model = XTFT(
154
+ static_input_dim=4,
155
+ dynamic_input_dim=5, # past targets first, then covariates
156
+ future_input_dim=2,
157
+ forecast_horizon=24,
158
+ quantiles=[0.1, 0.5, 0.9],
159
+ instance_norm=True,
160
+ )
161
+ q = model(x_static, x_dynamic, x_future) # (batch, 24, 3)
162
+ ```
163
+
164
+ Shapes: `x_static` `(B, S)`, `x_dynamic` `(B, T, D)` with `NaN`
165
+ for missing values, `x_future` `(B, T + H, F)`.
166
+
167
+ <details>
168
+ <summary><b>From a data frame to trained forecasts</b></summary>
169
+
170
+ ```python
171
+ import numpy as np
172
+ from xtft.data import PanelData, PanelSpec, WindowDataset
173
+ from xtft.nn import TrainConfig, fit, predict
174
+
175
+ # Long format: one row per series and time step.
176
+ spec = PanelSpec(static=("area",), past=("temp",), known=("dow_sin", "dow_cos"))
177
+ panel = PanelData.from_frame(df, spec)
178
+ val_start = panel.split_index(val_cutoff) # per-series indices
179
+ test_start = panel.split_index(test_cutoff)
180
+ panel.fit_scaling(val_start) # training statistics only
181
+
182
+ T, H = 56, 14
183
+ first = np.zeros(panel.n_series, dtype=np.int64)
184
+ train_ds = WindowDataset(panel, panel.origins(first, val_start, T, H, 1), T, H)
185
+ val_ds = WindowDataset(panel, panel.origins(val_start - 1, test_start, T, H, 1), T, H)
186
+
187
+ history = fit(model, train_ds, val_ds, quantiles,
188
+ TrainConfig(max_epochs=50, patience=5, device="auto"))
189
+ q_val = predict(model, val_ds) # (N, H, O, K), scaled units
190
+ ```
191
+
192
+ The full, runnable version is the example
193
+ [Forecast a panel of series](https://xtft.readthedocs.io/en/latest/auto_examples/plot_01_quickstart.html).
194
+
195
+ </details>
196
+
197
+ <details>
198
+ <summary><b>Calibrate the quantiles of any model</b></summary>
199
+
200
+ ```python
201
+ from xtft.evaluation import (
202
+ AdaptiveConformalCalibrator,
203
+ SimultaneousConformalBand,
204
+ path_coverage,
205
+ )
206
+
207
+ # Per-step intervals that adapt when errors drift.
208
+ aci = AdaptiveConformalCalibrator(quantiles, gamma=0.005)
209
+ q_cal = aci.fit(y_val, q_val).transform(y_test, q_test, times)
210
+
211
+ # A band that contains the whole forecast path 90% of the time.
212
+ band = SimultaneousConformalBand(quantiles, coverage=0.9)
213
+ lower, upper = band.fit(y_val, q_val).transform_online(y_test, q_test, times)
214
+ path_coverage(y_test, lower, upper)
215
+ ```
216
+
217
+ Arrays only: `y` `(N, H)`, `q` `(N, H, K)`, `times` `(N,)`. For a
218
+ panel, calibrate each series separately.
219
+
220
+ </details>
221
+
222
+ <details>
223
+ <summary><b>Score and compare forecasters</b></summary>
224
+
225
+ ```python
226
+ from xtft.evaluation import (
227
+ diebold_mariano,
228
+ evaluate_quantile_forecast,
229
+ friedman_nemenyi,
230
+ )
231
+
232
+ evaluate_quantile_forecast(y_test, q_cal, quantiles)
233
+ # -> pinball, crps_q, coverage_80, winkler_80, mae, ...
234
+
235
+ diebold_mariano(crps_a, crps_b, horizon=1) # two models
236
+ friedman_nemenyi(scores) # many models, many tasks
237
+ ```
238
+
239
+ </details>
240
+
241
+ <details>
242
+ <summary><b>Training on GPU, mixed precision, long panels</b></summary>
243
+
244
+ ```python
245
+ cfg = TrainConfig(
246
+ device="auto", # CUDA if available, else CPU ("mps" on Apple)
247
+ amp="bf16", # or "fp16" on CUDA
248
+ steps_per_epoch=200, # random batches per epoch for millions of windows
249
+ seed=0,
250
+ )
251
+ ```
252
+
253
+ </details>
254
+
255
+ <details>
256
+ <summary><b>Keras backend</b></summary>
257
+
258
+ `from xtft.nn import XTFT` follows the active backend: PyTorch by
259
+ default, Keras when TensorFlow is chosen, or when it is the only
260
+ framework installed.
261
+
262
+ ```python
263
+ import xtft
264
+
265
+ xtft.set_backend("tensorflow") # or XTFT_BACKEND=tensorflow
266
+ from xtft.nn import XTFT, TFT # Keras classes
267
+ ```
268
+
269
+ The PyTorch path never imports TensorFlow, and `xtft.evaluation` and
270
+ `xtft.data` import neither framework.
271
+
272
+ </details>
273
+
274
+ ## 📚 Documentation
275
+
276
+ | | |
277
+ |---|---|
278
+ | [Getting started](https://xtft.readthedocs.io/en/latest/getting_started/index.html) | installation, first forecast, backends |
279
+ | [User guide](https://xtft.readthedocs.io/en/latest/user_guide/index.html) | data, the model and its equations, training, missing data, calibration, evaluation |
280
+ | [Examples](https://xtft.readthedocs.io/en/latest/auto_examples/index.html) | runnable scripts with figures, as Python files or notebooks |
281
+ | [API reference](https://xtft.readthedocs.io/en/latest/api/index.html) | every public class and function |
282
+
283
+ ## 📊 Benchmarks
284
+
285
+ [`benchmarks/`](benchmarks/README.md) compares XTFT with TFT,
286
+ PatchTST, NBEATSx, DLinear, LSTM, LightGBM, LEAR and statistical
287
+ baselines under one protocol (same inputs, horizons, origins and
288
+ training budget; several seeds; shared metrics and calibration), on
289
+ electricity prices, retail sales, long-horizon series and CAMELS-US
290
+ streamflow.
291
+
292
+ ```bash
293
+ pip install -e ".[torch,bench]"
294
+ python benchmarks/run.py --dataset EPF-DE --model xtft --horizon 24
295
+ ```
296
+
297
+ ## 🤝 Contributing
298
+
299
+ Issues and pull requests are welcome. See
300
+ [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup, the
301
+ checks and the release process; work on the `dev` branch.
302
+
303
+ ## 📝 Citing
304
+
305
+ A paper describing XTFT is in preparation; until it is published,
306
+ please cite this repository.
307
+
308
+ ## ⚖️ Licence
309
+
310
+ BSD-3-Clause, see [LICENSE](LICENSE).
xtft-0.1.0/README.md ADDED
@@ -0,0 +1,251 @@
1
+ <div align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="docs/_static/logo/xtft-logo-dark.svg">
4
+ <img src="docs/_static/logo/xtft-logo.svg" alt="XTFT: covariate-aware probabilistic forecasting" width="560"/>
5
+ </picture>
6
+ <br/>
7
+ <em>Forecast many related time series at once, with static, observed and known-future inputs, and with intervals that keep the coverage they claim.</em>
8
+ <br/><br/>
9
+ <a href="https://pypi.org/project/xtft/"><img alt="PyPI" src="https://img.shields.io/pypi/v/xtft?label=PyPI&color=orange&logo=pypi&logoColor=white"/></a>
10
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/release.yml"><img alt="Release" src="https://github.com/earthai-tech/xtft/actions/workflows/release.yml/badge.svg"/></a>
11
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/tests.yml"><img alt="Tests" src="https://github.com/earthai-tech/xtft/actions/workflows/tests.yml/badge.svg?branch=master"/></a>
12
+ <a href="https://codecov.io/gh/earthai-tech/xtft"><img alt="Coverage" src="https://codecov.io/gh/earthai-tech/xtft/branch/master/graph/badge.svg"/></a>
13
+ <a href="https://xtft.readthedocs.io/en/latest/"><img alt="Docs" src="https://github.com/earthai-tech/xtft/actions/workflows/docs.yml/badge.svg?branch=master"/></a>
14
+ <a href="https://github.com/earthai-tech/xtft/actions/workflows/lint.yml"><img alt="Lint" src="https://github.com/earthai-tech/xtft/actions/workflows/lint.yml/badge.svg?branch=master"/></a>
15
+ <br/>
16
+ <img alt="Python" src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-3776ab?logo=python&logoColor=white"/>
17
+ <img alt="Backends: PyTorch (default) | TensorFlow/Keras 3" src="https://img.shields.io/badge/backend-PyTorch%20%7C%20TensorFlow-ee4c2c?logo=pytorch&logoColor=white"/>
18
+ <img alt="Devices" src="https://img.shields.io/badge/devices-CPU%20%7C%20CUDA%20%7C%20MPS-76b900?logo=nvidia&logoColor=white"/>
19
+ <img alt="Platform" src="https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20Windows-lightgrey"/>
20
+ <br/>
21
+ <a href="https://github.com/astral-sh/ruff"><img alt="Code style: ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json"/></a>
22
+ <a href="https://numpydoc.readthedocs.io/"><img alt="Docstrings: numpydoc" src="https://img.shields.io/badge/docstrings-numpydoc-4dabcf"/></a>
23
+ <a href="https://docs.pytest.org/"><img alt="Tested with pytest" src="https://img.shields.io/badge/tested%20with-pytest-0a9edc?logo=pytest&logoColor=white"/></a>
24
+ <a href="https://opensource.org/licenses/BSD-3-Clause"><img alt="License: BSD-3-Clause" src="https://img.shields.io/badge/license-BSD--3--Clause-blue"/></a>
25
+ <a href="CONTRIBUTING.md"><img alt="PRs welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen"/></a>
26
+ <br/><br/>
27
+
28
+ **[Documentation](https://xtft.readthedocs.io)** ·
29
+ [Quick start](#-quick-start) ·
30
+ [Examples](https://xtft.readthedocs.io/en/latest/auto_examples/index.html) ·
31
+ [API](https://xtft.readthedocs.io/en/latest/api/index.html) ·
32
+ [Contributing](CONTRIBUTING.md)
33
+
34
+ </div>
35
+
36
+ ---
37
+
38
+ ## 🧭 Why XTFT?
39
+
40
+ Panels of related series often share covariates: sales of many
41
+ products with prices and promotions, electricity prices with load
42
+ forecasts, river flow in many basins with weather. **XTFT**, the
43
+ eXogenous-aware Temporal Fusion Transformer, is built for them. It
44
+ extends the Temporal Fusion Transformer where such panels break
45
+ it, and ships a calibration layer that works for **any** model.
46
+
47
+ | | |
48
+ |---|---|
49
+ | 🧩 **Three kinds of input** | static attributes, observed past covariates and inputs known in advance, weighed by variable selection at every step |
50
+ | 🌊 **Level shift** | reversible instance normalisation of each window, so forecasts follow a level never seen in training |
51
+ | 🕳️ **Missing inputs** | pass gaps as `NaN`; masks, a learned decay and missingness-aware embeddings handle them inside the model |
52
+ | 📐 **Non-crossing quantiles** | a monotone head returns ordered quantiles by construction |
53
+ | 🛰️ **Calibration** | split-conformal and adaptive conformal intervals for the forecasts of any model |
54
+ | 🛤️ **Whole-path bands** | bands that contain the entire forecast path with the stated probability |
55
+
56
+ ## 🚀 Installation
57
+
58
+ ```bash
59
+ pip install "xtft[torch]"
60
+ ```
61
+
62
+ Requires **Python 3.10+**. Until the first release is on PyPI,
63
+ install from the repository (below).
64
+
65
+ <details>
66
+ <summary><b>Optional extras, GPUs &amp; source install</b></summary>
67
+
68
+ | Extra | Installs | For |
69
+ |---|---|---|
70
+ | `torch` | PyTorch ≥ 2.1 | the XTFT model, training and inference |
71
+ | `tf` | TensorFlow 2.20 / Keras 3 | the Keras XTFT and TFT |
72
+ | `bench` | neuralforecast, statsforecast, LightGBM, … | the benchmark scripts |
73
+ | `test`, `docs` | pytest, Sphinx and extensions | tests and documentation |
74
+
75
+ For a GPU, install the PyTorch build matching your CUDA driver from
76
+ [pytorch.org](https://pytorch.org/get-started/locally/) first.
77
+
78
+ From the repository:
79
+
80
+ ```bash
81
+ git clone https://github.com/earthai-tech/xtft.git
82
+ cd xtft
83
+ pip install -e ".[torch,dev,test]"
84
+ pytest
85
+ ```
86
+
87
+ </details>
88
+
89
+ ## ⚡ Quick start
90
+
91
+ ```python
92
+ from xtft.nn import XTFT
93
+
94
+ model = XTFT(
95
+ static_input_dim=4,
96
+ dynamic_input_dim=5, # past targets first, then covariates
97
+ future_input_dim=2,
98
+ forecast_horizon=24,
99
+ quantiles=[0.1, 0.5, 0.9],
100
+ instance_norm=True,
101
+ )
102
+ q = model(x_static, x_dynamic, x_future) # (batch, 24, 3)
103
+ ```
104
+
105
+ Shapes: `x_static` `(B, S)`, `x_dynamic` `(B, T, D)` with `NaN`
106
+ for missing values, `x_future` `(B, T + H, F)`.
107
+
108
+ <details>
109
+ <summary><b>From a data frame to trained forecasts</b></summary>
110
+
111
+ ```python
112
+ import numpy as np
113
+ from xtft.data import PanelData, PanelSpec, WindowDataset
114
+ from xtft.nn import TrainConfig, fit, predict
115
+
116
+ # Long format: one row per series and time step.
117
+ spec = PanelSpec(static=("area",), past=("temp",), known=("dow_sin", "dow_cos"))
118
+ panel = PanelData.from_frame(df, spec)
119
+ val_start = panel.split_index(val_cutoff) # per-series indices
120
+ test_start = panel.split_index(test_cutoff)
121
+ panel.fit_scaling(val_start) # training statistics only
122
+
123
+ T, H = 56, 14
124
+ first = np.zeros(panel.n_series, dtype=np.int64)
125
+ train_ds = WindowDataset(panel, panel.origins(first, val_start, T, H, 1), T, H)
126
+ val_ds = WindowDataset(panel, panel.origins(val_start - 1, test_start, T, H, 1), T, H)
127
+
128
+ history = fit(model, train_ds, val_ds, quantiles,
129
+ TrainConfig(max_epochs=50, patience=5, device="auto"))
130
+ q_val = predict(model, val_ds) # (N, H, O, K), scaled units
131
+ ```
132
+
133
+ The full, runnable version is the example
134
+ [Forecast a panel of series](https://xtft.readthedocs.io/en/latest/auto_examples/plot_01_quickstart.html).
135
+
136
+ </details>
137
+
138
+ <details>
139
+ <summary><b>Calibrate the quantiles of any model</b></summary>
140
+
141
+ ```python
142
+ from xtft.evaluation import (
143
+ AdaptiveConformalCalibrator,
144
+ SimultaneousConformalBand,
145
+ path_coverage,
146
+ )
147
+
148
+ # Per-step intervals that adapt when errors drift.
149
+ aci = AdaptiveConformalCalibrator(quantiles, gamma=0.005)
150
+ q_cal = aci.fit(y_val, q_val).transform(y_test, q_test, times)
151
+
152
+ # A band that contains the whole forecast path 90% of the time.
153
+ band = SimultaneousConformalBand(quantiles, coverage=0.9)
154
+ lower, upper = band.fit(y_val, q_val).transform_online(y_test, q_test, times)
155
+ path_coverage(y_test, lower, upper)
156
+ ```
157
+
158
+ Arrays only: `y` `(N, H)`, `q` `(N, H, K)`, `times` `(N,)`. For a
159
+ panel, calibrate each series separately.
160
+
161
+ </details>
162
+
163
+ <details>
164
+ <summary><b>Score and compare forecasters</b></summary>
165
+
166
+ ```python
167
+ from xtft.evaluation import (
168
+ diebold_mariano,
169
+ evaluate_quantile_forecast,
170
+ friedman_nemenyi,
171
+ )
172
+
173
+ evaluate_quantile_forecast(y_test, q_cal, quantiles)
174
+ # -> pinball, crps_q, coverage_80, winkler_80, mae, ...
175
+
176
+ diebold_mariano(crps_a, crps_b, horizon=1) # two models
177
+ friedman_nemenyi(scores) # many models, many tasks
178
+ ```
179
+
180
+ </details>
181
+
182
+ <details>
183
+ <summary><b>Training on GPU, mixed precision, long panels</b></summary>
184
+
185
+ ```python
186
+ cfg = TrainConfig(
187
+ device="auto", # CUDA if available, else CPU ("mps" on Apple)
188
+ amp="bf16", # or "fp16" on CUDA
189
+ steps_per_epoch=200, # random batches per epoch for millions of windows
190
+ seed=0,
191
+ )
192
+ ```
193
+
194
+ </details>
195
+
196
+ <details>
197
+ <summary><b>Keras backend</b></summary>
198
+
199
+ `from xtft.nn import XTFT` follows the active backend: PyTorch by
200
+ default, Keras when TensorFlow is chosen, or when it is the only
201
+ framework installed.
202
+
203
+ ```python
204
+ import xtft
205
+
206
+ xtft.set_backend("tensorflow") # or XTFT_BACKEND=tensorflow
207
+ from xtft.nn import XTFT, TFT # Keras classes
208
+ ```
209
+
210
+ The PyTorch path never imports TensorFlow, and `xtft.evaluation` and
211
+ `xtft.data` import neither framework.
212
+
213
+ </details>
214
+
215
+ ## 📚 Documentation
216
+
217
+ | | |
218
+ |---|---|
219
+ | [Getting started](https://xtft.readthedocs.io/en/latest/getting_started/index.html) | installation, first forecast, backends |
220
+ | [User guide](https://xtft.readthedocs.io/en/latest/user_guide/index.html) | data, the model and its equations, training, missing data, calibration, evaluation |
221
+ | [Examples](https://xtft.readthedocs.io/en/latest/auto_examples/index.html) | runnable scripts with figures, as Python files or notebooks |
222
+ | [API reference](https://xtft.readthedocs.io/en/latest/api/index.html) | every public class and function |
223
+
224
+ ## 📊 Benchmarks
225
+
226
+ [`benchmarks/`](benchmarks/README.md) compares XTFT with TFT,
227
+ PatchTST, NBEATSx, DLinear, LSTM, LightGBM, LEAR and statistical
228
+ baselines under one protocol (same inputs, horizons, origins and
229
+ training budget; several seeds; shared metrics and calibration), on
230
+ electricity prices, retail sales, long-horizon series and CAMELS-US
231
+ streamflow.
232
+
233
+ ```bash
234
+ pip install -e ".[torch,bench]"
235
+ python benchmarks/run.py --dataset EPF-DE --model xtft --horizon 24
236
+ ```
237
+
238
+ ## 🤝 Contributing
239
+
240
+ Issues and pull requests are welcome. See
241
+ [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup, the
242
+ checks and the release process; work on the `dev` branch.
243
+
244
+ ## 📝 Citing
245
+
246
+ A paper describing XTFT is in preparation; until it is published,
247
+ please cite this repository.
248
+
249
+ ## ⚖️ Licence
250
+
251
+ BSD-3-Clause, see [LICENSE](LICENSE).