ad-inventory-forecast
A production-grade Python library for predicting ad inventory (impressions) using ensemble forecasting with automatic model selection.
Installation
pip install .
# or editable install:
pip install -e .
Dependencies: numpy, pandas, scipy, statsmodels, scikit-learn, fastdtw
Optional: prophet, xgboost, pmdarima, lightgbm
Quick Start
import pandas as pd
from ad_inventory_forecast import AutoForecaster
# Load your data (must have DatetimeIndex)
df = pd.read_csv('impressions.csv', parse_dates=['date'], index_col='date')
# Fit and predict
model = AutoForecaster()
model.fit(df['impressions'])
forecast = model.predict(horizon=30)
print(forecast)
# predicted_impressions lower_bound upper_bound
# 2025-01-01 125000.0 98000.0 152000.0
# 2025-01-02 127500.0 99000.0 156000.0
# ...
Models
AutoForecaster
Automatically analyzes data and selects the best model. Recommended for most use cases.
from ad_inventory_forecast import AutoForecaster
model = AutoForecaster(
strategy='fast', # 'fast', 'balanced', or 'thorough'
horizon=30, # Expected forecast horizon
verbose=True # Print model selection details
)
model.fit(y)
forecast = model.predict(horizon=30)
# See what was selected
print(model.advice_.selected_model)
print(model.advice_.selection_reasoning)
InventoryForecaster
Growth-Seasonality Decomposition model. Separates trend, seasonality, and residuals.
from ad_inventory_forecast import InventoryForecaster
model = InventoryForecaster(
model='auto', # 'additive', 'multiplicative', or 'auto'
trend='auto', # 'linear', 'exponential', 'damped', or 'auto'
seasonal_periods=[7], # Weekly seasonality for daily data
trend_dampening=True, # Dampen trend extrapolation (recommended)
dampen_factor=0.98, # Dampening rate (0.9-0.99)
robust=True # Robust to outliers
)
model.fit(y)
forecast = model.predict(horizon=30)
ETSForecaster
Exponential Smoothing (Holt-Winters) with automatic component selection.
from ad_inventory_forecast.models import ETSForecaster
model = ETSForecaster(
trend='auto', # 'add', 'mul', or None
damped_trend=True, # Dampen trend (recommended)
seasonal='auto', # 'add', 'mul', or None
seasonal_periods=7 # Seasonal cycle length
)
model.fit(y)
forecast = model.predict(horizon=30)
SARIMAForecaster
Seasonal ARIMA with automatic order selection.
from ad_inventory_forecast.models import SARIMAForecaster
model = SARIMAForecaster(
auto=True, # Auto-select (p,d,q)(P,D,Q,m)
seasonal_periods=7 # Seasonal period
)
model.fit(y)
forecast = model.predict(horizon=30)
AnalogEventForecaster
"Copy model" for event-driven forecasting. Copies historical event profiles and scales to current trend.
from ad_inventory_forecast import AnalogEventForecaster
model = AnalogEventForecaster(
source_window=('2024-02-01', '2024-02-14'), # Historical event
target_window=('2025-02-01', '2025-02-14'), # Future event
use_dtw=True, # Dynamic Time Warping alignment
scale_by_trend=True # Scale by growth trend
)
model.fit(y)
forecast = model.predict()
Model Selection & Tuning
ModelAdvisor
Analyzes data characteristics and recommends models.
from ad_inventory_forecast import ModelAdvisor
advisor = ModelAdvisor(verbose=True)
advice = advisor.analyze(y)
print(advice.data_analysis) # Data characteristics
print(advice.selected_model) # Recommended model
print(advice.selection_reasoning) # Why
for rec in advice.recommendations:
print(f"{rec.model_name}: {rec.suitability}")
AutoTuner
Walk-forward cross-validation for model comparison.
from ad_inventory_forecast import AutoTuner
tuner = AutoTuner(
candidates=[model1, model2, model3],
metric='smape', # 'smape', 'mase', 'mape', 'rmse'
n_folds=3,
horizon=14
)
result = tuner.select_best(y)
best_model = result.best_model
Diagnostics & Robustness
The library includes automatic diagnostics for high-volatility series:
model = AutoForecaster(verbose=True)
model.fit(y)
# Access diagnostics
analysis = model.advice_.data_analysis
print(f"Volatility: {analysis.volatility.volatility_level}") # low/medium/high
print(f"CV: {analysis.volatility.coefficient_of_variation:.1%}")
if analysis.structural_break.detected:
print(f"Warning: {analysis.structural_break.message}")
Volatility-based interval widening:
| CV Range | Level | Interval Multiplier |
|---|---|---|
| < 20% | Low | 1.0x |
| 20-35% | Medium | 1.25x |
| > 35% | High | 1.5x |
Metrics
from ad_inventory_forecast import (
calculate_smape, # Symmetric MAPE (handles zeros)
calculate_mase, # Mean Absolute Scaled Error
calculate_mape, # Mean Absolute Percentage Error
calculate_rmse, # Root Mean Squared Error
calculate_mae # Mean Absolute Error
)
error = calculate_smape(actual, predicted)
Demographic Reconciliation
For hierarchical forecasts with demographic segments:
from ad_inventory_forecast import DemographicReconciler
reconciler = DemographicReconciler(method='top_down')
reconciled = reconciler.reconcile(total_forecast, segment_proportions)
Data Requirements
Input: pandas.Series with DatetimeIndex
Minimum history:
- Daily data: 30+ days (180+ recommended)
- Weekly data: 26+ weeks
- Monthly data: 12+ months
Output: pandas.DataFrame with columns:
predicted_impressions: Point forecastlower_bound: Lower confidence bound (95%)upper_bound: Upper confidence bound (95%)
Package Structure
ad_inventory_forecast/
├── models/
│ ├── decomposition.py # InventoryForecaster
│ ├── ets.py # ETSForecaster
│ ├── sarima.py # SARIMAForecaster
│ ├── analog.py # AnalogEventForecaster
│ ├── auto.py # AutoForecaster
│ └── trend.py # Trend models
├── core/
│ ├── advisor.py # ModelAdvisor
│ ├── diagnostics.py # Volatility & structural break detection
│ ├── validation.py # Data validation
│ └── preprocessing.py # Data transforms
├── backtesting/
│ ├── metrics.py # Error metrics
│ └── validator.py # Walk-forward validation
├── seasonality/
│ ├── extraction.py # STL/MSTL decomposition
│ └── fourier.py # Fourier seasonality
├── tuning/
│ └── auto_tuner.py # Model selection
├── reconciliation/
│ └── demographic.py # Hierarchical reconciliation
└── utils/
├── dtw.py # Dynamic Time Warping
└── synthetic.py # Synthetic data generation
License
MIT
Metadata
Release files for ad-inventory-forecast 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ad_inventory_forecast-0.2.0.tar.gz | 95.4 kB | Without provenance | Details for ad_inventory_forecast-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ad_inventory_forecast-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for ad_inventory_forecast-0.2.0-py3-none-any.whl |
Total release size: 209.2 kB
Release files / ad_inventory_forecast-0.2.0.tar.gz
| Download URL | ad_inventory_forecast-0.2.0.tar.gz |
|---|---|
| Size | 95.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2395ea6894713ccfb4616d7701ea489a54aeb59a2d28a1397e0929e5c29b2999
|
|
BLAKE2b-256 checksum How to use checksums |
bf89833b3478f68b58c689eb7429f4f171a3391c028526194cf5b9c3c963fd1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / ad_inventory_forecast-0.2.0-py3-none-any.whl
| Download URL | ad_inventory_forecast-0.2.0-py3-none-any.whl |
|---|---|
| Size | 113.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
284298432fcfbc38d5fbb1dc78d35b8c9476ac0e20c05260475563511982dae2
|
|
BLAKE2b-256 checksum How to use checksums |
cc254255e695806212e30285d7a8d3f498032e52b50391fd9240d505da0b26d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for ad-inventory-forecast 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.