Time Series and Online CP
Standard conformal prediction assumes exchangeable data, but many real-world scenarios involve non-exchangeable data such as:
- Time series forecasting
- Streaming data with distribution shift
- Online learning settings
Adaptive Conformal Inference (ACI) addresses these scenarios by dynamically adjusting the significance level (alpha) based on observed coverage.
The ACI Update Rule
ACI maintains target coverage by adjusting alpha after each observation:
\[\alpha_{t+1} = \alpha_t + \gamma \cdot (e_t - \alpha_{target})\]
where:
- \(e_t = 1\) if the true value was NOT covered, \(0\) otherwise
- \(\gamma\) is the learning rate (controls adaptation speed)
- \(\alpha_{target} = 1 - \text{target\_coverage}\)
This rule increases alpha when coverage is too low (making sets larger) and decreases it when coverage exceeds the target.
Classification with ACI
from sklearn.ensemble import RandomForestClassifier
from conformalpy.timeseries import AdaptiveConformalClassifier
from conformalpy.nonconformity.classification import lac_nonconformity
# Train base model
model = RandomForestClassifier().fit(X_train, y_train)
# Create ACI classifier
aci = AdaptiveConformalClassifier(
model=model,
nonconformity_function=lac_nonconformity,
target_coverage=0.9, # Desired coverage
gamma=0.02, # Learning rate
)
# Calibrate on initial data
aci.calibrate(X_calib, y_calib)
# Online prediction and update loop
for X_new, y_true in streaming_data:
# Get prediction sets using current alpha
pred_sets = aci.predict(X_new)
# After observing true label, update alpha
aci.update(X_new, y_true)
# Check final coverage
print(f"Final coverage: {aci.current_coverage:.1%}")Regression with ACI
from sklearn.ensemble import RandomForestRegressor
from conformalpy.timeseries import AdaptiveConformalRegressor
# Train base model
model = RandomForestRegressor().fit(X_train, y_train)
# Create ACI regressor
acr = AdaptiveConformalRegressor(
model=model,
target_coverage=0.9,
gamma=0.02,
)
# Calibrate on initial data
acr.calibrate(X_calib, y_calib)
# Online prediction and update loop
for X_new, y_true in streaming_data:
# Get prediction intervals
intervals = acr.predict_interval(X_new)
# After observing true value, update alpha
acr.update(X_new, y_true)Choosing the Learning Rate (gamma)
The learning rate \(\gamma\) controls how quickly ACI adapts:
| Gamma | Adaptation | Use Case |
|---|---|---|
| 0.001-0.01 | Slow, stable | Gradual distribution shift |
| 0.01-0.05 | Moderate | Typical time series (recommended) |
| 0.05-0.1 | Fast, may oscillate | Rapid distribution shift |
Start with gamma=0.01 and increase if coverage doesn’t converge.
Alpha Bounds
ACI clips alpha to prevent extreme values:
aci = AdaptiveConformalClassifier(
model=model,
nonconformity_function=lac_nonconformity,
target_coverage=0.9,
gamma=0.02,
alpha_min=0.001, # Maximum coverage (prevents alpha -> 0)
alpha_max=0.5, # Minimum coverage (prevents tiny sets)
)Tracking History
ACI tracks history for visualization and analysis:
# After running online updates
print(f"Updates performed: {aci.n_updates_}")
print(f"Current alpha: {aci.alpha_:.4f}")
print(f"Current coverage: {aci.current_coverage:.1%}")
# Access full history
alpha_history = aci.alpha_history_ # List of alpha values
coverage_history = aci.coverage_history_ # Cumulative coverage over time
error_history = aci.error_history_ # 1 if not covered, 0 if covered
# Visualize adaptation
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].plot(alpha_history)
axes[0].axhline(0.1, color='red', linestyle='--', label='Target alpha')
axes[0].set_xlabel('Time step')
axes[0].set_ylabel('Alpha')
axes[1].plot(coverage_history)
axes[1].axhline(0.9, color='red', linestyle='--', label='Target coverage')
axes[1].set_xlabel('Time step')
axes[1].set_ylabel('Cumulative coverage')
plt.show()Runtime Alpha Override
Like other conformal predictors, ACI supports runtime alpha:
# Use current adaptive alpha
pred_sets = aci.predict(X_test)
# Override with specific alpha
pred_sets = aci.predict(X_test, alpha=0.05) # Stricter thresholdReset State
Reset the adaptive state while keeping calibration:
# After some updates
print(f"Updates: {aci.n_updates_}")
# Reset to initial state
aci.reset()
print(f"After reset: {aci.n_updates_}") # 0When to Use ACI
Use ACI when:
- Data is non-exchangeable (time series, streaming)
- Distribution shift is expected over time
- You need online/adaptive prediction
- Coverage guarantee is important despite non-exchangeability
Use standard conformal (split/CV+) when:
- Data is exchangeable (i.i.d.)
- No distribution shift expected
- Batch prediction is sufficient
- Computational efficiency is critical
Coverage Guarantee
ACI provides an asymptotic coverage guarantee:
\[\lim_{T \to \infty} \frac{1}{T} \sum_{t=1}^{T} \mathbf{1}\{Y_t \in C_t(X_t)\} \geq 1 - \alpha\]
This guarantee holds even under distribution shift, as long as the shift is not too rapid relative to the learning rate \(\gamma\).
Reference:
Gibbs, I., & Candes, E. (2021). “Adaptive Conformal Inference Under Distribution Shift.” Advances in Neural Information Processing Systems (NeurIPS).
See the adaptive conformal example for a complete ACI workflow with visualization.