Skip to content

hsannav/portfolio-deployment

Repository files navigation

Biased-Randomized Portfolio Optimizer

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).

Features

  • 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.

Mathematical Background

The core algorithm departs from traditional quadratic programming by using a biased-randomized heuristic approach. It constructs portfolios iteratively:

  1. Ranking: Assets are ranked based on individual performance metrics (e.g., Sharpe Ratio).

  2. 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.

  3. Allocation: Weights are assigned based on a randomized allocation scheme constrained by lower and upper bounds.

  4. Refinement: A local search process adjusts weights to maximize the objective function defined as:

    $f(w) = \gamma * SharpeRatio + \omega * PortfolioReturn$

Project Structure

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.

Installation

  1. Clone the repository:

    git clone https://github.com/hsannav/portfolio-deployment.git
    cd portfolio-deployment
  2. Create a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  3. Install the required dependencies:

    pip install -r requirements.txt

Usage

  1. Run the Streamlit application:

    streamlit run 'Portfolio Optimizer.py'
  2. The application will open in your default web browser.

  3. 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".

Requirements

  • Python 3.8+
  • streamlit
  • numpy
  • pandas
  • yfinance
  • scipy
  • plotly

Disclaimer

This project is for educational and demonstration purposes only. It does not constitute financial advice. Algorithmic trading and portfolio optimization involve significant risk.

About

Biased-Randomization approach for portfolio optimization task

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages