Created
June 24, 2026 01:29
-
-
Save quantra-go-algo/078a6919173a0fa4fb203663766a48ec to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 5: The LLM Risk Manager: Building the Policy Table (snippet 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def state_stats(train_df): | |
| tmp = train_df.copy() | |
| tmp['state_lag'] = tmp['state'].shift(1) # use yesterday's state | |
| tmp['ret_fwd'] = tmp['ret'] # to predict today's return | |
| tmp = tmp.dropna(subset=['state_lag', 'ret_fwd']) | |
| g = tmp.groupby('state_lag')['ret_fwd'] | |
| stats = pd.DataFrame({ | |
| 'count': g.size(), | |
| 'mean': g.mean(), | |
| 'std': g.std(ddof=0), | |
| }) | |
| # Sharpe-like: annualized signal-to-noise ratio per state | |
| stats['sharpe_like'] = ( | |
| stats['mean'] / (stats['std'] + 1e-12) | |
| ) * math.sqrt(252) | |
| return stats.sort_values('count', ascending=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment