GizStack
View .mdOpen in ChatGPTOpen in Claude

gizai:ml/linear

linear models.

gizlang
import * as Linear from "gizai:ml/linear";

Types

Sgd

gizlang
type Sgd = { loss: string; rate: string; alpha: f64; l2: bool; eta0: f64; power: f64; cost: f64; epsilon: f64; maxIter: i64; tol: f64; patience: i64; }

Stochastic gradient descent as scikit-learn's _plain_sgd. loss: squared_error, hinge, perceptron, epsilon_insensitive; rate: optimal, invscaling, constant, pa1 (passive-aggressive). y is ±1 for classification.

  • loss: string
  • rate: string
  • alpha: f64
  • l2: bool
  • eta0: f64
  • power: f64
  • cost: f64
  • epsilon: f64
  • maxIter: i64
  • tol: f64
  • patience: i64

SgdState

gizlang
type SgdState = { w: Vec<f64>; b: f64; scale: f64; t: f64; init: f64; }

One SGD model as it learns: the weights (times scale), the intercept, the step count and the start of the optimal rate.

  • w: Vec<f64>
  • b: f64
  • scale: f64
  • t: f64
  • init: f64

Functions

ridge

gizlang
function ridge(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, t: i64, alpha: f64, center: bool): Vec<f64>

Least squares (ridge when alpha > 0) of t targets: the minimum-norm solution, as LAPACK's lstsq gives it.

Effects: fail.

enetFit

gizlang
function enetFit(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, t: i64, alpha: f64, l1: f64, maxIter: i64, tol: f64): Vec<f64>

Elastic net coefficients (d by t, row-major) for the t targets of the centered data, one coordinate descent per target.

Effects: fail.

omp

gizlang
function omp(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, t: i64, atoms: i64): Vec<f64>

Orthogonal matching pursuit: the atom most correlated with the residual, then least squares on the chosen atoms.

Effects: fail.

bayesianRidge

gizlang
function bayesianRidge(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, maxIter: i64, tol: f64, a1: f64, a2: f64, l1: f64, l2: f64): Vec<f64>

Bayesian ridge regression (scikit-learn's evidence maximization in the eigenbasis of XᵀX).

Effects: fail.

ard

gizlang
function ard(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, maxIter: i64, tol: f64, threshold: f64, a1: f64, a2: f64, l1: f64, l2: f64): Vec<f64>

Automatic relevance determination (scikit-learn's ARDRegression).

Effects: fail.

sgdState

gizlang
function sgdState(s: Sgd, d: i64): SgdState

The starting state of SGD model s over d features: zero weights and its first learning rate.

Effects: fail.

sgdPass

gizlang
function sgdPass(s: Sgd, st: mut SgdState, x: Vec<f64>, d: i64, y: Vec<f64>, order: Vec<i64>): f64

One pass of SGD over rows order of x: the sum of their losses.

Effects: fail.

sgdWeights

gizlang
function sgdWeights(st: SgdState, d: i64): Vec<f64>

The d weights of state st, scaled, followed by its intercept.

Effects: fail.

sgd

gizlang
function sgd(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, s: Sgd, rng: mut Random): Vec<f64>

The weights then intercept that SGD model s learns from the n by d rows x and targets y, in rng's order.

Effects: fail.

ransac

gizlang
function ransac(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, t: i64, trials0: i64, rng: mut Random): Vec<f64> throws string

RANSAC around least squares: the consensus of the most inliers within the median absolute deviation of y.

Effects: fail.

theilSen

gizlang
function theilSen(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, population: i64, maxIter: i64, tol: f64, rng: mut Random): Vec<f64>

Theil-Sen: the spatial median (modified Weiszfeld) of the exact fits of subsets of d + 1 rows.

Effects: fail.