Skip to content

Code

Monte Carlo Simulation

Estimate an option price without closed-form formula (or when a closed form exists but a numerical check is wanted). Covers the core Monte Carlo workflow, the standard probability distributions used to draw random inputs, how pseudo-random number generators actually work (linear congruential generator, Mersenne Twister), and a worked example: pricing a European call option by simulating Geometric Brownian Motion price paths and comparing to the Black-Scholes closed form, including a variance-reduction technique (antithetic variates).

Monte Carlo option price: 10.4137 (95% CI +/- 0.2863, n=10,000) vs. Black-Scholes closed-form price: 10.4506, agreement within about 0.4%. Variance reduction by an average of 50.9% versus plain Monte Carlo at matched sample sizes.


Time-Varying Coefficients (Kalman Filter vs. MCMC)

The assumption of a fixed relationship between predictors and returns is dropped and the true coefficients are allowed to drift. The project compares the Kalman filter (a recursive, online estimator of a time-varying state) against three MCMC samplers, Gibbs, Metropolis-Hastings, and Adaptive Metropolis, all targeting a static Bayesian regression posterior on the same 6 pre-selected predictors. A follow-up section combines both methods: MCMC is used to put a full posterior (not a single point estimate) on the Kalman filter's own process- and observation-noise hyperparameters.

Effective sample size (of 4,500 post-burn-in draws): Gibbs 4,500, Metropolis-Hastings 53, Adaptive Metropolis 179. Cross-validated Kalman filter hyperparameter search: 1.41% one-step-ahead validation RMSE improvement from allowing the coefficients to drift, versus holding them fixed. Out-of-sample cumulative P&L / Sharpe ratio (250 days): OLS 3.6913 / 7.39, MCMC (Gibbs posterior mean) 3.6934 / 7.40, Kalman filter (online) 4.2871 / 8.14. Bayesian Kalman filter hyperparameter recovery: posterior mean process_std 0.0102 (true 0.01) and posterior mean obs_std 0.0556 (true 0.0558), both well inside their 90% credible intervals.


PCA Dimensionality Reduction

The covariance matrix underlies both simulation (sampling correlated variables via the multivariate normal) and dimensionality reduction (PCA) in financial data. The project simulates a 10-stock, 3-sector portfolio and uses PCA to compress it into a handful of factors, tests how well a low-dimensional reconstruction predicts portfolio returns out of sample, and runs a rolling-window PCA to detect a simulated correlation spike (a stress-period proxy).

6 of 10 components needed to explain 90% of portfolio variance. Out-of-sample portfolio-return reconstruction RMSE: 0.00057 with k=3 components (vs. 0.00000, i.e. exact, with all 10). Rolling 60-day PCA: dominant factor (PC1) variance share rose from a 38.5% baseline to an 89.5% peak during an injected correlation shock, a concrete illustration of rising systemic co-movement.


Estimators for Sparse Return Prediction

Large-dimensional correlated set of candidate predictors (400 simulated assets across 20 sectors) and a target return truly driven by only 6 of them, which estimator recovers the signal and predicts well out of sample: Principal Component Regression, plain OLS, Lasso, or Post-Lasso (double selection, a bias-correction step run after Lasso's variable selection)? All four are converted into simple trading strategies and their 250-day out-of-sample cumulative return traces are compared against actual buy-and-hold.

Population R-squared of the true underlying model: 0.156. Out-of-sample test R-squared: PCR 0.045, OLS -0.171, Lasso 0.097, Post-Lasso 0.095. Out-of-sample test RMSE: PCR 0.0711, OLS 0.0787, Lasso 0.0691, Post-Lasso 0.0692. Lasso and Post-Lasso both recovered all 6 true predictors (Lasso used 29 nonzero coefficients in total, Post-Lasso narrowed this to 8).


Reinforcement Learning vs. Supervised Estimators

Reinforcement-learning agent, trained purely on trial-and-error reward with no labeled return targets, can match supervised estimators (OLS, PCR, Post-Lasso) at the same return-prediction task from the previous notebook. A linear policy-gradient (REINFORCE) agent is trained to size a trading position from the same 400-asset state, with its raw profit-and-loss as the reward signal, then compared against the three supervised models on the same 250-day out-of-sample window.

Out-of-sample cumulative P&L (250 days): Buy&Hold -0.4258, OLS 1.9576, PCR 1.8921, Post-Lasso 2.8842, RL 1.7173. Annualized Sharpe ratio: OLS 3.55, PCR 3.23, Post-Lasso 4.70, RL 1.63. Mean position size (conviction): OLS 0.407, PCR 0.385, Post-Lasso 0.392, RL 0.892. Post-Lasso was the best performer on both cumulative P&L and Sharpe ratio; RL, despite correctly learning the direction of all 6 true signal assets from reward alone, lagged the supervised methods here.


Interest Rate Factor Models and 1-Day-Ahead Prediction

Forecast an entire Treasury yield curve (10 maturities from 3 months to 30 years) using a small number of factors, comparing two prominent approaches: Nelson-Siegel (a parametric level/slope/curvature model) and PCA (a purely empirical factor model). Includes term-spread analysis (10y-2y, 10y-3m) around a simulated hiking-cycle inversion, inflation-adjusted real yields, discount-factor curves, and a bond-pricing example, then runs genuine 1-day-ahead rolling-window prediction of the full curve with both factor models against a random-walk baseline.

Nelson-Siegel factor recovery: correlation with true simulated factors of 0.998 (level), 0.998 (slope), 0.813 (curvature). PCA variance explained: PC1 77.7%, PC2 21.6%, PC3 0.2% (99.51% cumulative), with loading shapes matching the classic level/slope/curvature pattern. 1-day-ahead average RMSE across all maturities: Nelson-Siegel 6.55 bps, PCA 6.00 bps, random walk 6.83 bps. Downstream forecast RMSE: 10y-2y spread 6.04 bps (Nelson-Siegel) / 6.02 bps (PCA); 10-year 4% coupon bond price 0.486 (Nelson-Siegel) / 0.431 (PCA). Simulated term-spread inversion: minimum 10y-2y spread of -53 bps, with 314 of 2,016 trading days inverted.


FX Carry Strategy Backtester

Synthetic, 8 currency pairs vs. USD, 20 years of monthly data (240 observations), spot/forward/PPP generated with a realistic Covered Interest Rate Parity relationship and a deliberately modest carry edge. Not real market data, see the review notes for full methodology and limitations.

Strategy Annualized Return Annualized Vol Sharpe Ratio Max Drawdown
Simple_Carry 1.89% 3.43% 0.551 4.72%
VolAdj_Carry 1.72% 3.28% 0.524 4.72%
Value 1.76% 3.21% 0.549 7.64%
Max_Sharpe_Port -7.01% 15.71% -0.446 77.00%
50/50 Simple_Carry & Value (blend) 1.82% 2.55% 0.713 4.39%
Simple_Carry_Value (composite signal) 1.82% 2.56% 0.711 4.39%

The three rule-based strategies performed similarly (Sharpe 0.52-0.55), a modest, realistic carry edge rather than a dramatic one, consistent with how the synthetic data was calibrated. Blending or combining Simple_Carry with Value improved risk-adjusted return (Sharpe rises to about 0.71 either way), a genuine diversification benefit from combining a carry signal with a largely uncorrelated value signal.