Permalink
Browse files

Improve basis function library

  • Loading branch information...
batterseapower committed Aug 17, 2008
1 parent c612bf3 commit 1961a026edb92562a5c4bf9a014de9705dc6156a
Showing with 11 additions and 2 deletions.
  1. +10 −1 Algorithms/MachineLearning/BasisFunctions.hs
  2. +1 −1 Algorithms/MachineLearning/Tests/Driver.hs
@@ -5,9 +5,18 @@ import Algorithms.MachineLearning.Framework
import Algorithms.MachineLearning.Utilities
+-- | Basis function that is 1 everywhere
+constantBasis :: Double -> Double
+constantBasis = const 1
+
-- | /Unnormalized/ 1D Gaussian, suitable for use as a basis function.
gaussianBasis :: Mean -- ^ Mean of the Gaussian
-> Variance -- ^ Variance of the Gaussian
-> Double -- ^ Point on X axis to sample
-> Double
-gaussianBasis mean variance x = exp (negate $ (square (x - mean)) / (2 * variance))
+gaussianBasis mean variance x = exp (negate $ (square (x - mean)) / (2 * variance))
+
+-- | Family of gaussian basis functions with constant variance and the given means, with
+-- a constant basis function to capture the mean of the target variable.
+gaussianBasisFamily :: [Mean] -> Variance -> [Double -> Double]
+gaussianBasisFamily means variance = constantBasis : map (flip gaussianBasis variance) means
@@ -17,7 +17,7 @@ import System.Random
basisFunctions :: [Double -> Double]
-basisFunctions = const 1 : map (\mean -> gaussianBasis (rationalToDouble mean) 0.04) [-1,-0.9..1]
+basisFunctions = gaussianBasisFamily (map rationalToDouble [-1,-0.9..1]) 0.04
sumOfSquaresError :: [(Double, Double)] -> Double
sumOfSquaresError targetsAndPredictions = sum $ map (abs . uncurry (-)) targetsAndPredictions

0 comments on commit 1961a02

Please sign in to comment.