Linear regression
Least squares has a closed form, so with rational data the coefficients, fitted values, residuals and R² come out as exact fractions. Only the inference on top of them (p-values, interval ends) needs the certified distributions.
A line through the data
// hours of study and exam score for ten students
const hours = [1, 2, 2, 3, 4, 5, 5, 6, 7, 8];
const score = [52, 55, 61, 60, 68, 70, 74, 75, 83, 86];
import { linearModel, show } from "@ratmath/stats";
const m = linearModel(hours, score);
String(m); // "y = 22847/481 + 2338/481·x (n = 10, R² = 1366561/1412216)"
m.coefficients; // [22847/481, 2338/481]: about 47.50 and 4.861
m.variance; // 9131/1924: s², the residual variance on n − p = 8 degrees of freedom
m.table(); // estimate, standard error, t and p-value for each coefficient
m.interval("x"); // slope between 4.1364 and 5.5850 (95%)
m.fTest(); // F test of the whole regression
With a design matrix (a column of ones for the intercept, then one column per predictor) and responses , least squares minimises . The minimiser solves the normal equations
The package forms exactly and inverts it by Gauss–Jordan elimination over the rationals, so , the fitted values and the residuals are exact. So are the sums of squares , , and the residual variance . If one column is a linear combination of the others is singular; the fit refuses with code "collinear" and names the column that breaks the rank.
Inference on the coefficients
Under the usual model, with independent errors, is normal with covariance . Replacing by gives, for each coefficient,
As with the t tests, is exact and the p-value comes from at that exact argument. The overall F test compares the model with the intercept alone: , an exact fraction.
Predicting
m.predict(6); // 36875/481 ≈ 76.66
m.predictionInterval(6); // the mean score at 6 hours: [74.6532, 78.6732]
m.predictionInterval(6, { kind: "new" }); // one new student: [71.2524, 82.0740]
At a new point (with its leading 1), the fitted mean has standard error with . A single new observation adds its own noise, so its interval uses and is always wider: it never shrinks to zero however much data there is.
Leverage and influence
m.leverage; // the diagonal of the hat matrix, exact: 157/481, 101/481, …, 5/13
m.standardizedResiduals();// eᵢ / (s√(1 − hᵢ))
m.cooksDistance; // the third student (2 hours, 61 points) has the most influence: 0.506
The hat matrix maps to ; its diagonal entries , the leverages, lie in , sum to , and measure how far point sits from the others in predictor space. Cook's distance
measures how far all the fitted values move when point is left out; the test suite checks this formula against refitting without each point. Both are exact here.
Several predictors, and curves
const house = linearModel({ size: [50, 70, 80, 100, 120, 60, 90, 110], rooms: [2, 3, 3, 4, 4, 2, 3, 5] },
[150, 200, 230, 270, 330, 165, 250, 300]);
String(house); // "y = 8405/472 + 1231/472·size − 95/118·rooms (n = 8, R² = 526772/529525)"
house.predict({ size: 85, rooms: 3 }); // 27975/118
// rooms adds nothing once size is known: p = 0.8667
const q2 = polyFit([0, 1, 2, 3, 4, 5, 6], [1.2, 1.9, 4.8, 9.6, 16.5, 25.4, 36.1], 2);
String(q2); // "y = 241/210 − 5/28·x + 421/420·x^2 (…)"
compareModels(q2, polyFit([0, 1, 2, 3, 4, 5, 6], [1.2, 1.9, 4.8, 9.6, 16.5, 25.4, 36.1], 3));
// F = 7.56, p = 0.070769: the cubic term is not needed at 5%
A polynomial fit is linear regression on the columns . Two nested models, the smaller one's columns a subset of the larger one's, are compared by
which has the distribution when the extra terms are zero. compareModels checks the nesting exactly before it computes anything.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.