Process test samples one by one, updating alpha after each observation.
Code
# Store predictions before update for proper evaluationpredictions_before_update = []true_labels = []for i inrange(len(X_test)):# Get prediction using current alpha pred_sets = aci.predict(X_test[i : i +1]) predictions_before_update.append(pred_sets[0]) true_labels.append(y_test[i])# Update with true label (this would happen after observing ground truth) aci.update(X_test[i : i +1], y_test[i : i +1])print(f"\nAfter {aci.n_updates_} updates:")print(f"Final alpha: {aci.alpha_:.4f}")print(f"Final coverage: {aci.current_coverage:.1%}")
After 300 updates:
Final alpha: 0.0010
Final coverage: 99.3%
for i inrange(len(X_test_r)):# Get interval intervals = acr.predict_interval(X_test_r[i : i +1])# Update with true value acr.update(X_test_r[i : i +1], y_test_r[i : i +1])print(f"\nAfter {acr.n_updates_} updates:")print(f"Final alpha: {acr.alpha_:.4f}")print(f"Final coverage: {acr.current_coverage:.1%}")
After 300 updates:
Final alpha: 0.5000
Final coverage: 75.3%
The learning rate gamma controls how quickly alpha adapts. Larger gamma = faster adaptation but more oscillation. Smaller gamma = smoother adaptation but slower response to shift.
Code
gammas = [0.005, 0.02, 0.05, 0.1]colors = ["#3498db", "#2ecc71", "#e74c3c", "#9b59b6"]fig, axes = plt.subplots(1, 2, figsize=(12, 5))for gamma, color inzip(gammas, colors):# Reset and run with this gamma aci_temp = AdaptiveConformalClassifier( model=model, nonconformity_function=lac_nonconformity, target_coverage=0.9, gamma=gamma, ) aci_temp.calibrate(X_calib, y_calib)for i inrange(len(X_test)): aci_temp.update(X_test[i : i +1], y_test[i : i +1])# Plot alpha axes[0].plot(aci_temp.alpha_history_, color=color, linewidth=1.5, label=f"gamma={gamma}", alpha=0.8)# Plot coverage axes[1].plot(aci_temp.coverage_history_, color=color, linewidth=1.5, label=f"gamma={gamma}", alpha=0.8)axes[0].axhline(0.1, color="black", linestyle="--", alpha=0.5)axes[0].set_xlabel("Time step")axes[0].set_ylabel("Alpha")axes[0].set_title("Alpha Adaptation: Effect of Gamma")axes[0].legend()axes[0].grid(True, alpha=0.3)axes[1].axhline(0.9, color="black", linestyle="--", alpha=0.5)axes[1].set_xlabel("Time step")axes[1].set_ylabel("Coverage")axes[1].set_title("Coverage: Effect of Gamma")axes[1].legend()axes[1].grid(True, alpha=0.3)plt.tight_layout()plt.show()
When 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
Choosing gamma:
Start with gamma=0.01-0.02 for most applications
Use larger gamma (0.05-0.1) for rapid distribution shift
Use smaller gamma (<0.01) for stable distributions