From 18f28602e70c3f40d6c94b4ebc26ebef9b0b18cc Mon Sep 17 00:00:00 2001 From: John Donnellan Date: Fri, 6 Mar 2015 16:05:05 +0000 Subject: [PATCH] Add naive F# solution --- .../naive-fsharp/App.config | 6 ++ .../naive-fsharp/Naive.fsproj | 76 +++++++++++++++++++ .../naive-fsharp/Program.fs | 62 +++++++++++++++ .../naive-fsharp/readme.md | 9 +++ 4 files changed, 153 insertions(+) create mode 100644 01-Simple-Linear-Regression/naive-fsharp/App.config create mode 100644 01-Simple-Linear-Regression/naive-fsharp/Naive.fsproj create mode 100644 01-Simple-Linear-Regression/naive-fsharp/Program.fs create mode 100644 01-Simple-Linear-Regression/naive-fsharp/readme.md diff --git a/01-Simple-Linear-Regression/naive-fsharp/App.config b/01-Simple-Linear-Regression/naive-fsharp/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/01-Simple-Linear-Regression/naive-fsharp/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/01-Simple-Linear-Regression/naive-fsharp/Naive.fsproj b/01-Simple-Linear-Regression/naive-fsharp/Naive.fsproj new file mode 100644 index 0000000..34693ce --- /dev/null +++ b/01-Simple-Linear-Regression/naive-fsharp/Naive.fsproj @@ -0,0 +1,76 @@ + + + + + Debug + AnyCPU + 2.0 + 584f6b10-ee0f-4bc1-8894-278a80d6818d + Exe + ConsoleApplication1 + Naive + v4.5 + true + 4.3.1.0 + ConsoleApplication1 + + + true + full + false + false + bin\Debug\ + DEBUG;TRACE + 3 + AnyCPU + bin\Debug\ConsoleApplication1.XML + true + + + pdbonly + true + true + bin\Release\ + TRACE + 3 + AnyCPU + bin\Release\ConsoleApplication1.XML + true + + + + + True + + + + + + + + + + + 11 + + + + + $(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets + + + + + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets + + + + + + \ No newline at end of file diff --git a/01-Simple-Linear-Regression/naive-fsharp/Program.fs b/01-Simple-Linear-Regression/naive-fsharp/Program.fs new file mode 100644 index 0000000..4e433b1 --- /dev/null +++ b/01-Simple-Linear-Regression/naive-fsharp/Program.fs @@ -0,0 +1,62 @@ +open System +open System.IO + +type point = {x:float; y:float} + +let costOfSequence data a c = + let costOfPoint point = + (point.y - ( a * point.x + c)) ** 2.0 + data |> Array.sumBy costOfPoint + +let rec climbHill func x y step = seq{ + yield (x, y, step) + + let best = + [(x+step, y); (x, y+step); (x-step, y); (x, y-step)] + |> List.map (fun (x, y) -> (func x y, x, y)) + |> List.max + + yield! match best with + | (value', x', y') when value' > func x y -> climbHill func x' y' step + | (value', _, _) -> climbHill func x y (step/10.0) +} + +let guess (data: point array) = + let step = List.max <| [for d in data do yield abs d.y] + let first = data.[0] + let last = data.[data.Length - 1] + if first.y = last.y then + (0.0, first.y, step) + else + let a = (last.y - first.y)/(last.x - first.x) + let c = first.y - (first.x * a) + (a, c, step) + + +[] +let main argv = + Environment.CurrentDirectory <- __SOURCE_DIRECTORY__ + + let readFloats file = + File.ReadAllLines file + |> Array.map float + + let sampleData = + Array.zip (readFloats @"..\data\ex2x.dat") (readFloats @"..\data\ex2y.dat") + |> Array.map(fun (a, h) -> {x=a; y = h}) + + let minStep = 0.000000001 + let aGuess, cGuess, step = guess sampleData + printfn "Starting with: a=%f c=%f step=%f" aGuess cGuess step + let quality a c = -(costOfSequence sampleData a c) + let trail = climbHill quality aGuess cGuess step + + let a, c, _ = trail + |> Seq.where (fun (_, _, step) -> abs step < minStep) + |> Seq.head + + printfn "" + printfn "Answer: a=%0.8f c=%0.8f" a c + + Console.ReadKey() |> ignore + 0 \ No newline at end of file diff --git a/01-Simple-Linear-Regression/naive-fsharp/readme.md b/01-Simple-Linear-Regression/naive-fsharp/readme.md new file mode 100644 index 0000000..62484fe --- /dev/null +++ b/01-Simple-Linear-Regression/naive-fsharp/readme.md @@ -0,0 +1,9 @@ +F# Naive +======== + +A naive linear regression implementation using F# started at the +(meetup)[http://www.meetup.com/Cambridge-Programmers-Study-Group/events/220440417/] +by Alessandro and me. I think it's first F# progrom +by either of us. + +John \ No newline at end of file