MGMT 675



AI-Assisted Financial Analysis

Optimal Portfolios

Examples

  • From Applied:
    • U.S., U.K., France, Germany, Japan
    • U.S., Developed, Emerging
  • ETFs from Yahoo Finance

Solver in Python

  • There are several choices in python for optimizing functions.
  • The qp function from cvxopt is a very good choice for portfolio optimization.
    • cvxopt = convex optimization
    • qp = quadratic programming

Matrix Multiplication

Primer/review of matrix multiplication

  • Excel’s MMULT function
  • Example:

\[\begin{pmatrix} \color{red}{1} & \color{red}{2} \\ 2 & 2 \end{pmatrix} \begin{pmatrix} \color{red}{3} \\ \color{red}{5} \end{pmatrix} = \begin{pmatrix} \color{red}{13} \\ 16 \end{pmatrix} \]

Primer/review of matrix multiplication

  • Excel’s MMULT function
  • Example:

\[\begin{pmatrix} 1 & 2 \\ \color{red}{2} & \color{red}{2} \end{pmatrix} \begin{pmatrix} \color{red}{3} \\ \color{red}{5} \end{pmatrix} = \begin{pmatrix} 13 \\ \color{red}{16} \end{pmatrix} \]

Primer/review of matrix multiplication

  • Excel’s MMULT function
  • Example:

\[\begin{pmatrix} 1 & 2 \\ \color{red}{2} & \color{red}{2} \end{pmatrix} \begin{pmatrix} \color{red}{3} \\ \color{red}{5} \end{pmatrix} = \begin{pmatrix} 13 \\ \color{red}{16} \end{pmatrix} \]

  • \((2 \times 2)\) * \((2 \times 1)\) = \((2 \times 1)\)
  • height = height of 1st matrix, width = width of 2nd matrix

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} \color{red}{1} & \color{red}{2} \\ 2 & 2 \\ 3 & 2\end{pmatrix} \begin{pmatrix} \color{red}{3} & 2 \\ \color{red}{5} & 4 \end{pmatrix} = \begin{pmatrix} \color{red}{13} & 10 \\ 16 & 12 \\ 19 & 14 \end{pmatrix} \]

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} 1 & 2 \\ \color{red}{2} & \color{red}{2} \\ 3 & 2\end{pmatrix} \begin{pmatrix} \color{red}{3} & 2 \\ \color{red}{5} & 4 \end{pmatrix} = \begin{pmatrix} 13 & 10 \\ \color{red}{16} & 12 \\ 19 & 14 \end{pmatrix} \]

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} 1 & 2 \\ 2 & 2 \\ \color{red}{3} & \color{red}{2}\end{pmatrix} \begin{pmatrix} \color{red}{3} & 2 \\ \color{red}{5} & 4 \end{pmatrix} = \begin{pmatrix} 13 & 10 \\ 16 & 12 \\ \color{red}{19} & 14 \end{pmatrix} \]

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} \color{red}{1} & \color{red}{2} \\ 2 & 2 \\ 3 & 2\end{pmatrix} \begin{pmatrix} 3 & \color{red}{2} \\ 5 & \color{red}{4} \end{pmatrix} = \begin{pmatrix} 13 & \color{red}{10} \\ 16 & 12 \\ 19 & 14 \end{pmatrix} \]

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} 1 & 2 \\ \color{red}{2} & \color{red}{2} \\ 3 & 2\end{pmatrix} \begin{pmatrix} 3 & \color{red}{2} \\ 5 & \color{red}{4} \end{pmatrix} = \begin{pmatrix} 13 & 10 \\ 16 & \color{red}{12} \\ 19 & 14 \end{pmatrix} \]

Another example

  • \((3 \times 2)\) * \((2 \times 2)\) = \((3 \times 2)\)

\[\begin{pmatrix} 1 & 2 \\ 2 & 2 \\ \color{red}{3} & \color{red}{2}\end{pmatrix} \begin{pmatrix} 3 & \color{red}{2} \\ 5 & \color{red}{4} \end{pmatrix} = \begin{pmatrix} 13 & 10 \\ 16 & 12 \\ 19 & \color{red}{14} \end{pmatrix} \]

cvxopt

Matrices in cvxopt: portfolio variance

  • \(w^\top \text{Cov} \,w =\) portfolio variance =

