Optimization Methods

Portfolio weight allocation techniques from Modern Portfolio Theory.

Live Demo — Efficient Frontier

Generated from 252 days of sample data for AAPL, MSFT, GOOGL, AMZN, TSLA

Comparison

MethodReturnVolatilitySharpeAAPLMSFTGOOGLAMZNTSLA
MSR5.22%9.71%0.5415.0%40.0%15.0%15.0%15.0%
GMV-0.33%8.57%-0.0420.8%23.5%22.0%22.9%10.9%
EW-0.64%9.92%-0.0620.0%20.0%20.0%20.0%20.0%
MSR

Maximum Sharpe Ratio (MSR)

Finds the portfolio weights that maximize the Sharpe ratio — the best risk-adjusted return.

Objective
max_w  (w^T * μ - r_f) / √(w^T * Σ * w)

Subject to: Σw_i = 1, w_min ≤ w_i ≤ w_max. Where μ = expected returns, Σ = covariance matrix, r_f = risk-free rate.

How It Works

Uses scipy.optimize.minimize with SLSQP method to find weights that minimize the negative Sharpe ratio (equivalent to maximizing Sharpe), subject to weight constraints (min 1%, max 95% per asset) and the constraint that weights sum to 1.

Characteristics

  • Highest Sharpe ratio among all portfolios on the frontier
  • Located on the tangent line from the risk-free rate to the frontier
  • Concentrates in high-return, low-correlation assets
  • Sensitive to expected return estimates

When to Use

Best when you have reliable return forecasts and want to maximize reward per unit of risk.

Code Example

from tradepilot.optimization import msr

weights = msr(
    riskfree_rate=0.04,
    er=expected_returns,
    cov=covariance_matrix,
    min_w=0.01, max_w=0.95
)
GMV

Global Minimum Variance (GMV)

Finds the portfolio with the lowest possible volatility — the leftmost point on the efficient frontier.

Objective
min_w  √(w^T * Σ * w)

Subject to: Σw_i = 1, w_min ≤ w_i ≤ w_max. Uses the MSR trick: when all expected returns are equal, maximizing Sharpe minimizes volatility.

How It Works

Calls the MSR optimizer with all expected returns set to 1 and risk-free rate of 0. When returns are identical, maximizing Sharpe ratio is equivalent to minimizing portfolio variance.

Characteristics

  • Lowest volatility portfolio on the efficient frontier
  • Does not require expected return estimates
  • Only depends on the covariance matrix
  • More diversified than MSR typically

When to Use

Best when return estimates are unreliable or when the primary goal is capital preservation and risk minimization.

Code Example

from tradepilot.optimization import gmv

weights = gmv(
    cov=covariance_matrix,
    min_w=0.01, max_w=0.95
)
EW

Equal Weight (EW)

Allocates equal weight to every asset in the portfolio — the simplest possible allocation.

Objective
w_i = 1 / N  for all i

Where N is the number of assets. No optimization needed — just divide equally.

How It Works

Returns a weight vector where each element is 1/N. Serves as a baseline benchmark for comparing optimization methods.

Characteristics

  • No estimation error — completely model-free
  • Maximum naive diversification
  • Often surprisingly competitive (the "1/N puzzle")
  • Zero rebalancing complexity

When to Use

Best as a robust baseline, when you distrust all forecasts, or for simple rebalancing strategies.

Code Example

from tradepilot.optimization import eq_weighted

weights = eq_weighted(expected_returns)
# Returns array([0.2, 0.2, 0.2, 0.2, 0.2]) for 5 assets