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.

Pages: 1 2

5 Responses to “Linear Regression”

  1. Daniel said

    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.

    x = [60;61;62;63;65];
    y = [3.1;3.6;3.8;4.0;4.1];
    
    X = [ones(size(x,1),1), x];
    
    w = X \ y
    
    y_hat = [1,64] * w
    

    Output:

    w =
       -7.9635
        0.1878
    
    y_hat =
        4.0581
    
  2. Jeff said

    Shouldn’t the calculation for b be:

    b = (Σy / n – m × Σx / n)

  3. programmingpraxis said

    Fixed. Thank you.

  4. Jeff said

    Thanks for your website!

  5. davor said

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: