lizfcm/deprecated-cl/vector,least-squares.lisp

15 lines
488 B
Common Lisp
Raw Permalink Normal View History

2023-10-10 16:44:33 -04:00
(in-package :lizfcm.vector)
(defun least-squares-reg (x y)
(let* ((n (length x))
(sum-y (reduce #'+ y))
(sum-x (reduce #'+ x))
(sum-xy (reduce #'+ (mapcar #'* x y)))
(sum-xsquared (reduce #'+ (mapcar #'* x x)))
(b (/ (- (* sum-y sum-xsquared) (* sum-x sum-xy))
(- (* n sum-xsquared) (* sum-x sum-x))))
(a (/ (- (* n sum-xy) (* sum-x sum-y))
(- (* n sum-xsquared) (* sum-x sum-x)))))
(list a b)))