from typing import Dict, List import numpy as np import pandas as pd def monte_carlo_quantiles(weight_df: pd.DataFrame, feature_df: pd.DataFrame, horizon_days: int = 126, n_sims: int = 1200) -> Dict[str, float]: tickers = weight_df["ticker"].tolist() weights = weight_df["weight"].values returns_wide = feature_df.pivot(index="date", columns="ticker", values="ret_1d").dropna().loc[:, tickers] mu = returns_wide.mean().values cov = returns_wide.cov().values + np.eye(len(tickers)) * 1e-6 rng = np.random.default_rng(7) sims = rng.multivariate_normal(mu, cov, size=(n_sims, horizon_days)) port_paths = 1.0 + np.einsum("shn,n->sh", sims, weights) terminal = port_paths.prod(axis=1) return {"p05": float(np.quantile(terminal, 0.05)), "p50": float(np.quantile(terminal, 0.50)), "p95": float(np.quantile(terminal, 0.95))} def simple_backtest(weight_df: pd.DataFrame, feature_df: pd.DataFrame) -> List[Dict[str, float]]: tickers = weight_df["ticker"].tolist() weights = weight_df["weight"].values returns_wide = feature_df.pivot(index="date", columns="ticker", values="ret_1d").dropna().loc[:, tickers] benchmark = returns_wide.mean(axis=1) port = returns_wide.values @ weights portfolio_curve = (1 + pd.Series(port, index=returns_wide.index)).cumprod() benchmark_curve = (1 + benchmark).cumprod() points = [] for d in returns_wide.index[-120:]: points.append({"timestamp": float(pd.Timestamp(d).timestamp()), "portfolio": float(portfolio_curve.loc[d]), "benchmark": float(benchmark_curve.loc[d])}) return points