-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression.ts
37 lines (34 loc) · 957 Bytes
/
regression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** Mean Absolute Error */
export function mae(y: ArrayLike<number>, y1: ArrayLike<number>): number {
let err = 0;
for (let i = 0; i < y.length; i += 1) {
err += (y[i] - y1[i]) ** 2;
}
return err / y.length;
}
/** Mean Square Error */
export function mse(y: ArrayLike<number>, y1: ArrayLike<number>): number {
let err = 0;
for (let i = 0; i < y.length; i += 1) {
err += (y[i] - y1[i]) ** 2;
}
return err / y.length;
}
/** Root Mean Square Error */
export function rmse(y: ArrayLike<number>, y1: ArrayLike<number>): number {
return Math.sqrt(mse(y, y1));
}
/** R2 Score for regression */
export function r2(y: ArrayLike<number>, y1: ArrayLike<number>): number {
let mean = 0;
for (let i = 0; i < y.length; i += 1) {
mean += y[i];
}
mean /= y.length;
let ssr = 0, sst = 0;
for (let i = 0; i < y.length; i += 1) {
ssr += Math.pow(y1[i] - mean, 2);
sst += Math.pow(y[i] - mean, 2);
}
return ssr / sst;
}