foundationforecast.core.conformal
Cross-validation settings for distribution-free prediction intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_windows
|
int
|
Number of rolling backtest windows used to estimate residuals. Must be at least 2. |
2
|
method
|
str | ConformalMethod
|
|
'conformal_distribution'
|
step_size
|
int | None
|
Gap between consecutive calibration cutoffs. |
None
|
n_windows
class-attribute
instance-attribute
n_windows: int = 2
step_size
class-attribute
instance-attribute
step_size: int | None = None
__post_init__
__post_init__() -> None
Source code in foundationforecast/core/conformal.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
Bases: ABC
Base class for parameterized conformal calibration methods.
Pass an instance as ConformalIntervals(method=...) (for example
PID or ACI). Instances are called
with the point forecasts and the signed cross-validation errors
y - y_hat of the calibration windows, and return the forecast frame
with -lo-{level} / -hi-{level} columns added.
Calibration errors arrive as cs_df[model] reshaped to
(cs_n_windows, n_series, cs_h); window 0 is the oldest cutoff and
consecutive windows are step_size observations apart.
validate
validate(n_windows: int, step_size: int | None) -> None
Raise ValueError if the method cannot run with these settings.
Source code in foundationforecast/core/conformal.py
29 30 31 | |
__call__
abstractmethod
__call__(
fcst_df: DataFrame,
cs_df: DataFrame,
model_names: list[str],
level: list[int | float],
cs_n_windows: int,
cs_h: int,
n_series: int,
horizon: int,
step_size: int,
) -> DataFrame
Source code in foundationforecast/core/conformal.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Bases: ConformalMethod
Conformal PID control for online interval calibration under shift.
Implements the proportional and integral modules of Angelopoulos, Candes and Tibshirani (2023). Starting from the oldest calibration window the method replays the backtest in time order. Per horizon it tracks the error quantile directly on the score scale:
- P (quantile tracking) moves the threshold up after a miss and down
after a cover, by
lrtimes the recent range of the errors. - I (error integration) feeds the cumulative miscoverage through a
saturating
tanso that persistent coverage bias is corrected.
Errors are delay-aware: the error at horizon h of a window is only used
h observations after its forecast origin, so step_size < h windows
never see the future. The forecast being calibrated sees every window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symmetric
|
bool
|
|
False
|
lr
|
float
|
Base learning rate of quantile tracking, scaled internally by the range of recent errors. |
0.1
|
integrate
|
bool
|
Include the error-integration term. |
True
|
KI
|
float | None
|
Integrator scale. |
None
|
Csat
|
float | None
|
Integrator saturation constant. |
None
|
Tg
|
float | None
|
Target time for the coverage guarantee. |
None
|
delta
|
float
|
Tolerated absolute coverage gap in |
0.01
|
ncal
|
int | None
|
Initial windows used to warm start the tracker, and the trailing
window of the learning-rate range when |
None
|
rolling
|
bool
|
Use only the last |
False
|
init
|
Literal['quantile', 'zero']
|
|
'quantile'
|
Example
from foundationforecast import ConformalIntervals, PID
ConformalIntervals(n_windows=20, method=PID(lr=0.1))
Each level is tracked independently. Thresholds are then floored at zero
(intervals always contain the point forecast) and sorted across levels so
wider levels contain narrower ones. When the integrator saturates (the
realized miss rate is far from the target) the bound becomes infinite and
forecast() emits a UserWarning. The scorecasting (D) module is not
implemented.
symmetric
class-attribute
instance-attribute
symmetric: bool = False
lr
class-attribute
instance-attribute
lr: float = 0.1
integrate
class-attribute
instance-attribute
integrate: bool = True
KI
class-attribute
instance-attribute
KI: float | None = None
Csat
class-attribute
instance-attribute
Csat: float | None = None
Tg
class-attribute
instance-attribute
Tg: float | None = None
delta
class-attribute
instance-attribute
delta: float = 0.01
ncal
class-attribute
instance-attribute
ncal: int | None = None
rolling
class-attribute
instance-attribute
rolling: bool = False
init
class-attribute
instance-attribute
init: Literal['quantile', 'zero'] = 'quantile'
__post_init__
__post_init__() -> None
Source code in foundationforecast/core/conformal_pid.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
resolve_ncal
resolve_ncal(n_windows: int) -> int
Source code in foundationforecast/core/conformal_pid.py
213 214 | |
resolve_csat
resolve_csat(n_windows: int) -> float
Integrator saturation constant, or inf if the integrator is off.
Source code in foundationforecast/core/conformal_pid.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
validate
validate(n_windows: int, step_size: int | None) -> None
Raise ValueError if the method cannot run with these settings.
Source code in foundationforecast/core/conformal_pid.py
231 232 233 234 235 236 237 | |
thresholds
thresholds(
errors: ndarray,
level: list[int | float],
step_size: int,
final_gap: int,
) -> tuple[ndarray, ndarray]
Lower and upper thresholds (n_levels, n_series, h) for errors.
errors are signed y - y_hat with shape
(n_windows, n_series, h), oldest window first. The interval is
[y_hat - lower, y_hat + upper].
Source code in foundationforecast/core/conformal_pid.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
radii
radii(
errors: ndarray,
level: list[int | float],
step_size: int,
final_gap: int,
) -> tuple[ndarray, ndarray]
thresholds floored at zero and nested across level.
level must be sorted ascending. Warns when a bound is infinite.
Source code in foundationforecast/core/conformal_pid.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
__call__
__call__(
fcst_df: DataFrame,
cs_df: DataFrame,
model_names: list[str],
level: list[int | float],
cs_n_windows: int,
cs_h: int,
n_series: int,
horizon: int,
step_size: int,
) -> DataFrame
Source code in foundationforecast/core/conformal_pid.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
Bases: ConformalMethod
Adaptive conformal inference reconstructed from calibration windows.
Replays the Gibbs and Candes (2021) update
alpha_next = alpha + gamma * (target_alpha - miss) separately for each
series, horizon step, and level, starting at target_alpha = 1 - level/100.
Scores are absolute point-forecast errors. An interval uses the inverse
empirical CDF of previously revealed scores, without interpolation or the
finite-sample rank correction of conformal_error_finite_sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gamma
|
float
|
Adaptation rate in |
0.01
|
ncal
|
int | None
|
Initial windows used only to seed the score distribution (no alpha
updates). |
None
|
rolling
|
bool
|
Use only the latest |
False
|
Feedback at horizon step j is used only after j observations have
passed since its forecast origin. Pending scores are checked against the
intervals actually issued during replay, so overlapping windows do not
leak future errors. No state persists across calls or models.
Alpha is not clipped: alpha <= 0 produces an unbounded interval and
alpha >= 1 an empty interval. Empty intervals are represented by lower
+inf and upper -inf. Output radii are widened across sorted levels
to make intervals nested. Non-finite output bounds emit a UserWarning.
Resetting replay on each call, delayed feedback, and output nesting mean this API does not inherit the continuous online algorithm's long-run coverage guarantee. Measure coverage and width on held-out backtests.
Example
from foundationforecast import ACI, ConformalIntervals
ConformalIntervals(n_windows=20, method=ACI(gamma=0.005))
gamma
class-attribute
instance-attribute
gamma: float = 0.01
ncal
class-attribute
instance-attribute
ncal: int | None = None
rolling
class-attribute
instance-attribute
rolling: bool = False
__post_init__
__post_init__() -> None
Source code in foundationforecast/core/conformal_aci.py
108 109 110 111 112 113 114 115 116 117 118 119 120 | |
resolve_ncal
resolve_ncal(n_windows: int) -> int
Source code in foundationforecast/core/conformal_aci.py
122 123 | |
validate
validate(n_windows: int, step_size: int | None) -> None
Raise ValueError if the method cannot run with these settings.
Source code in foundationforecast/core/conformal_aci.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
thresholds
thresholds(
errors: ndarray,
level: Sequence[int | float],
step_size: int,
final_gap: int,
) -> ndarray
Return raw symmetric radii of shape (n_levels, n_series, h).
Errors have shape (n_windows, n_series, h) and are oldest first.
final_gap is the distance from the last calibration origin to the
forecast origin; forecast() passes its calibration horizon here.
This mathematical helper does not nest radii or warn about edge sets.
Source code in foundationforecast/core/conformal_aci.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
__call__
__call__(
fcst_df: DataFrame,
cs_df: DataFrame,
model_names: list[str],
level: list[int | float],
cs_n_windows: int,
cs_h: int,
n_series: int,
horizon: int,
step_size: int,
) -> DataFrame
Source code in foundationforecast/core/conformal_aci.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |