Documentation

Logistic Regression

@statili/stats ·v0.0.2-beta.0 ·1 export

signatures
logistic(arg1: Partial<LogisticRegressionOptions>): (args_0: MultiDataPoint[]) => MultiRegressionResult
logistic(suppliedOptions: Partial<LogisticRegressionOptions>, data: MultiDataPoint[]): MultiRegressionResult
Performs binary logistic regression via batch gradient descent.

Models the probability that an observation belongs to class 1: P(y=1 | x) = σ(b₀ + b₁x₁ + b₂x₂ + … + bₖxₖ) where σ is the sigmoid function. Predictions at P ≥ 0.5 are classified as 1.

y must be binary: each observation’s y value must be exactly 0 or 1.

Parameters

NameTypeDescription
suppliedOptions Partial<LogisticRegressionOptions> Optional { learningRate?, iterations?, precision? }.
data MultiDataPoint[] Array of { x: number[], y: 0 | 1 | null } observations.

Returns

MultiRegressionResult

Discriminant union:

  • ok: true — includes coefficients ([b₀, …, bₖ]), r2 (McFadden pseudo-R²), accuracy (proportion correctly classified), n, predict.
  • ok: false — includes errorType and message.
insight
  • Classification qualityaccuracy (proportion of training observations correctly classified at the 0.5 threshold).
  • Model fitr2 (McFadden pseudo-R²): values ≥ 0.2 indicate good fit, ≥ 0.4 indicate excellent fit (scale differs from ordinary R²).
  • Feature direction — positive coefficient bᵢ → feature i increases P(y=1); negative → it decreases P(y=1).
  • Decision boundary — the boundary where P(y=1) = 0.5 is where the linear predictor equals 0: b₀ + b₁x₁ + … = 0.
  • Odds interpretation — e^bᵢ is the odds multiplier per unit increase in xᵢ.

Examples

// Classify pass (1) / fail (0) from study hours and practice problems
const data: MultiDataPoint[] = [
  { x: [1, 5],  y: 0 }, { x: [2, 10], y: 0 }, { x: [3, 15], y: 0 },
  { x: [4, 20], y: 1 }, { x: [5, 25], y: 1 }, { x: [6, 30], y: 1 },
];
const result = logistic({}, data);
if (result.ok) {
  console.log(result.accuracy);               // e.g. 0.9167 (91.67%)
  console.log(result.predict([4.5, 22]).y);   // probability ≈ 0.72
}
// Curried / partial application (data-last)
const fitLogistic = logistic({ learningRate: 0.05, iterations: 2000 });
const result = fitLogistic(data);