Solving the Capital Allocation Problem with Python
Today, I’m excited to explore an interesting financial mathematics problem: the capital allocation problem.
This problem is fundamental in portfolio management, where we need to decide how to distribute funds across different investment opportunities to achieve optimal returns while managing risk.
Let’s dive into a concrete example and solve it using Python.
I’ll walk you through the entire process, from mathematical formulation to visualization of results.
Problem Statement
Imagine we have $10,000 to invest in 4 different assets: stocks, bonds, real estate, and commodities.
Each asset has its own expected return, volatility (risk), and correlation with other assets.
Our goal is to find the optimal allocation that maximizes the Sharpe ratio (return per unit of risk).
Mathematical Formulation
The Sharpe ratio is defined as:
$$S = \frac{E[R] - R_f}{\sigma}$$
Where:
- $E[R]$ is the expected return of the portfolio
- $R_f$ is the risk-free rate
- $\sigma$ is the standard deviation (volatility) of the portfolio
For a portfolio with weights $w_i$ for each asset $i$, the expected return is:
$$E[R_p] = \sum_{i=1}^{n} w_i E[R_i]$$
And the portfolio variance is:
$$\sigma_p^2 = \sum_{i=1}^{n} \sum_{j=1}^{n} w_i w_j \sigma_i \sigma_j \rho_{ij}$$
Where $\rho_{ij}$ is the correlation between assets $i$ and $j$.
Let’s implement this in Python:
1 | import numpy as np |
Code Explanation
Let’s break down what the code does:
Setup: We first define our assets, expected returns, volatilities, and correlation matrix.
These are the inputs to our optimization problem.Covariance Matrix: We calculate the covariance matrix from the correlation matrix and volatilities.
This is essential for calculating portfolio risk.Portfolio Performance: We create functions to calculate the expected return and volatility of a portfolio given certain weights.
Optimization: We use
scipy.optimize.minimizeto find the weights that maximize the Sharpe ratio (or minimize the negative Sharpe ratio).
We set constraints (weights sum to 1) and bounds (no short selling, so weights are between $0$ and $1$).Monte Carlo Simulation: We generate $10,000$ random portfolios to visualize the efficient frontier.
Performance Comparison: We compare our optimal portfolio with some alternative allocations (equal weights, stocks-heavy, bonds-heavy).
Results and Visualization
The optimization process identifies the optimal portfolio weights that maximize the Sharpe ratio.
Let’s look at the key findings:
- Optimal Allocation: The optimization suggests a diversified portfolio with specific weights for each asset class.

Optimal Portfolio Weights: Stocks: 0.1700 (17.00%) Bonds: 0.5481 (54.81%) Real Estate: 0.1113 (11.13%) Commodities: 0.1706 (17.06%) Optimal Portfolio Performance: Expected Annual Return: 0.0738 (7.38%) Annual Volatility: 0.0927 (9.27%) Sharpe Ratio: 0.5802 Capital Allocation for $10000: Stocks: $1700.10 Bonds: $5480.85 Real Estate: $1113.21 Commodities: $1705.84
- Risk-Return Profile: The efficient frontier plot shows the relationship between risk and return for different portfolios. The optimal portfolio (marked with a red star) provides the best risk-adjusted return.

- Capital Allocation: For our $10,000 investment, we can see exactly how much to allocate to each asset class.

- Comparison: The bar charts compare the optimal allocation with alternative strategies in terms of expected return, volatility, and Sharpe ratio.

Insights and Conclusions
This example demonstrates the power of modern portfolio theory and optimization techniques in solving capital allocation problems.
The key insights are:
Diversification Benefits: The optimal portfolio is well-diversified across all asset classes, taking advantage of the correlation structure to reduce overall risk.
Risk-Return Tradeoff: There’s a clear tradeoff between risk and return, but optimization helps us find the sweet spot that maximizes risk-adjusted returns.
Practical Application: With this approach, investors can make data-driven decisions about capital allocation rather than relying on intuition alone.
In practice, this approach would be enhanced with more sophisticated models, historical data analysis, and forward-looking scenarios.
However, the fundamental principles demonstrated here remain the same across more complex implementations.
The capital allocation problem is a perfect example of how mathematical optimization can be applied to real-world financial decisions, helping individuals and institutions make better investment choices.












