Linear algebra and risk builtins
29 builtins of Soma 3.2.4 in the category Linear algebra and risk, with their exact signatures, as soma describe --builtins --json prints them.
- matrix
- mat
- reshape
- transpose
- shape
- matmul
- det
- diag_sum
- identity
- scale
- flatten_mat
- rows
- cols
- eye
- zeros
- ones
- diag
- to_sampled
- sample_row
- drop_sampled
- importance_sample_rows
- svd_lowrank
- regress_sgd
- clean_covariance
- impact_sqrt
- quantile
- var_historical
- expected_shortfall_historical
- var_gaussian
matrix
matrix("1 2; 3 4") -> List<List<Float>>MATLAB-style matrix literal: ';' separates rows, whitespace/',' separates entries.
mat
mat(rows: Int, cols: Int, values: List<Float>) -> List<List<Float>>Reshape a flat list into an r×c matrix; errors if the count mismatches.
reshape
reshape(values, rows: Int, cols: Int) -> MatrixLay a flat list or matrix out row-major as rows×cols; also m.reshape(r,c).
transpose
transpose(m: Matrix) -> MatrixTranspose; also m.transpose().
shape
shape(m) -> List<Int>[rows, cols] for a matrix, [n] for a vector; also m.shape().
matmul
matmul(a: Matrix, b: Matrix) -> MatrixMatrix product (also the * operator on two matrices); inner dims must agree.
det
det(m: Matrix) -> FloatDeterminant of a square matrix (LU with partial pivoting).
Used in verified programs:
- math/linear_solve, line 21: let detA = det(A)
- math/matrix_algebra, line 34: "det_A", det(A),
diag_sum
diag_sum(m: Matrix) -> FloatMatrix trace: sum of the diagonal.
Used in verified programs:
- math/matrix_algebra, line 24: return diag_sum(m)
identity
identity(n: Int) -> Matrixn×n identity matrix (alias of eye).
Used in verified programs:
- math/matrix_algebra, line 53: assert det(identity(4)) == 1.0
scale
scale(m: Matrix, k) -> MatrixScalar-multiply every entry (also k * m).
flatten_mat
flatten_mat(m: Matrix) -> List<Float>Flatten a matrix to a row-major vector.
rows
rows(r1: List<Float>, r2: List<Float>, ...) -> List<List<Float>>Build a matrix from row vectors.
cols
cols(c1: List<Float>, c2: List<Float>, ...) -> List<List<Float>>Build a matrix from column vectors (transposes).
eye
eye(n: Int) -> List<List<Float>>n×n identity matrix.
zeros
zeros(r: Int, c: Int) -> List<List<Float>>r×c matrix of zeros.
ones
ones(r: Int, c: Int) -> List<List<Float>>r×c matrix of ones.
diag
diag(values: List<Float>) -> List<List<Float>>Square diagonal matrix from a list.
to_sampled
to_sampled(A: List<List<Float>>, opts?: {max_rows, max_cols}) -> MapBuild a BST-backed length-squared sampling handle (Tang); O(log n) per sample after.
sample_row
sample_row(A) -> MapDraw one row index by ℓ²-norm importance sampling (time-seeded PRNG).
drop_sampled
drop_sampled(handle: Map) -> BoolFree a to_sampled() registry entry; true if it existed.
importance_sample_rows
importance_sample_rows(A, opts: {samples, max_dim}) -> MapSample rows by squared-norm importance (time-seeded PRNG).
svd_lowrank
svd_lowrank(A, opts: {row_samples, col_samples, rank, max_dim}) -> MapSublinear randomized low-rank SVD with declared sampling bounds.
regress_sgd
regress_sgd(A, b: List<Float>, opts: {eps, eta, lambda, max_iter, rigorous, samples_per_iter, max_dim}) -> MapRidge regression via stochastic gradient descent with declared bounds.
clean_covariance
clean_covariance(returns: List<List<Float>>, opts: {method: "rie"|"clip"|"raw", eta, center, max_assets, max_obs}) -> MapRMT (Bouchaud-Potters) covariance cleaning: rows are observations (T), columns assets (N); the sample covariance divides by T (population — numpy cov uses T-1); center (default true) subtracts each column mean; clip replaces the eigenvalues inside the Marchenko-Pastur bulk by their mean; .matrix is the cleaned N×N, .eigenvalues the cleaned spectrum (no eigenvectors). max_obs / max_assets past the data raise kind range.
impact_sqrt
impact_sqrt(qty: Float, daily_volume: Float, sigma: Float, opts?: {Y}) -> MapBouchaud square-root market-impact law; .bps is expected slippage.
quantile
quantile(values: List<Float>, q: Float) -> Floatq-th quantile with linear interpolation between the two nearest sorted values (numpy's default): quantile(xs, 0.5) == median(xs).
var_historical
var_historical(returns: List<Float>, opts?: {alpha, max_obs}) -> FloatHistorical Value-at-Risk as a POSITIVE loss: -quantile(returns, 1 - alpha) (returns positive for gains); alpha in (0, 1), else kind range; more observations than max_obs raise kind range.
expected_shortfall_historical
expected_shortfall_historical(returns: List<Float>, opts?: {alpha, max_obs}) -> FloatHistorical expected shortfall (CVaR) as a positive loss: minus the mean of the returns at or below the (1 - alpha) quantile; alpha in (0, 1); max_obs as for var_historical.
var_gaussian
var_gaussian(returns: List<Float>, opts?: {alpha, mu, sigma, max_obs}) -> FloatGaussian VaR assuming N(mu, sigma^2); moments inferred unless overridden.