GitHub
deepbox/stats

Correlations

Measure the strength and direction of relationships between variables.
pearsonr
pearsonr(x: Tensor, y: Tensor): [number, number]

Pearson correlation coefficient. Measures linear correlation. Returns [r, pvalue]. r=1 perfect positive, r=−1 perfect negative, r=0 no linear relationship.

spearmanr
spearmanr(x: Tensor, y: Tensor): [number, number]

Spearman rank correlation. Measures monotonic relationship. Robust to outliers. Pearson correlation applied to ranks.

kendalltau
kendalltau(x: Tensor, y: Tensor): [number, number]

Kendall's tau rank correlation. Measures ordinal association. Based on concordant and discordant pairs. More robust than Spearman for small samples.

corrcoef
corrcoef(x: Tensor, y?: Tensor): Tensor

Compute the Pearson correlation matrix. Returns 2×2 matrix when y is provided. Diagonal entries are 1.0.

cov
cov(x: Tensor, y?: Tensor, ddof?: number): Tensor

Compute the covariance matrix. Returns 2×2 matrix when y is provided. ddof=1 (default) for sample covariance.

Pearson

r = Cov(X,Y) / (σ_X · σ_Y)

Where:

  • Cov = Covariance
  • σ = Standard deviation

Spearman

ρ = pearsonr(rank(X), rank(Y))

Where:

  • rank = Ordinal ranks of data
correlations.ts
import { pearsonr, spearmanr, kendalltau, corrcoef, cov } from "deepbox/stats";import { tensor } from "deepbox/ndarray";const x = tensor([1, 2, 3, 4, 5]);const y = tensor([2, 4, 5, 4, 5]);const [r, pval] = pearsonr(x, y);   // r ≈ 0.83, pval < 0.05const [rho, p2] = spearmanr(x, y);  // Spearman rank correlationconst [tau, p3] = kendalltau(x, y); // Kendall's tauconst corrMatrix = corrcoef(x, y);  // [[1, r], [r, 1]]const covMatrix = cov(x, y);        // [[Var(x), Cov], [Cov, Var(y)]]

When to Use

  • pearsonr — Linear relationships between continuous variables
  • spearmanr — Monotonic relationships; robust to outliers; ordinal data
  • kendalltau — Small sample sizes; ordinal data; more conservative than Spearman
  • corrcoef — Correlation matrix for pairs of variables
  • cov — Covariance matrix (unscaled correlation)