timecopilot.models.ensembles
MedianEnsemble
MedianEnsemble(
models: list[Forecaster], alias: str = "MedianEnsemble"
)
Bases: Forecaster
Initialize a MedianEnsemble forecaster.
This ensemble combines the forecasts of multiple models by taking the median of their predictions for each time step and series. For probabilistic forecasts (quantiles and levels), it uses isotonic regression to ensure monotonicity of the quantile outputs across the ensemble. Optionally, you can set a custom alias for the ensemble.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models
|
list[Forecaster]
|
List of instantiated forecaster models to be ensembled. Each model must implement the forecast method and have a unique alias. |
required |
alias
|
str
|
Name to use for the ensemble in output DataFrames and logs. Defaults to "MedianEnsemble". |
'MedianEnsemble'
|
Example
import pandas as pd
from timecopilot.models.benchmarks import SeasonalNaive
from timecopilot.models.ensembles.median import MedianEnsemble
from timecopilot.models.foundational.chronos import Chronos
df = pd.read_csv(
"https://timecopilot.s3.amazonaws.com/public/data/air_passengers.csv",
parse_dates=["ds"],
)
models = [
Chronos(
repo_id="amazon/chronos-t5-tiny",
alias="Chronos-T5",
),
Chronos(
repo_id="amazon/chronos-bolt-tiny",
alias="Chronos-Bolt",
),
SeasonalNaive(),
]
median_ensemble = MedianEnsemble(models=models)
fcst_df = median_ensemble.forecast(
df=df,
h=12,
)
print(fcst_df)
Source code in timecopilot/models/ensembles/median.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
forecast
forecast(
df: DataFrame,
h: int,
freq: str | None = None,
level: list[int | float] | None = None,
quantiles: list[float] | None = None,
) -> DataFrame
Generate forecasts for time series data using the model.
This method produces point forecasts and, optionally, prediction intervals or quantile forecasts. The input DataFrame can contain one or multiple time series in stacked (long) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing the time series to forecast. It must include as columns:
|
required |
h
|
int
|
Forecast horizon specifying how many future steps to predict. |
required |
freq
|
str
|
Frequency of the time series (e.g. "D" for daily, "M" for monthly). See Pandas frequency aliases for valid values. If not provided, the frequency will be inferred from the data. |
None
|
level
|
list[int | float]
|
Confidence levels for prediction intervals, expressed as percentages (e.g. [80, 95]). If provided, the returned DataFrame will include lower and upper interval columns for each specified level. |
None
|
quantiles
|
list[float]
|
List of quantiles to forecast, expressed as floats between 0
and 1. Should not be used simultaneously with |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing forecast results. Includes:
For multi-series data, the output retains the same unique identifiers as the input DataFrame. |
Source code in timecopilot/models/ensembles/median.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
cross_validation
cross_validation(
df: DataFrame,
h: int,
freq: str | None = None,
n_windows: int = 1,
step_size: int | None = None,
level: list[int | float] | None = None,
quantiles: list[float] | None = None,
) -> DataFrame
Perform cross-validation on time series data.
This method splits the time series into multiple training and testing windows and generates forecasts for each window. It enables evaluating forecast accuracy over different historical periods. Supports point forecasts and, optionally, prediction intervals or quantile forecasts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing the time series to forecast. It must include as columns:
|
required |
h
|
int
|
Forecast horizon specifying how many future steps to predict in each window. |
required |
freq
|
str
|
Frequency of the time series (e.g. "D" for daily, "M" for monthly). See Pandas frequency aliases for valid values. If not provided, the frequency will be inferred from the data. |
None
|
n_windows
|
int
|
Number of cross-validation windows to generate. Defaults to 1. |
1
|
step_size
|
int
|
Step size between the start of consecutive windows. If None, it
defaults to |
None
|
level
|
list[int | float]
|
Confidence levels for prediction intervals, expressed as percentages (e.g. [80, 95]). When specified, the output DataFrame includes lower and upper interval columns for each level. |
None
|
quantiles
|
list[float]
|
Quantiles to forecast, expressed as floats between 0 and 1.
Should not be used simultaneously with |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the forecasts for each cross-validation window. The output includes:
|
Source code in timecopilot/models/utils/forecaster.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 233 234 235 236 237 238 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 278 279 280 281 282 |
|
plot
staticmethod
plot(
df: DataFrame | None = None,
forecasts_df: DataFrame | None = None,
ids: list[str] | None = None,
plot_random: bool = True,
max_ids: int | None = 8,
models: list[str] | None = None,
level: list[float] | None = None,
max_insample_length: int | None = None,
plot_anomalies: bool = False,
engine: str = "matplotlib",
palette: str | None = None,
seed: int | None = None,
resampler_kwargs: dict | None = None,
ax: Axes | ndarray | Figure | None = None,
)
Plot forecasts and insample values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with columns
[ |
None
|
forecasts_df
|
DataFrame
|
DataFrame with
columns [ |
None
|
ids
|
list[str]
|
Time Series to plot. If None, time series are selected randomly. Defaults to None. |
None
|
plot_random
|
bool
|
Select time series to plot randomly. Defaults to True. |
True
|
max_ids
|
int
|
Maximum number of ids to plot. Defaults to 8. |
8
|
models
|
list[str]
|
Models to plot. Defaults to None. |
None
|
level
|
list[float]
|
Prediction intervals to plot. Defaults to None. |
None
|
max_insample_length
|
int
|
Maximum number of train/insample observations to be plotted. Defaults to None. |
None
|
plot_anomalies
|
bool
|
Plot anomalies for each prediction interval. Defaults to False. |
False
|
engine
|
str
|
Library used to plot. 'plotly', 'plotly-resampler' or 'matplotlib'. Defaults to 'matplotlib'. |
'matplotlib'
|
palette
|
str
|
Name of the matplotlib colormap to use for the plots. If None, uses the current style. Defaults to None. |
None
|
seed
|
int
|
Seed used for the random number generator. Only used if plot_random is True. Defaults to 0. |
None
|
resampler_kwargs
|
dict
|
Keyword arguments to be passed to
plotly-resampler constructor. For further custumization ("show_dash")
call the method, store the plotting object and add the extra arguments
to its |
None
|
ax
|
matplotlib axes, array of matplotlib axes or plotly Figure
|
Object where plots will be added. Defaults to None. |
None
|
Source code in timecopilot/models/utils/forecaster.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 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 |
|