liveBook · Manning

Efficient frontier

As explained in Build a Robo-Advisor with Python (From Scratch)

The efficient frontier is the set of portfolios offering the highest expected return for each level of risk or the lowest risk for each expected return.

The efficient frontier maps the best attainable trade-offs between expected return and risk. Each point offers the highest expected return for its volatility, or the lowest volatility for its return. section 2.3

A risk–reward plot begins with simulated portfolios. Given asset expected returns μ, covariance matrix Σ, and weights w, calculate portfolio return as μₚ = wᵀμ and volatility as σₚ = √(wᵀΣw). section 2.3

From random portfolios to the frontier

Simulate 1,000 portfolios, compute each portfolio’s return and volatility, and plot the results. Then overlay the minimum-variance frontier. Its upper branch is efficient; its lower branch is inefficient because another portfolio provides the same volatility with a higher expected return. section 2.3

Without a risk-free asset, the frontier follows a hyperbola in mean–standard-deviation space. The lowest point is the global minimum-variance portfolio; the efficient frontier begins above it. For A = 1ᵀΣ⁻¹1, B = 1ᵀΣ⁻¹μ, and C = μᵀΣ⁻¹μ, its volatility satisfies σₚ = √((Aμₚ² − 2Bμₚ + C)/(AC − B²)). section 2.7

Listing 2.8 Adding minimum-variance frontier to risk–reward plot
def plot_min_var_frontier(mu, Cov):  #1
    A,B,C = compute_ABC(mu, Cov)
    y = np.linspace(0,B/A,100)   #2
    x = np.sqrt((A*y*y-2*B*y+C)/(A*C-B*B))   #2
    plt.plot(x,y, color='black', lw=2.5, linestyle='--')  #2

    y = np.linspace(B/A,.45,100)  #3
    x = np.sqrt((A*y*y-2*B*y+C)/(A*C-B*B))  #3
    plt.plot(x,y, color='black', lw=2.5, label='Efficient Frontier')  #3
    plt.legend()


def compute_ABC(mu, Cov):  #4
    Cov_inv = np.linalg.inv(Cov)
    ones = np.ones(n_assets)
    A = ones @ Cov_inv @ ones
    B = ones @ Cov_inv @ mu
    C = mu @ Cov_inv @ mu
    return A,B,C

plot_points(mu, sigma, stocks)
plot_random_portfolios(mu, Cov, n_simulations)    
plot_min_var_frontier(mu, Cov)
plt.show()

Inspect the NumPy and Matplotlib implementation that computes A, B, and C, plots random portfolios, and separates the efficient branch from the inefficient branch. section 2.3

A constrained frontier turns the curve into investable portfolio choices. Sweep across target volatility levels and optimize expected return subject to long-only, full-investment, diversification, and asset-group constraints. Some targets may be infeasible. section 10.3.3

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Derives the frontier equation and implements its plot, then extends it to constrained target-volatility optimization while deferring infeasible targets.
Examples
Uses three-stock random portfolios, risk–reward plots, frontier overlays, and constrained portfolios spanning target volatility levels.
What it assumes
Assumes expected returns, covariance matrices, portfolio weights, and matrix operations, while introducing frontier construction through concrete simulations.
What you can do after
Build it from scratch in Python by simulating weights, calculating portfolio statistics, plotting the frontier, and optimizing constrained target-risk portfolios.
How it connects
Connects portfolio construction with expected return, portfolio volatility, covariance matrices, diversification, optimization, and risk-free assets.

Also explained in

Compare how 2 books explain efficient frontier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.