soma.cell

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

matrix("1 2; 3 4") -> List<List<Float>>

MATLAB-style matrix literal: ';' separates rows, whitespace/',' separates entries.

deterministic · replay: pure

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.

deterministic · replay: pure

reshape

reshape(values, rows: Int, cols: Int) -> Matrix

Lay a flat list or matrix out row-major as rows×cols; also m.reshape(r,c).

deterministic · replay: pure

transpose

transpose(m: Matrix) -> Matrix

Transpose; also m.transpose().

deterministic · replay: pure

shape

shape(m) -> List<Int>

[rows, cols] for a matrix, [n] for a vector; also m.shape().

deterministic · replay: pure

matmul

matmul(a: Matrix, b: Matrix) -> Matrix

Matrix product (also the * operator on two matrices); inner dims must agree.

deterministic · replay: pure

det

det(m: Matrix) -> Float

Determinant of a square matrix (LU with partial pivoting).

deterministic · replay: pure

Used in verified programs:

diag_sum

diag_sum(m: Matrix) -> Float

Matrix trace: sum of the diagonal.

deterministic · replay: pure

Used in verified programs:

identity

identity(n: Int) -> Matrix

n×n identity matrix (alias of eye).

deterministic · replay: pure

Used in verified programs:

scale

scale(m: Matrix, k) -> Matrix

Scalar-multiply every entry (also k * m).

deterministic · replay: pure

flatten_mat

flatten_mat(m: Matrix) -> List<Float>

Flatten a matrix to a row-major vector.

deterministic · replay: pure

rows

rows(r1: List<Float>, r2: List<Float>, ...) -> List<List<Float>>

Build a matrix from row vectors.

deterministic · replay: pure

cols

cols(c1: List<Float>, c2: List<Float>, ...) -> List<List<Float>>

Build a matrix from column vectors (transposes).

deterministic · replay: pure

eye

eye(n: Int) -> List<List<Float>>

n×n identity matrix.

deterministic · replay: pure

zeros

zeros(r: Int, c: Int) -> List<List<Float>>

r×c matrix of zeros.

deterministic · replay: pure

ones

ones(r: Int, c: Int) -> List<List<Float>>

r×c matrix of ones.

deterministic · replay: pure

diag

diag(values: List<Float>) -> List<List<Float>>

Square diagonal matrix from a list.

deterministic · replay: pure

to_sampled

to_sampled(A: List<List<Float>>, opts?: {max_rows, max_cols}) -> Map

Build a BST-backed length-squared sampling handle (Tang); O(log n) per sample after.

deterministic · replay: pure

sample_row

sample_row(A) -> Map

Draw one row index by ℓ²-norm importance sampling (time-seeded PRNG).

deterministic · replay: pure

drop_sampled

drop_sampled(handle: Map) -> Bool

Free a to_sampled() registry entry; true if it existed.

deterministic · replay: pure

importance_sample_rows

importance_sample_rows(A, opts: {samples, max_dim}) -> Map

Sample rows by squared-norm importance (time-seeded PRNG).

deterministic · replay: pure

svd_lowrank

svd_lowrank(A, opts: {row_samples, col_samples, rank, max_dim}) -> Map

Sublinear randomized low-rank SVD with declared sampling bounds.

deterministic · replay: pure

regress_sgd

regress_sgd(A, b: List<Float>, opts: {eps, eta, lambda, max_iter, rigorous, samples_per_iter, max_dim}) -> Map

Ridge regression via stochastic gradient descent with declared bounds.

deterministic · replay: pure

clean_covariance

clean_covariance(returns: List<List<Float>>, opts: {method: "rie"|"clip"|"raw", eta, center, max_assets, max_obs}) -> Map

RMT (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.

deterministic · replay: pure

impact_sqrt

impact_sqrt(qty: Float, daily_volume: Float, sigma: Float, opts?: {Y}) -> Map

Bouchaud square-root market-impact law; .bps is expected slippage.

deterministic · replay: pure

quantile

quantile(values: List<Float>, q: Float) -> Float

q-th quantile with linear interpolation between the two nearest sorted values (numpy's default): quantile(xs, 0.5) == median(xs).

deterministic · replay: pure

var_historical

var_historical(returns: List<Float>, opts?: {alpha, max_obs}) -> Float

Historical 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.

deterministic · replay: pure

expected_shortfall_historical

expected_shortfall_historical(returns: List<Float>, opts?: {alpha, max_obs}) -> Float

Historical 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.

deterministic · replay: pure

var_gaussian

var_gaussian(returns: List<Float>, opts?: {alpha, mu, sigma, max_obs}) -> Float

Gaussian VaR assuming N(mu, sigma^2); moments inferred unless overridden.

deterministic · replay: pure