μ
linear algebra library
Linear algebra library
Portfolio construction building blocks from stdlib/linalg.mu: outer product, covariance/correlation matrices, argmin/argmax, log-returns, portfolio variance/return, Markowitz weights, and Kelly sizing.
| name | definition | description |
|---|---|---|
| outer | {[f;xs;ys] {[i] {[x] f (xs@i) x}' ys}' !#xs} | outer[f; xs; ys] — generalised outer product Apply a two-argument function f to every (xs@i, ys@j) pair. Returns a list-of-lists (matrix) of shape (#xs) × (#ys). Example: outer[{[a;b] a+b}; 1 2; 10 20] → (11 21;12 22) |
| covmat | {[cols] {[i] {[j] cov[(cols@i);(cols@j)]}' !#cols}' !#cols} | covmat[cols] — covariance matrix from a list of return-series columns cols: list of N equal-length numeric vectors (one per asset) Returns: N×N symmetric covariance matrix Example: covmat[(0.1 0.2 0.3;0.3 0.2 0.1)] or with explicit list: covmat[[(0.1 0.2 0.3);(0.3 0.2 0.1)]] |
| corrmat | {[cols] {[i] {[j] cor[(cols@i);(cols@j)]}' !#cols}' !#cols} | corrmat[cols] — correlation matrix from a list of return-series columns Returns: N×N symmetric correlation matrix (diagonal = 1.0) Example: corrmat[[(0.1 0.2 0.3);(0.3 0.2 0.1)]] |
| argmin | {(*(< {[i] (((x@i)=(min x))*(1+i))+((0>(x@i)-(min x))*(1+(#x)+i))}' !#x))-1} | argmin[x] — index of the first minimum element (0-based) Strategy: for each index i, map to (i+1) if x@i = min(x), else (1+#x+i). Non-matching indices get large sentinel values; sort ascending and take first element - 1. Avoids grade (unavailable) while staying O(N log N). Example: argmin 3 1 4 1 → 1 |
| argmax | {(*(< {[i] (((x@i)=(max x))*(1+i))+((0>(max x)-(x@i))*(1+(#x)+i))}' !#x))-1} | argmax[x] — index of the first maximum element (0-based) Example: argmax 3 1 4 1 → 2 |
| logrets | {1 _ log(ratios x)} | logrets[prices] — log-return series from a price vector Drops the first element (which is just log of the first price). Returns a vector of length (#prices - 1). Example: logrets 100 105 102 → log(105/100) log(102/105) |
| portvar | {[w;Sigma] +/ w * (Sigma *. w)} | portvar[w; Sigma] — portfolio variance w' Σ w w: weight vector (length N) Sigma: N×N covariance matrix (list-of-lists from covmat or cut) Returns: scalar variance Example: portvar[0.3 0.7; cut 2 (0.04 0.01 0.01 0.09)] → 0.0519 |
| portret | {[w;mu] +/ w * mu} | portret[w; mu] — portfolio expected return w' μ w: weight vector (length N) mu: expected return vector (length N) Returns: scalar expected return Example: portret[0.3 0.7; 0.1 0.2] → 0.17 |
| kelly | {[mu;sigma2;rf] (mu-rf)%sigma2} | kelly[mu; sigma2; rf] — full-Kelly leverage fraction mu: expected return of the asset/portfolio sigma2: variance of returns rf: risk-free rate Returns: scalar fraction f* = (mu - rf) / sigma2 For half-Kelly multiply result by 0.5. Example: kelly[0.1; 0.04; 0.02] → 2.0 |
| mkw | {[Sigma;mu;rf] (Sigma %. (mu - rf)) % (+/(Sigma %. (mu - rf)))} | mkw[Sigma; mu; rf] — Markowitz maximum-Sharpe portfolio weights Solves the KKT system for the tangency (max-Sharpe) portfolio: min w'Σw s.t. 1'w = 1 using the two-fund separation: solve Σw = (μ - rf·1), then normalise. Sigma: N×N covariance matrix mu: N-vector of expected returns rf: scalar risk-free rate Returns: N-vector of portfolio weights summing to 1 Example (2-asset): S: cut 2 (0.04 0.01 0.01 0.09) mkw[S; 0.10 0.15; 0.02] |
| eigenmat | eigen | eigenmat[A] — eigenvalue decomposition of symmetric matrix Uses the Jacobi algorithm to compute eigenvalues and eigenvectors of a symmetric matrix A. Returns (eigenvalues; eigenvectors) where: - eigenvalues: float vector sorted in descending order - eigenvectors: N×N matrix where row i is the eigenvector for eigenvalue i The eigenvectors are orthonormal: (vecs *. transpose vecs) ≈ identity N. Example: S: covmat[(r1;r2;r3)] evd: eigenmat S vals: evd@0 / eigenvalues vecs: evd@1 / eigenvectors |
| inv | invert | inv[A] — matrix inverse of square matrix Computes the inverse of a square matrix A. Automatically detects symmetric positive-definite matrices and uses Cholesky decomposition for efficiency; falls back to Gaussian elimination for general matrices. A: N×N invertible square matrix Returns: N×N inverse matrix A⁻¹ such that (A *. inv A) ≈ identity N Example: A: cut 2 (4.0 1.0 1.0 3.0) A_inv: inv A A *. A_inv / should be approximately identity |