;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: learning/algorithms/linear-regression.lisp

;;;; Linear Regression (Section 19.6)

(defun univariate-linear-regression (xs ys)
  "The weights w0 and w1 of the line y = w1 x + w0 that minimizes the
  squared error on the points (XS, YS), in closed form (Equation 19.3)."
  (let* ((n (length xs))
         (sx (sum xs))
         (sy (sum ys))
         (sxy (sum (mapcar #'* xs ys)))
         (sxx (sum xs #'square))
         (w1 (/ (- (* n sxy) (* sx sy)) (- (* n sxx) (square sx))))
         (w0 (/ (- sy (* w1 sx)) n)))
    (values w0 w1)))

;;; Multivariable linear regression by batch gradient descent: the
;;; hypothesis is the list of weights (w0 w1 ... wn), for the dummy input
;;; 1 and the attributes of the problem, all real-valued, as is the goal.

(defun linear-inputs (e attributes)
  (cons 1 (mapcar #'(lambda (a) (attribute-value a e)) attributes)))

(defun linear-regression-learning (problem &key (alpha 0.01) (epochs 1000))
  "Fit the weights by gradient descent on the squared loss: at each epoch,
  w_i <- w_i + alpha sum_j (y_j - h_w(x_j)) x_j,i (Equation 19.6)."
  (let* ((attributes (learning-problem-attributes problem))
         (goal (first (learning-problem-goals problem)))
         (data (mapcar #'(lambda (e) (cons (attribute-value goal e)
                                            (linear-inputs e attributes)))
                       (learning-problem-examples problem)))
         (w (make-list (+ 1 (length attributes)) :initial-element 0)))
    (dotimes (epoch epochs w)
      (let ((gradient (make-list (length w) :initial-element 0)))
        (dolist (example data)
          (let ((err (- (car example) (dot-product w (cdr example)))))
            (setf gradient (mapcar #'(lambda (g x) (+ g (* err x)))
                                   gradient (cdr example)))))
        (setf w (mapcar #'(lambda (wi g) (+ wi (* alpha g))) w gradient))))))

(defun linear-predict (w e attributes)
  "The value predicted by the weights W for the example E."
  (dot-product w (linear-inputs e attributes)))

;;;; Logistic Regression (Section 19.6.5)

;;; The hypothesis is again a list of weights, for the logistic function
;;; h_w(x) = 1 / (1 + e^(-w.x)) of the attributes, real-valued; the goal is
;;; 0 or 1, and h_w(x) is the probability that it is 1.

(defun logistic-regression-learning (problem &key (alpha 0.1) (epochs 1000))
  "Fit the weights by gradient descent on the squared loss: at each epoch,
  w_i <- w_i + alpha sum_j (y_j - h_w(x_j)) h_w(x_j) (1 - h_w(x_j)) x_j,i
  (Equation 19.8)."
  (let* ((attributes (learning-problem-attributes problem))
         (goal (first (learning-problem-goals problem)))
         (data (mapcar #'(lambda (e) (cons (attribute-value goal e)
                                            (linear-inputs e attributes)))
                       (learning-problem-examples problem)))
         (w (make-list (+ 1 (length attributes)) :initial-element 0)))
    (dotimes (epoch epochs w)
      (let ((gradient (make-list (length w) :initial-element 0)))
        (dolist (example data)
          (let* ((h (sigmoid (dot-product w (cdr example))))
                 (delta (* (- (car example) h) h (- 1 h))))
            (setf gradient (mapcar #'(lambda (g x) (+ g (* delta x)))
                                   gradient (cdr example)))))
        (setf w (mapcar #'(lambda (wi g) (+ wi (* alpha g))) w gradient))))))

(defun logistic-predict (w e attributes)
  "The probability, under the weights W, that the goal of E is 1."
  (sigmoid (dot-product w (linear-inputs e attributes))))
