Last active
April 16, 2016 11:43
-
-
Save gvergnaud/86d131f8724d093ffc2fa8bf39772121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ----------------------------------------- * | |
Learning Machin Learning | |
* ----------------------------------------- */ | |
// gradientDescent for the square error function and a Theta of Two parameter (a, b) | |
// Theta :: [Number, Number] | |
// sum :: (a -> Int) -> List a -> Int | |
const sum = (transformer, list) => | |
list.reduce((acc, item) => acc + transformer(item), 0) | |
// getNextTheta :: Number -> List Data -> Theta | |
const getNextTheta = (step, dataSet) => ([a, b]) => [ | |
a - step / dataSet.length * sum(([x, y]) => a + b * x - y, dataSet), | |
b - step / dataSet.length * sum(([x, y]) => (a + b * x - y) * x, dataSet), | |
] | |
// isConverging :: Theta -> Theta -> Bool | |
const isConverging = ([a1, b1], [a2, b2]) => | |
Math.abs(a1 - a2) < 0.01 && Math.abs(b1 - b2) < 0.01 | |
// repeatUntilConverge :: (Theta -> Theta) -> Theta -> Theta | |
const repeatUntilConverge = (f, theta) => { | |
const nextTheta = f(theta) | |
return isConverging(nextTheta, theta) | |
? nextTheta | |
: repeatUntilConverge(f, nextTheta) | |
} | |
// gradientDescent :: { step :: Number, dataSet :: List Data } -> Theta | |
const gradientDescent = ({ step, dataSet }) => | |
repeatUntilConverge(getNextTheta(step, dataSet), [0, 0]) | |
export default gradientDescent | |
// linearRegression.js | |
import gradientDescent from './gradientDescent' | |
// linearRegression :: List Data -> (Number -> Number) | |
export default function linearRegression(dataSet) { | |
const [a, b] = gradientDescent({ step: .0000001, dataSet }) | |
console.log(a, b) | |
return x => a + b * x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment