This project is a comprehensive quantitative finance toolkit that combines Portfolio Optimization, Risk Management, and Derivatives Pricing.
- Allocators (Optimization): Constructs the "ideal" portfolio using Modern Portfolio Theory (MPT) and Efficient Frontier analysis.
- Risk Engine (Simulation): Stress-tests the portfolio against historical crashes (e.g., 2008 Financial Crisis) and custom shock scenarios using Modified Geometric Brownian Motion.
- Derivatives (Hedging): Calculates option prices (Black-Scholes) and Greeks to design hedging strategies for the equity positions.
Click here to view the Interactive Dashboard
- Core Logic:
Python,NumPy,Pandas,SciPy - Visualization:
Plotly(Interactive Charts),Matplotlib - Dashboard:
Streamlit - Testing:
Pytest
- Clone the repository:
git clone https://github.com/eddiesung111/portfolio-optimizer-engine.git
- Create a virtual environment:
python3 -m venv .venv source .venv/bin/activate - Install dependencies:
pip3 install -r requirements.txt
Goal: Construct the "Efficient Frontier" portfolio that offers the highest expected return for a given level of risk.
python3 main.pyThe project solves the following optimization problem:
Subject to:
-
Unity Constraint:
$\sum_{i=1}^{N} w_i = 1$ -
Long-Only Constraint:
$0 \leq w_i \leq 1$
OPTIMAL PORTFOLIO ALLOCATION (Max Sharpe: 1.32)
------------------------------------------------
TSLA : 68.97%
NVDA : 26.01%
GOOG : 5.01%
Goal: Stress-test the optimized portfolio against thousands of potential future market paths using Geometric Brownian Motion (GBM).
-
Stochastic Process: We model stock price paths (
$S_t$ ) using Geometric Brownian Motion (GBM):
-
$\mu$ : Expected Return (Drift) -
$\sigma$ : Volatility -
$dW_t$ : Wiener Process (Random Walk)
- Stress Testing (Regime Switching): Unlike standard VaR models that assume normal conditions, this engine allows for Parameter Tampering to simulate market crashes.
- Covariance Decomposition: The covariance matrix
$\Sigma$ is decomposed into Volatility ($D$ ) and Correlation ($R$ ) matrices ($\Sigma = D \cdot R \cdot D$ ). - Shock Injection:
-- 2008 Crash Scenario: Volatility is tripled (
$\sigma \times 3$ ) and correlations are forced to 0.9 (Systemic Failure). -- Tech Bubble: Sector-specific shocks are applied to Tech tickers, decoupling them from the broader market.
Maps the risk-return profile of random portfolios vs. the optimal allocation.

Uses Geometric Brownian Motion (GBM) to model correlated asset paths over a 2-year horizon (10,000 iterations).

A histogram of final portfolio values showing the 95% VaR threshold (the red dashed line).

Goal: Calculate theoretical prices for European Call/Put options and visualize market sensitivity (Greeks).
python3 src/options_pricer.pyOpens an interactive 3D plot window.
Implements the closed-form Black-Scholes Model:
Where
This module also generates a 3D Volatility Surface to visualize how option prices react to changes in underlying price and volatility.
-
Call Option Volatility Surface Visualizes the relationship between Underlying Price, Volatility, and Call Price.

-
Put Option Volatility Surface Visualizes the Put Price surface. Note the inverse relationship with stock price compared to the Call option.

All three modules are integrated into a single interactive web application.
streamlit run dashboard.pyThe project uses pytest to ensure mathematical accuracy (e.g., weights summing to 1.0, Put-Call Parity).
To run the full test suite:
pytestportfolio-optimization-engine/
βββ src/
β βββ __init__.py # Package marker
β βββ data_loader.py # Yfinance fetcher
β βββ portfolio_optimizer.py# Mean-Variance Solver
β βββ risk_manager.py # Monte Carlo Engine
β βββ options_pricer.py # Black-Scholes & 3D Plotting
βββ tests/
β βββ test_optimizer.py # Optimizer tests
β βββ test_pricer.py # Black-Scholes tests
βββ results/
β βββ monte_carlo_simulation.png
β βββ final_value_distribution.png
β βββ option_surface.png
βββ main.py # Orchestrator script
βββ dashboard.py # Main Streamlit Application
βββ requirements.txt # Dependencies
βββ README.md # Project Documentation
This software is for educational purposes only. Past performance is not indicative of future results.