fork download
  1. ; linear regression
  2.  
  3. (define (regression xs ys)
  4. (let loop ((xs xs) (ys ys) (n 0)
  5. (x 0) (y 0) (xy 0) (xx 0))
  6. (if (pair? xs)
  7. (loop (cdr xs) (cdr ys) (+ n 1)
  8. (+ x (car xs)) (+ y (car ys))
  9. (+ xy (* (car xs) (car ys)))
  10. (+ xx (* (car xs) (car xs))))
  11. (let ((m (/ (- (* n xy) (* x y))
  12. (- (* n xx) (* x x)))))
  13. (list m (/ (- y (* m x)) n))))))
  14.  
  15. (display (regression '(60 61 62 63 65) '(3.1 3.6 3.8 4 4.1))) (newline)
  16.  
  17. (display (- (* 0.1878 64) 7.9635)) (newline)
Success #stdin #stdout 0.03s 8656KB
stdin
Standard input is empty
stdout
(0.18783783783783292 -7.963513513513208)
4.0557