Skip to content

Instantly share code, notes, and snippets.

@55v
Created October 14, 2011 05:01
Show Gist options
  • Save 55v/1286293 to your computer and use it in GitHub Desktop.
Save 55v/1286293 to your computer and use it in GitHub Desktop.
ML linear regression cost
// Learn more about F# at http://fsharp.net
#light
let T0 = 0.0
let T1 = 1.0
let m = 4.0
let X = [| 3.0; 1.0; 0.0; 4.0 |]
let Y = [| 2.0; 2.0; 1.0; 3.0 |]
//let XY = Array.zip(x y)
let h x = T0 + T1 * x
Array.iter(fun xi -> printfn "%f" (h xi)) X
printfn ""
let J arrX arrY = (1.0/(2.0 * m)) * Array.fold2(fun acc elemX elemY -> acc + (h elemX - elemY) ** 2.0) 0.0 arrX arrY
let result = J X Y
printf "%f" result
System.Console.ReadKey() |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment