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, prophet_then_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
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 394 395 396 397 398 399 400 401 402 403 404 | |
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_days=90, min_train_days=365, skip_days=0, cv_end_date=None, split_method='days', test_pct=0.1, min_train_pct=0.5, skip_pct=0.0, 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.
Folds are non-overlapping by construction. Consecutive validation windows
are separated by skip_days (matching the R reference implementation's
skip parameter). The CV window can be capped at cv_end_date to prevent
tuning or evaluation folds from touching the held-out test period defined
by run_single_its.
Fold layout (train = expanding, test = fixed width):
|------ min_train_days ------|-- test_days --|-- skip_days --|-- test_days --|...
fold 1: train [0, T0), test [T0, T0+test_days)
fold 2: train [0, T0+test_days+skip_days), test [T0+test_days+skip_days, ...)
...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Full time series dataset. |
required |
intervention_date
|
str or Timestamp
|
CV uses only pre-intervention data (or data before cv_end_date if set). |
required |
model_name
|
str
|
Model to evaluate. |
'arima'
|
n_folds
|
int
|
Maximum number of CV folds to attempt. |
5
|
test_days
|
int
|
Length of each validation window in days. |
90
|
min_train_days
|
int
|
Minimum training window for the first fold. |
365
|
skip_days
|
int
|
Gap in days 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 days for daily data). |
0
|
cv_end_date
|
str or Timestamp
|
Upper bound on the data used for CV. Must be <= intervention_date. Use intervention_date - test_days to keep CV folds out of the held-out evaluation window used by run_single_its. Defaults to intervention_date (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
56 57 58 59 60 61 62 63 64 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 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 | |
tune_model(df: pd.DataFrame, intervention_date, model_name: str, n_trials: int | None = None, n_folds: int = 5, test_days: int = 365, min_train_days: int = 730, skip_days: int = 0, cv_end_date=None, split_method: str = 'percent', test_pct: float = 0.1, min_train_pct: float = 0.5, skip_pct: float = 0.0, metric: str = 'rmse', config_path=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.
R reference CV settings: 5 folds, 12-month validation window, 2-year initial training window, 12-month skip between folds. Matching those settings: n_folds=5, test_days=365, min_train_days=730, skip_days=365
To prevent tuning from seeing the held-out evaluation window that run_single_its uses, set cv_end_date to intervention_date minus test_days.
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", "prophet_then_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
|
test_days
|
int
|
Validation window per fold in days. |
365
|
min_train_days
|
int
|
Minimum training window for the first fold in days. |
730
|
skip_days
|
int
|
Gap in days between consecutive fold validation windows. Set to 365 to match the R reference (skip = "12 months"). Defaults to 0 (adjacent folds). |
0
|
cv_end_date
|
str or Timestamp
|
Upper bound on data used for CV folds. Must be <= intervention_date. Pass intervention_date - pd.Timedelta(days=test_days) to prevent tuning folds from overlapping with the held-out evaluation window. Defaults to None (use 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
|
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 (R-matched CV settings, leakage-free):
import pandas as pd
result = tune_model(
df, "2025-01-07", "prophet_xgb",
n_trials=100, n_folds=5,
test_days=365, min_train_days=730, skip_days=365,
cv_end_date=pd.Timestamp("2025-01-07") - pd.Timedelta(days=365),
)
run_single_its(
df, "2025-01-07",
model_name="prophet_xgb",
config_overrides={"models": {"prophet_xgb": result.best_params}},
)
Source code in its2s/tuning.py
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 | |
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)
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. |
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
|
|