The user wants a plan for a contextual bandit offline evaluation harness.
Key components required:
Contextual Bandit Context: Logging policy, data collection, target policy.
Offline Evaluation: The core goal is evaluating a new policy using historical data.
Doubly Robust (DR) Estimator: This is the specific statistical method requested.
Guardrails: Mechanisms to ensure the validity, stability, and safety of the evaluation (e.g., handling high variance, importance weight clipping, safety checks).
Structure of the Plan:
Objective & Overview: Briefly state what we are building.
Data Structure: Define what the logged data looks like.
Refinement on "Cross-Fitting":
Using the same data to train the reward model and evaluate the policy introduces bias. I should recommend a 2-fold or K-fold cross-fitting approach (sometimes called the "Dudoit" approach in causal inference).
Split data D into D1,D2.
Train on , evaluate on .
Refinement on Guardrails:
What if plog≈0? Need a minimum probability threshold in the logging policy (exploration).
What if the Reward Model is terrible? Look at RMSE on a validation set.
Structuring the Output:
I will organize this as a technical design document or a robust engineering plan.
System Architecture: High level.
Mathematical Foundation: The DR formula.
Implementation Steps: Code/Pseudocode.
Guardrails & Metrics: The safety checks.
Reporting: How to present results.
Let's write out the code skeleton. Python with Numpy/Pandas is the standard.
Need to define:
logging_policy: The policy that generated the data.
target_policy: The policy we want to test.
reward_model: ML model to predict r given x,a.
Self-Correction during drafting:
Initial thought: Just use IPS.
Correction: User specifically asked for Doubly Robust. Must emphasize the hybrid nature (Model-based + IPS).
Initial thought: Use all data to train reward model.
Correction: Must mention cross-fitting to avoid bias.
Key Metrics for "Guardrails":
ESS (Effective Sample Size).
Weight Kurtosis/Max Value.
Distributional Shift (KL divergence between logging and target).
Final Polish: Ensure the tone is professional, engineering-focused, and covers edge cases (like propensity underflow).
Let's assemble the plan.
Phase 1: Data Prep & Ingestion: Schema validation, propensity checks.
Phase 2: Reward Modeling (The "D" in DR): Cross-fitting strategy.
Phase 3: Estimation (The "R" in DR): Weight calculation, clipping, aggregation.
This looks solid. I will write the response following this structure.
This plan outlines a production-grade harness for offline evaluation of Contextual Bandits using the Doubly Robust (DR) estimator.
The architecture emphasizes modularity (swapping policies), statistical robustness (handling high variance), and safety (guardrails against distribution shift).
1. System Architecture Overview
The harness acts as a simulator. It replays historical logged data against a new "Target Policy" to estimate performance without online deployment.
Data Flow:
Log Data: Historical context, action, reward, and prob_logging (propensity).
Reward Model (Imputer): A supervised model trained to predict rewards for actions not taken.
Target Policy: The candidate policy (deterministic or stochastic) being evaluated.
Estimator Engine: Calculates IPS, DM, and Doubly Robust scores.
Guardrail System: Validates statistical assumptions and safety constraints.
project/
├── data/
│ └── loader.py # Schema validation, p_log != 0 check
├── models/
│ └── reward_imputer.py # LightGBM/NN to predict rewards
├── policies/
│ ├── base.py # Abstract class
│ └── candidate_v1.py # The policy to test
├── evaluation/
│ ├── estimators.py # IPS, DM, DR logic
│ ├── bootstrap.py # CI calculation
│ └── guardrails.py # ESS, Weight Clipping, Overlap checks
└── main.py # Orchestrator
Summary of Guardrails
Guardrail
Metric
Action if Failed
Propensity Support
min(plog)>ϵ
Filter data or reject run.
Weight Clipping
weight> (e.g., 100)
^
DR
=
n1∑i=1n[r^(xi,ai)+
plog(ai∣xi)πtarget(ai∣xi)(ri−
r^(xi,ai))]
Need to train r^ (reward model).
Need to calculate propensity ratio (weight).
2
Overlapping Support: Check if target proposes actions that log never took (or took with tiny prob).
Model Bias Check: The "Direct Method" part relies on the reward model. If the reward model is biased, DR inherits variance reduction but potential bias. However, DR is consistent as long as either reward model is correct or IPS weights are correct.
r^1
D2
D1
Train r^2 on D1, evaluate on D2.
Combine.
N1
∑i=1N
r^
(
xi
,
πtarget
(
xi
))
(Requires a trained reward model r^. Low variance, potentially biased.)
IPS (Inverse Propensity Scoring):V^IPS=N1∑i=1Npri(Unbiased, but high variance if plog is small.)
Doubly Robust (DR):V^DR=N1∑i=1N[r^(x
(∑wi)2
Rule: If ESS<5% of total log size, flag the evaluation as "Unreliable/Low Support".
logging
If πtarget(a∣x)>0 but πlogging(a∣x)=0, the DR estimator is biased.
τ
Clip weight to τ and log variance warning.
Effective Sample Size
ESS/N<0.05
Report "High Variance / Low Overlap".
Model Bias
R2 of Reward Model
If low, switch estimator preference to IPS (if support allows) or reject.
Significance
Bootstrap CI
Do not deploy if CI crosses current production baseline.