Topic

Portfolios

A portfolio is a list of weights. Its expected return is a weighted sum and its variance a quadratic form, so with rational data both are exact fractions, and so is the whole efficient frontier. Only a standard deviation, a square root, comes back as an interval.

const p = portfolio({
  names: ["bonds", "shares", "property"],
  means: ["4%", "8%", "6%"],
  covariance: [["1/400", "1/1000", "0"], ["1/1000", "1/25", "1/100"], ["0", "1/100", "1/50"]],
});
p.minimumVariance();          // weights, expected return, variance (exact) and risk (enclosed)
p.frontier("6%");             // the least risk for a 6% expected return, short sales allowed
p.frontierLongOnly("6%");     // the same with every weight ≥ 0, with a certificate
p.tangency("2%");             // the best ratio of excess return to risk
capm({ asset, market, riskFree: "2%" });   // beta, alpha and the required return

Return and risk

With expected returns μ and covariance matrix Σ, weights w (adding to 1) give expected return w⊤μw^\top\mu and variance w⊤Σww^\top\Sigma w. The data can be given as means and covariances, or as lists of past returns (returnsFrom(prices) turns prices into returns), from which the sample means and covariances are computed exactly.

The efficient frontier

For a target expected return m, the least-variance portfolio with short sales allowed minimises w⊤Σww^\top\Sigma w subject to 1⊤w=1\mathbf 1^\top w = 1 and μ⊤w=m\mu^\top w = m. Lagrange multipliers give the solution in closed form. With

A=1⊤Σ−11,B=1⊤Σ−1μ,C=μ⊤Σ−1μ,D=AC−B2,A = \mathbf 1^\top\Sigma^{-1}\mathbf 1,\quad B = \mathbf 1^\top\Sigma^{-1}\mu,\quad C = \mu^\top\Sigma^{-1}\mu,\quad D = AC - B^2,

the weights are w=Σ−1(λ1+γμ)w = \Sigma^{-1}(\lambda\mathbf 1 + \gamma\mu) with λ=(C−Bm)/D\lambda = (C - Bm)/D, γ=(Am−B)/D\gamma = (Am - B)/D, and the variance is (Am2−2Bm+C)/D(Am^2 - 2Bm + C)/D, a parabola in m. Everything here is a fraction when the data are. The minimum-variance portfolio is the vertex, w=Σ−11/Aw = \Sigma^{-1}\mathbf 1/A.

No short sales

Requiring every weight to be at least zero turns the problem into a quadratic program, with no closed form. For a few assets it can still be solved exactly. For each set S of assets that might be held, solve the equality-constrained problem on S, which is a linear system. Keep the solution if its weights are not negative and if, for every asset left out, 2(Σw)j−λ−γμj≥02(\Sigma w)_j - \lambda - \gamma\mu_j \ge 0, which means adding a little of that asset would not lower the variance. These are the Karush–Kuhn–Tucker conditions. The problem is convex, so meeting them proves the minimum. The multipliers are returned as the certificate. Trying every subset limits this to 12 assets.

The tangency portfolio and CAPM

With a risk-free rate r, the portfolio with the largest Sharpe ratio (w⊤μ−r)/w⊤Σw(w^\top\mu - r)/\sqrt{w^\top\Sigma w} has weights proportional to Σ−1(μ−r1)\Sigma^{-1}(\mu - r\mathbf 1). Its squared Sharpe ratio is the exact fraction (μ−r1)⊤Σ−1(μ−r1)(\mu - r\mathbf 1)^\top\Sigma^{-1}(\mu - r\mathbf 1).

The capital asset pricing model says an asset's expected return is r+β(E[Rm]−r)r + \beta(E[R_m] - r) with β=cov⁡(R,Rm)/var⁡(Rm)\beta = \operatorname{cov}(R, R_m)/\operatorname{var}(R_m). capm computes β from paired returns, the required return, and α, the average return in excess of what the model requires.

callreturns
portfolio({ names, means, covariance } or { returns })Portfolio
.expectedReturn(w), .variance(w), .risk(w)exact, exact, enclosed
.minimumVariance(), .frontier(m){ weights, w, expectedReturn, variance, risk }
.frontierLongOnly(m)the same with support and certificate { lambda, gamma }
.tangency(r)the same with sharpeSquared and sharpe
capm({ asset, market, riskFree, marketReturn }){ beta, alpha, required, marketPremium }
returnsFrom(prices)simple returns, exact
Try itEdit the code and run it with @ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs