Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save quantra-go-algo/25f7b7715d2ff277fd1584cbd95e2611 to your computer and use it in GitHub Desktop.

Select an option

Save quantra-go-algo/25f7b7715d2ff277fd1584cbd95e2611 to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 8: The Walk-Forward Loop: Keeping It Honest (snippet 1)
def run_walk_forward_oos(feat_all):
oos_start = pd.to_datetime(OOS_START)
months = month_starts(feat_all.index, oos_start)
cache = load_cache(POLICY_CACHE_FILE)
# Compute lags over FULL history so month boundaries never force flat.
# state_lag[t] = state at close of t-1, available before t opens.
state_lag_full = feat_all['state'].shift(1)
vol_lag_full = feat_all['vol'].shift(1)
month_blocks = [] # (test_index, policy, guard_params)
best_guard = None
for m in months:
next_m = (m + pd.offsets.MonthBegin(1)).normalize()
test = feat_all.loc[(feat_all.index >= m) & (feat_all.index < next_m)]
if len(test) < 5: continue
train_start = (m - pd.DateOffset(years=TRAIN_YEARS)).normalize()
train = feat_all.loc[(feat_all.index >= train_start) & (feat_all.index < m)]
if len(train) < 252: continue # need at least ~1 year
policy = build_policy_for_month(m, train, cache)
if best_guard is None or ((m.month - 1) % OPT_FREQ_MONTHS == 0):
best_guard = optimize_guardrails(train, policy)
month_blocks.append((test.index, policy, best_guard))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment