Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add regularized linear regression

  • Loading branch information...
commit 8f1dedb75336b8a873ac8a563cfce473f271ce59 1 parent cca8e9b
@batterseapower authored
Showing with 13 additions and 1 deletion.
  1. +13 −1 Algorithms/MachineLearning/LinearRegression.hs
View
14 Algorithms/MachineLearning/LinearRegression.hs
@@ -2,7 +2,7 @@
-- | Linear regression models, as discussed in chapter 3 of Bishop.
module Algorithms.MachineLearning.LinearRegression (
- LinearModel, regressLinearModel
+ LinearModel, regressLinearModel, regressRegularizedLinearModel
) where
import Algorithms.MachineLearning.Framework
@@ -34,6 +34,18 @@ instance Model LinearModel where
regressLinearModel :: (Vectorable input) => [input -> Target] -> DataSet input -> LinearModel input
regressLinearModel = regressLinearModelCore pinv
+-- | Regress a basic linear model with a sum-of-squares regularization term. This penalizes models with weight
+-- vectors of large magnitudes and hence ameliorates the over-fitting problem of 'regressLinearModel'.
+-- The strength of the regularization is controlled by the lambda parameter.
+--
+-- The resulting model will be optimal in terms of least-squares penalized by lambda times the sum-of-squares of
+-- the weight vector. Like 'regressLinearModel', a closed form solution is used to find the model quickly.
+regressRegularizedLinearModel :: (Vectorable input) => RegularizationCoefficient -> [input -> Target] -> DataSet input -> LinearModel input
+regressRegularizedLinearModel lambda = regressLinearModelCore regularizedPinv
+ where
+ regularizedPinv phi = let trans_phi = trans phi
+ in inv ((lambda .* (ident (cols phi))) + (trans_phi <> phi)) <> trans_phi
+
regressLinearModelCore :: (Vectorable input) => (Matrix Double -> Matrix Double) -> [input -> Target] -> DataSet input -> LinearModel input
regressLinearModelCore find_pinv basis_fns ds
= LinearModel { lm_basis_fns = basis_fns, lm_weights = weights }
Please sign in to comment.
Something went wrong with that request. Please try again.