This project implements a heuristic algorithm for financial portfolio optimization. It utilizes a Biased-Randomized (BR) approach combined with Local Search to find optimal asset allocations, aiming to maximize the Sharpe Ratio and expected returns while managing risk. It is accesible at portfolio-br.streamlit.app.
The application features an interactive web interface built with Streamlit, allowing users to configure parameters, select assets, and compare the custom algorithm against industry-standard benchmarks like Markowitz Mean-Variance, Risk Parity, and Hierarchical Risk Parity (HRP).
- Biased-Randomized Algorithm: Implements a multi-start heuristic using geometric and triangular distributions to explore the solution space efficiently.
- Local Search Optimization: Refines solutions generated by the constructive phase to avoid local optima.
- Flexible Objective Function: Allows users to weight the optimization goal between risk-adjusted returns (Sharpe Ratio) and raw returns.
- Benchmark Comparison: Automatically compares results against:
- Equal Weight (1/N)
- Minimum Variance
- Risk Parity
- Hierarchical Risk Parity (HRP)
- Maximum Diversification
- Greedy Heuristic
- Interactive Visualizations: Includes efficient frontier plots, allocation charts, rolling performance metrics, and correlation heatmaps using Plotly.
- Data Management: Fetches real-time data via Yahoo Finance and supports caching/saving datasets for offline analysis.
The core algorithm departs from traditional quadratic programming by using a biased-randomized heuristic approach. It constructs portfolios iteratively:
-
Ranking: Assets are ranked based on individual performance metrics (e.g., Sharpe Ratio).
-
Biased Selection: Assets are selected for the portfolio using a non-uniform distribution (Geometric or Triangular), favoring higher-ranked assets but maintaining randomness to ensure diversity.
-
Allocation: Weights are assigned based on a randomized allocation scheme constrained by lower and upper bounds.
-
Refinement: A local search process adjusts weights to maximize the objective function defined as:
$f(w) = \gamma * SharpeRatio + \omega * PortfolioReturn$
The codebase is modularized into three main components to ensure separation of concerns:
Portfolio Optimizer.py: The entry point for the Streamlit application. Handles user interface, state management, and interaction flow.financial_model.py: Contains the core business logic, including data fetching, mathematical calculations, and the implementation of the optimization algorithms.visualization.py: Dedicated module for generating Plotly charts and graphs, keeping the UI logic clean.
-
Clone the repository:
git clone https://github.com/hsannav/portfolio-deployment.git cd portfolio-deployment -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the Streamlit application:
streamlit run 'Portfolio Optimizer.py' -
The application will open in your default web browser.
-
In the sidebar:
- Select "New/Existing configuration".
- Choose a preset portfolio (e.g., Tech Giants) or enter custom tickers.
- Adjust the objective weights (Sharpe vs. Return).
- Set optimization parameters (number of iterations, beta).
- Click "Run optimization".
- Python 3.8+
- streamlit
- numpy
- pandas
- yfinance
- scipy
- plotly
This project is for educational and demonstration purposes only. It does not constitute financial advice. Algorithmic trading and portfolio optimization involve significant risk.