Skip to content

Commit 8d1c7ae

Browse files
[Autoloop: perf-comparison] Iteration 390: add OLS multiple regression benchmark pair
Run: https://github.com/githubnext/tsb/actions/runs/29003458535 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1db8c9c commit 8d1c7ae

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

benchmarks/pandas/bench_ols.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Benchmark: OLS (Ordinary Least Squares) multiple regression on 10k rows x 5 predictors"""
2+
import json, time
3+
import numpy as np
4+
5+
ROWS = 10_000
6+
WARMUP = 5
7+
ITERATIONS = 20
8+
9+
rng = np.random.default_rng(42)
10+
X = rng.uniform(-1, 1, size=(ROWS, 5))
11+
# y = 1*x1 + 2*x2 - 0.5*x3 + 3*x4 + 0.1*x5 + noise
12+
coefs = np.array([1.0, 2.0, -0.5, 3.0, 0.1])
13+
y = X @ coefs + rng.normal(0, 0.05, size=ROWS)
14+
15+
# Add intercept column (matching tsb OLS default addIntercept=true)
16+
X_design = np.column_stack([X, np.ones(ROWS)])
17+
18+
for _ in range(WARMUP):
19+
np.linalg.lstsq(X_design, y, rcond=None)
20+
21+
start = time.perf_counter()
22+
for _ in range(ITERATIONS):
23+
np.linalg.lstsq(X_design, y, rcond=None)
24+
total = (time.perf_counter() - start) * 1000
25+
26+
print(json.dumps({
27+
"function": "ols",
28+
"mean_ms": total / ITERATIONS,
29+
"iterations": ITERATIONS,
30+
"total_ms": total,
31+
}))

benchmarks/tsb/bench_ols.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Benchmark: OLS (Ordinary Least Squares) multiple regression on 10k rows × 5 predictors
3+
*/
4+
import { OLS } from "../../src/index.js";
5+
6+
const ROWS = 10_000;
7+
const WARMUP = 5;
8+
const ITERATIONS = 20;
9+
10+
// Reproducible design matrix: 5 predictors with known coefficients
11+
const rng = (seed: number) => {
12+
let s = seed;
13+
return () => {
14+
s = (s * 1664525 + 1013904223) & 0xffffffff;
15+
return (s >>> 0) / 4294967296;
16+
};
17+
};
18+
const rand = rng(42);
19+
20+
const X: number[][] = Array.from({ length: ROWS }, () =>
21+
Array.from({ length: 5 }, () => rand() * 2 - 1),
22+
);
23+
// y = 1*x1 + 2*x2 - 0.5*x3 + 3*x4 + 0.1*x5 + noise
24+
const y: number[] = X.map((row) => {
25+
const [x1, x2, x3, x4, x5] = row as [number, number, number, number, number];
26+
return x1 + 2 * x2 - 0.5 * x3 + 3 * x4 + 0.1 * x5 + (rand() - 0.5) * 0.1;
27+
});
28+
29+
const model = new OLS();
30+
31+
for (let i = 0; i < WARMUP; i++) {
32+
model.fit(X, y);
33+
}
34+
35+
const start = performance.now();
36+
for (let i = 0; i < ITERATIONS; i++) {
37+
model.fit(X, y);
38+
}
39+
const total = performance.now() - start;
40+
41+
console.log(
42+
JSON.stringify({
43+
function: "ols",
44+
mean_ms: total / ITERATIONS,
45+
iterations: ITERATIONS,
46+
total_ms: total,
47+
}),
48+
);

0 commit comments

Comments
 (0)