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 and variance . 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 subject to and . Lagrange multipliers give the solution in closed form. With
the weights are with , , and the variance is , a parabola in m. Everything here is a fraction when the data are. The minimum-variance portfolio is the vertex, .
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, , 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 has weights proportional to . Its squared Sharpe ratio is the exact fraction .
The capital asset pricing model says an asset's expected return is with . capm computes β from paired returns, the required return, and α, the average return in excess of what the model requires.
| call | returns |
|---|---|
| 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 |
@ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.