\[ \begin{pmatrix} w_1 & \cdots & w_n \end{pmatrix} \begin{pmatrix} \text{var}_1 & \cdots & \text{cov}_{1n} \\ \vdots & \vdots & \vdots \\ \text{cov}_{1n} & \cdots & \text{var}_n \end{pmatrix} \begin{pmatrix} w_1 \\ \vdots \\ w_n \end{pmatrix}\]

  • \((1 \times n)\) * \((n \times n)\) * \((n \times 1)\) = \((1 \times 1)\)
  • \(P=\) covariance matrix in cvxopt notation

Matrices in cvxopt: equality constraints

  • Weights sum to 1 and expected return = target is expressed as:

\[ \begin{pmatrix} 1 & \cdots & 1 \\ \mu_1 & \cdots & \mu_n \end{pmatrix} \begin{pmatrix} w_1 \\ \vdots \\ w_n \end{pmatrix} = \begin{pmatrix} 1 \\ \mu_{\text{target}}\end{pmatrix}\]

  • \(Aw=b\) in cvxopt notation (Julius should supply \(A\) and \(b\))

Matrices in cvxopt: inequality constraints

  • No short sales is equivalent to:

\[ \begin{pmatrix} -1 & 0 & \cdots & 0 & 0\\ \vdots & \vdots & \vdots & \vdots & \vdots \\ 0 & 0 & \cdots & 0 & -1 \end{pmatrix} \begin{pmatrix} w_1 \\ \vdots \\ w_n \end{pmatrix} \le \begin{pmatrix} 0 \\ \vdots \\ 0 \end{pmatrix}\]

  • \(Gw \le h\) in cvxopt notation (Julius should supply \(G\) and \(h\))

Matrices in cvxopt: risk premium

  • Risk premium of a portfolio is

\[\begin{pmatrix} \mu_1 - r_f & \cdots & \mu_n - r_f \end{pmatrix} \begin{pmatrix} w_1 \\ \vdots \\ w_n \end{pmatrix}\]

  • \(q^\top w\) in cvxopt notation (Julius should supply \(q\))

Frontier and tangency portfolios

Frontier portfolios w/o risk-free asset

  • Frontier \(\sim\) hyperbola in mean/std dev space
  • Minimize variance subject to expected return = target and weights sum to 1
  • Vary target expected return and trace out frontier

Tangency portfolio

  • Include risk-free asset
  • Minimize variance minus risk premium
  • Produces portfolio on capital allocation line
  • Divide by sum of weights to get tangency portfolio

Example 1: U.S., U.K., France, Germany, and Japan

Data

  • Download international.xlsx and international_corrs.xlsx from the course website.
  • Upload the files to Julius. Ask Julius to read them.
  • Ask Julius to convert the expected returns, standard deviations, and correlation matrix into separate numpy arrays.
  • Ask Julius to compute the covariance matrix from the standard deviations and correlation matrix.

Frontier portfolios (w/o risk-free asset)

  • Ask Julius to use cvxopt to minimize variance subject to achieving a target expected return.
  • Tell Julius whether you want to allow short sales. To allow short sales, tell Julius there are no inequality constraints.
  • Ask Julius to repeat for a range of target expected returns and to plot the expected returns and standard deviations.

Tangency Portfolio

  • Give Julius a number for the risk-free rate.
  • Ask Julius to minimize the variance minus the risk premium.
  • Tell Julius whether you want to allow short sales.

  • Ask Julius to divide by the sum of the weights to compute the tangency portfolio.
  • Ask Julius to include the tangency portfolio and the capital allocation line on the previous plot.

Example 2: U.S., Developed, and Emerging

U.S., Developed, and Emerging

  • Start a new chat.
  • Upload us_developed_emerging_rets.xlsx and ask Julius to read it.
  • Ask Julius to compute the sample means, sample standard deviations and sample correlation matrix as numpy arrays.
  • Repeat the frontier and tangency portfolio calculations.

Example 3: ETFs

ETFs from Yahoo

  • Example: ask Julius to use yfinance to get Yahoo adjusted closing prices for
    • SPY = S&P 500
    • VBR = Vanguard small-cap value
    • IEF = Treasury bonds
    • UUP = U.S. dollar bullish
  • Ask Julius to downsample prices to end-of-month and compute monthly returns as percent changes in the downsampled prices.

  • Ask Julius to compute means, standard deviations, and correlation matrix as numpy arrays.
  • Ask Julius to find frontier of risky assets and tangency portfolio as before.