Linear Regression
June 10, 2016
Here’s our program:
(define (regression xs ys)
(let loop ((xs xs) (ys ys) (n 0)
(x 0) (y 0) (xy 0) (xx 0))
(if (pair? xs)
(loop (cdr xs) (cdr ys) (+ n 1)
(+ x (car xs)) (+ y (car ys))
(+ xy (* (car xs) (car ys)))
(+ xx (* (car xs) (car xs))))
(let ((m (/ (- (* n xy) (* x y))
(- (* n xx) (* x x)))))
(list m (/ (- y (* m x)) n))))))
And here’s the program run against the sample data:
> (regression '(60 61 62 63 65) '(3.1 3.6 3.8 4 4.1)) (0.18783783783783292 -7.963513513513208) > (- (* 0.1878 64) 7.9635) 4.0557
You can run the program at http://ideone.com/9QJGUQ.
Here’s a solution in matlab. The same approach can be used for multiple regression, but X would have additional columns for the additional variables.
Output:
w = -7.9635 0.1878 y_hat = 4.0581Shouldn’t the calculation for b be:
b = (Σy / n – m × Σx / n)
Fixed. Thank you.
Thanks for your website!
Doing it in APL felt like cheating.
∇X LINREG Y
M←(((⍴X)×+/X×Y)-((+/X)×+/Y))÷((⍴X)×+/X*2)-(+/X)*2
B←((+/Y)-M×+/X)÷⍴X
∆