API Reference¶
Public entrypoints for the package. Each section below is generated from the live docstrings in the source code.
Pipeline¶
run_single_its(df, intervention_date, target_col=None, date_col=None, covariate_cols=None, model_name='prophet_xgb', config_path=None, config_overrides=None, output_dir=None, seed=42, split_method=None)
¶
Run a single ITS counterfactual analysis pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Full time series dataset. |
required |
intervention_date
|
str or Timestamp
|
Date of the intervention. |
required |
target_col
|
str
|
Target column name. Defaults to config value. |
None
|
date_col
|
str
|
Date column name. Defaults to config value. |
None
|
covariate_cols
|
list[str]
|
Covariate column names. Defaults to config value. |
None
|
model_name
|
str
|
Model to use. One of: prophet_xgb, neuralprophet, arima. |
'prophet_xgb'
|
config_path
|
str or Path
|
Path to custom YAML config. |
None
|
config_overrides
|
dict
|
Runtime config overrides. |
None
|
output_dir
|
str or Path
|
Directory for saving outputs. If None, no files are saved. |
None
|
seed
|
int
|
Random seed for bootstrap reproducibility. |
42
|
Returns:
| Type | Description |
|---|---|
PipelineResult
|
|
Source code in its2s/pipeline.py
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 283 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 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 | |
run_batch(series_list, config_path=None, output_dir='output', n_jobs=1, seed=42)
¶
Run ITS pipeline on multiple series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series_list
|
list[dict]
|
Each dict has: series_id, df, intervention_date, target_col, date_col, covariate_cols, model_name (optional), config_overrides (optional). |
required |
config_path
|
str or Path
|
Path to shared YAML config. |
None
|
output_dir
|
str or Path
|
Base output directory. |
'output'
|
n_jobs
|
int
|
Number of parallel jobs. 1 = sequential. |
1
|
seed
|
int
|
Global seed for reproducibility. |
42
|
Returns:
| Type | Description |
|---|---|
list[PipelineResult]
|
|
Source code in its2s/batch/runner.py
Cross-validation and tuning¶
time_series_cv(df, intervention_date, model_name='arima', n_folds=5, test_obs=None, min_train_obs=None, skip_obs=None, cv_end_date=None, split_method='observations', test_pct=None, min_train_pct=None, skip_pct=None, date_col=None, target_col=None, covariate_cols=None, config_path=None, config_overrides=None)
¶
Evaluate a model using expanding-window time-series cross-validation.
All CV windows are sized in OBSERVATIONS (rows of the regular series),
never calendar days: fold boundaries are positional slices, so on a weekly
series test_obs=52 spans one year. Calendar-day windows exist only in
prepare_splits. Folds are non-overlapping by construction. Consecutive
validation windows are separated by skip_obs (matching the R reference
implementation's skip parameter). The CV frame is capped at
cv_end_date, which by default is derived from the run's held-out test
split so tuning and evaluation folds never touch the window
run_single_its evaluates on (GH #40).
Only the window arguments belonging to the chosen split_method may be
passed; arguments for the other method raise ValueError rather than being
silently ignored.
Fold layout (train = expanding, test = fixed width, all in observations):
|------ min_train_obs ------|-- test_obs --|-- skip_obs --|-- test_obs --|...
fold 1: train [0, T0), test [T0, T0+test_obs)
fold 2: train [0, T0+test_obs+skip_obs), test [T0+test_obs+skip_obs, ...)
...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Full time series dataset. |
required |
intervention_date
|
str or Timestamp
|
CV uses only data before cv_end_date, which is at most the intervention date. |
required |
model_name
|
str
|
Model to evaluate. |
'arima'
|
n_folds
|
int
|
Maximum number of CV folds to attempt. |
5
|
split_method
|
(observations, percent)
|
"observations" (default): size the fold windows as explicit
observation counts via |
"observations"
|
test_obs
|
int
|
Length of each validation window in observations. Defaults to 90.
Only with |
None
|
min_train_obs
|
int
|
Minimum training window for the first fold, in observations.
Defaults to 365. Only with |
None
|
skip_obs
|
int
|
Gap in observations between the end of one validation window and the
start of the next. Set to 0 for adjacent non-overlapping folds. The R
reference uses skip = "12 months" (365 observations for daily data).
Defaults to 0. Only with |
None
|
test_pct
|
float
|
Validation window per fold as a fraction of the CV observations.
Defaults to 0.10. Only with |
None
|
min_train_pct
|
float
|
Minimum training window as a fraction of the CV observations.
Defaults to 0.50. Only with |
None
|
skip_pct
|
float
|
Gap between folds as a fraction of the CV observations. Defaults to
0.0. Only with |
None
|
cv_end_date
|
str or Timestamp
|
Upper bound on the data used for CV. Must be <= intervention_date. Defaults to the first date of the held-out test window that prepare_splits produces for this df and the loaded config's "periods" section, so CV folds never touch the window run_single_its evaluates on (GH #40). The derivation is row-exact for every split method and series frequency; evaluate on the same missing-handled DataFrame and periods config you will run with. Pass cv_end_date=intervention_date explicitly to use all pre-intervention data. |
None
|
date_col
|
str
|
Date column name. Defaults to config value. |
None
|
target_col
|
str
|
Target column name. Defaults to config value. |
None
|
covariate_cols
|
list[str]
|
Covariate column names. |
None
|
config_path
|
str or Path
|
|
None
|
config_overrides
|
dict
|
|
None
|
Returns:
| Type | Description |
|---|---|
CVResult
|
|
Source code in its2s/cross_validation.py
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 161 162 163 164 165 166 167 168 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 283 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 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 | |
tune_model(df: pd.DataFrame, intervention_date, model_name: str, n_trials: int | None = None, n_folds: int = 5, test_obs: int | None = None, min_train_obs: int | None = None, skip_obs: int | None = None, cv_end_date=None, split_method: str = 'percent', test_pct: float | None = None, min_train_pct: float | None = None, skip_pct: float | None = None, metric: str = 'rmse', config_path=None, config_overrides: dict | None = None, n_jobs: int = 1, seed: int = 42) -> TuningResult
¶
Tune model hyperparameters via Latin hypercube grid search with time-series CV.
Mirrors the R reference implementation (Two_Stage_ITS): a one-shot space-filling sample of the parameter space is evaluated via expanding-window CV, and the combination with the lowest mean CV RMSE (or MAE) is selected.
All CV windows are sized in OBSERVATIONS (rows), never calendar days -- see time_series_cv. The R reference CV settings are 5 folds, 12-month validation window, 2-year initial training window, 12-month skip between folds; on DAILY data those translate to: split_method="observations", n_folds=5, test_obs=365, min_train_obs=730, skip_obs=365 (on any other frequency, convert months to observation counts first).
Only the window arguments belonging to the chosen split_method may be
passed; arguments for the other method raise ValueError rather than being
silently ignored.
By default, tuning folds are capped at the start of the held-out test window that run_single_its will evaluate on, derived row-exactly from the config's "periods" section (GH #40). Pass the same config_path / config_overrides you will run with so the derived boundary matches the run; pass cv_end_date=intervention_date to deliberately tune on all pre-intervention data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Full time series dataset. |
required |
intervention_date
|
str or Timestamp
|
Only pre-intervention data is used for tuning CV. |
required |
model_name
|
str
|
One of "arima", "neuralprophet", "prophet_xgb". |
required |
n_trials
|
int or None
|
Number of parameter combinations to evaluate. Defaults to model-specific values matching R reference (100 for most models, 75 for neuralprophet). |
None
|
n_folds
|
int
|
Number of expanding-window CV folds. |
5
|
split_method
|
(percent, observations)
|
"percent" (default): size the fold windows as fractions of the CV
observations via |
"percent"
|
test_obs
|
int
|
Validation window per fold in observations. Defaults to 365.
Only with |
None
|
min_train_obs
|
int
|
Minimum training window for the first fold, in observations.
Defaults to 730. Only with |
None
|
skip_obs
|
int
|
Gap in observations between consecutive fold validation windows. Set
to 365 on daily data to match the R reference (skip = "12 months").
Defaults to 0 (adjacent folds). Only with
|
None
|
test_pct
|
float
|
Validation window per fold as a fraction of the CV observations.
Defaults to 0.10. Only with |
None
|
min_train_pct
|
float
|
Minimum training window as a fraction of the CV observations.
Defaults to 0.50. Only with |
None
|
skip_pct
|
float
|
Gap between folds as a fraction of the CV observations. Defaults to
0.0. Only with |
None
|
cv_end_date
|
str or Timestamp
|
Upper bound on data used for CV folds. Must be <= intervention_date. Defaults to the first date of the held-out test window that prepare_splits produces for this df and the loaded config's "periods" section, so tuning folds never touch the window run_single_its evaluates on (GH #40). The derivation is row-exact for every split method and series frequency; tune on the same missing-handled DataFrame and periods config you will run with. Pass cv_end_date=intervention_date explicitly to tune on all pre-intervention data. |
None
|
metric
|
str
|
Objective for selecting the best parameter set. "rmse" or "mae". |
'rmse'
|
config_path
|
str or Path
|
Path to a custom base YAML config (merged before tuning overrides). |
None
|
config_overrides
|
dict
|
Runtime config overrides, as in run_single_its. Pass the same overrides here that the run will use -- in particular any "periods" override -- so the derived cv_end_date matches the run's actual test window. Per-trial model params are merged on top and always win over model overrides given here. |
None
|
n_jobs
|
int
|
Parallel workers for evaluating trials. -1 uses all available cores. |
1
|
seed
|
int
|
Random seed for the Latin hypercube sampler. |
42
|
Returns:
| Type | Description |
|---|---|
TuningResult
|
Contains best_params (inject via run_single_its config_overrides), trials_df (all evaluated combinations and their CV metrics), and summary statistics. |
Examples:
Tune and apply best params on a DAILY series (R-matched CV settings; 365 observations = 365 calendar days only because the series is daily). Tuning folds stop before the run's held-out test window by default; passing the same periods override to both calls keeps the derived boundary and the run's actual window identical:
overrides = {"periods": {"split_method": "days",
"test_days": 365, "holdout_days": 365}}
result = tune_model(
df, "2025-01-07", "prophet_xgb",
n_trials=100, n_folds=5,
split_method="observations",
test_obs=365, min_train_obs=730, skip_obs=365,
config_overrides=overrides,
)
run_single_its(
df, "2025-01-07",
model_name="prophet_xgb",
config_overrides={"models": {"prophet_xgb": result.best_params},
**overrides},
)
Source code in its2s/tuning.py
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 283 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 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 | |
TuningResult(model_name: str, best_params: dict, best_rmse: float, best_std_rmse: float, trials_df: pd.DataFrame, n_trials: int, n_folds: int, metric: str, seed: int, cv_end_date: pd.Timestamp | None = None)
dataclass
¶
Result from a hyperparameter tuning run.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the tuned model (e.g. |
best_params |
dict
|
Nested param dict ready to pass as |
best_rmse |
float
|
Mean CV RMSE of the best parameter combination. |
best_std_rmse |
float
|
Std dev of CV RMSE across folds for the best combination. |
trials_df |
DataFrame
|
One row per trial. Columns: |
n_trials |
int
|
Number of parameter combinations evaluated. |
n_folds |
int
|
Number of expanding-window CV folds used per trial. |
metric |
str
|
Objective used for selection ( |
seed |
int
|
Random seed driving the Latin hypercube sample. |
cv_end_date |
Timestamp or None
|
Effective CV cap used for every trial; derived from the run's held-out test split when not passed explicitly (GH #40). |
Comparison¶
compare_models(df, intervention_date, model_names=None, target_col=None, date_col=None, covariate_cols=None, config_path=None, config_overrides=None, output_dir=None, seed=42)
¶
Run the ITS pipeline with multiple models and return a comparison table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Full time series dataset. |
required |
intervention_date
|
str or Timestamp
|
Date of the intervention. |
required |
model_names
|
list[str]
|
Models to compare. Defaults to all available models. |
None
|
target_col
|
str
|
|
None
|
date_col
|
str
|
|
None
|
covariate_cols
|
list[str]
|
|
None
|
config_path
|
str or Path
|
|
None
|
config_overrides
|
dict
|
|
None
|
output_dir
|
str or Path
|
|
None
|
seed
|
int
|
|
42
|
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, dict[str, PipelineResult]]
|
(comparison_table, results_dict) |
Source code in its2s/compare.py
Configuration¶
load_config(path=None, overrides=None)
¶
Load YAML config, merge with defaults, apply overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to a custom YAML config. Merged on top of defaults. |
None
|
overrides
|
dict
|
Runtime overrides applied last. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|