Book Image

Clojure for Data Science

By : Henry Garner
Book Image

Clojure for Data Science

By: Henry Garner

Overview of this book

Table of Contents (18 chapters)
Clojure for Data Science
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

The normal equation


Now that we've covered the basics of matrix and vector manipulation we're in a position to study the normal equation. This is an equation that uses matrix algebra to calculate the coefficients of our OLS linear regression model:

We read "to find β, multiply the inverse of X transpose X, by X transpose y" where X is the matrix of independent variables (including the intercept term) for our sample and y is a vector containing the dependent variables for our sample. The result β contains the calculated coefficients. This normal equation is relatively easy to derive from the equation of multiple regression, applying the rules of matrix multiplication, but the mathematics is beyond the scope of this book.

We can implement the normal equation with Incanter using only the functions we have just encountered:

(defn normal-equation [x y]
  (let [xtx  (i/mmult (i/trans x) x)
        xtxi (i/solve xtx)
        xty  (i/mmult (i/trans x) y)]
    (i/mmult xtxi xty)))

This normal equation...