Module 3: Mean-Variance Analysis

MGMT 675: Generative AI for Finance

Kerry Back, Rice University

Claude.ai Is an Agent

Claude.ai is an agent connected to a Python environment — it writes and runs code in a cloud sandbox to answer your questions.

Claude.ai also has access to other tools:

  • Web browser — search the web and read pages
  • File system — read and write files you upload or create
  • Artifacts — build interactive apps and charts in a side panel
  • Document generation — produce Excel, Word, and PowerPoint files

When you ask Claude.ai to analyze data, it plans the code, executes it in the Python environment, observes the result, and iterates — the same agent loop from Module 1.

Portfolio Optimization

The Problem

Given expected returns, risks (standard deviations), and correlations for multiple assets, plus a risk-free rate:

  • What is the best portfolio?
  • What does the set of all possible portfolios look like?
  • How do real-world constraints change the answer?

Key Concepts

Portfolios

  • Tangency portfolio: the risky portfolio with the highest Sharpe ratio (excess return per unit of risk)
  • Global minimum variance (GMV): the risky portfolio with the lowest possible risk
  • Efficient frontier: the set of risky portfolios with the highest return for each level of risk

Capital Allocation Line

  • The straight line from the risk-free rate through the tangency portfolio
  • Represents all combinations of the risk-free asset and the tangency portfolio
  • The best attainable risk-return tradeoff

Solution in Excel

MGMT 648 Example

Real-World Constraints

Common constraints

  • No short sales (all weights \(\geq 0\))
  • Maximum position (e.g., \(\leq 40\%\) per asset)
  • Minimum position (e.g., \(\geq 2\%\) if held)
  • Margin requirement (sum of absolute values \(\leq 2\))

What happens

  • The efficient frontier shifts inward (lower returns and/or higher risk)
  • The tangency portfolio changes
  • The Sharpe ratio cannot improve with constraints—it can only stay the same or get worse

Solvers

  • Solver (numerical optimization)
    • Maximize Sharpe ratio (tangency portfolio)
    • Minimize risk for a target return (efficient frontier)
    • Minimize risk (global minimum variance portfolio)

Python solver options: scipy.minimize, cvxopt, cvxpy—AI chooses and writes the code.

Ask Claude: Discuss the advantages and disadvantages of these solver options for mean-variance analysis with inequality constraints.

In-Class Exercise: Data

We will use monthly returns for four ETFs:

  • SPY — S&P 500
  • GLD — Gold
  • IEF — 7–10 Year Treasuries
  • EFA — International Developed Markets

Data: monthly returns, May 2007 – present. Monthly risk-free rate: \(0.0375/12\).

Download data. Then upload to Claude. Tell Claude the risk-free rate.

In-Class Exercise: No Short Sales

  1. Ask Claude to compute the tangency portfolio with no short sales (all weights \(\geq 0\)). Report the weights, expected return, standard deviation, and Sharpe ratio.
  2. Ask: “Show me the code you used and explain it.”

The Unconstrained Case

  • If we tell Claude to find the tangency portfolio allowing short sales, then
    • the same solvers would work, but
    • Claude will probably take a different route, solving a system of linear equations
  • A similar system yields the GMV portfolio
  • The entire frontier can also be found by solving linear equations
  • Solving linear equations is numerically easier than running a solver. Claude has seen it lots of times in its training data.

Example of Linear Equations

\[3x + 2y = 7\] \[2x + 5y = 4\]

Rewrite in matrix form:

\[\begin{pmatrix} 3 & 2 \\ 2 & 5 \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} 7 \\ 4 \end{pmatrix}\]

Solving with Matrix Inverse

Call the matrix \(\Sigma\), the vector \((x,y)\) as \(w\), and the right-hand side \(\mu\):

\[\Sigma w = \mu \implies w = \Sigma^{-1}\mu = \begin{pmatrix} 27/11 \\ -2/11 \end{pmatrix}\]

Solving Linear Equations for Optimal Portfolios

  • Tangency portfolio: \(\Sigma=\) covariance matrix, \(\mu=\) risk premia
  • GMV portfolio: \(\Sigma=\) covariance matrix, \(\mu=\) constants (ones)
  • Usual code np.linalg.inv(Sigma) @ mu or np.linalg.solve(Sigma, mu) or np.linalg.inv(Sigma).dot(mu)
  • In both cases, after solving equations, scale so that weights sum to one (100% invested)

In-Class Exercise: Tangency Portfolio (Unrestricted)

  1. Ask Claude to compute the tangency portfolio allowing short sales. Report the weights, expected return, standard deviation, and Sharpe ratio.
  2. Ask Claude: “Show me the code you used and explain it step by step.”

In-Class Exercise: Frontier and CAL

  1. Ask Claude to compute the tangency portfolio and the efficient frontier for both cases (with and without short sales).
  2. Ask Claude to plot the efficient frontiers and capital allocation lines for both cases in the same figure.
  3. Ask Claude to also provide a figure with the two cases as stacked subplots with the same scales.

Creating a Reusable Tool

Once we have code that we’re happy with (we’ve read the code and/or compared results to known results), we can package it as a tool to be reused.

  1. Create and deploy a mean-variance app (like a Claude artifact, but many ways to do it), or
  2. Package it as a Python script and create a skill. The skill would say “use the script.””

AI in Excel

  • Open ETF-returns.xlsx
  • Click the Claude icon to open Claude in a sidebar
  • Ask Claude to compute the sample means and sample covariance matrix and to compute the tangency portfolio assuming a risk-free rate of 0.0375/12
  • Repeat using Microsoft Copilot in Excel