Skip to main content
kstats-correlation covers two related tasks: measuring the strength of association between variables, and modeling a linear relationship. The module is split into two sections reflecting this distinction.

Correlation

Pearson Correlation

Measures the strength and direction of the linear association between two numeric variables. The coefficient ranges from -1 (perfect negative) to +1 (perfect positive).
Use Pearson when both variables are continuous and the relationship is approximately linear. Sensitive to outliers.

Spearman Correlation

Applies Pearson correlation to the ranks of the data. Measures monotonic association — whether the variables tend to increase together, regardless of linearity.
Use Spearman when the relationship is monotonic but not necessarily linear, or when the data contains outliers.

Kendall Tau

Counts concordant and discordant pairs to measure ordinal association. More robust than Spearman for small samples and heavy ties.
The Kendall tau implementation runs in O(nlogn)O(n \log n) time using a merge-sort–based algorithm, making it efficient for large datasets.

Point-Biserial Correlation

Measures the association between a binary variable (coded as 0/1 integers) and a continuous variable. Equivalent to Pearson correlation when one variable is dichotomous.
Use when one variable is naturally binary: treatment/control, pass/fail, male/female.

Partial Correlation

Measures the association between two variables after controlling for a third variable. Removes the effect of the confounding variable.
Use when a third variable might explain the apparent relationship between the first two.

Correlation and Covariance Matrices

For multi-variable analysis, build pairwise matrices. Each cell (i,j)(i, j) contains the correlation (or covariance) between variables ii and jj.

Choosing a Correlation Method

Pearson:r=i(xixˉ)(yiyˉ)i(xixˉ)2i(yiyˉ)2r = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_i (x_i - \bar{x})^2 \sum_i (y_i - \bar{y})^2}}Spearman: Pearson correlation applied to ranks.Kendall tau-b:τb=CD(C+D+TX)(C+D+TY)\tau_b = \frac{C - D}{\sqrt{(C + D + T_X)(C + D + T_Y)}}where CC = concordant pairs, DD = discordant pairs, TXT_X = pairs tied only on X, TYT_Y = pairs tied only on Y.Partial correlation:rxyz=rxyrxzryz(1rxz2)(1ryz2)r_{xy \cdot z} = \frac{r_{xy} - r_{xz} \cdot r_{yz}}{\sqrt{(1 - r_{xz}^2)(1 - r_{yz}^2)}}

Regression

Simple Linear Regression

Fits the line y^=β0+β1x\hat{y} = \beta_0 + \beta_1 x to the data using ordinary least squares. The result includes the slope, intercept, goodness-of-fit (R2R^2), standard errors, residuals, and a prediction function.
β^1=i(xixˉ)(yiyˉ)i(xixˉ)2,β^0=yˉβ^1xˉ\hat{\beta}_1 = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sum_i (x_i - \bar{x})^2}, \qquad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}R2=1i(yiy^i)2i(yiyˉ)2R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}

API Reference

Full API Reference

Browse all correlation functions, result types, and parameter overloads in the Dokka-generated reference.
Last modified on April 18, 2026