Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 01-Simple-Linear-Regression/naive-fsharp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
76 changes: 76 additions & 0 deletions 01-Simple-Linear-Regression/naive-fsharp/Naive.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>584f6b10-ee0f-4bc1-8894-278a80d6818d</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApplication1</RootNamespace>
<AssemblyName>Naive</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFSharpCoreVersion>4.3.1.0</TargetFSharpCoreVersion>
<Name>ConsoleApplication1</Name>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Debug\ConsoleApplication1.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Release\ConsoleApplication1.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.fs" />
<None Include="App.config" />
</ItemGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
62 changes: 62 additions & 0 deletions 01-Simple-Linear-Regression/naive-fsharp/Program.fs
Original file line number Diff line number Diff line change
@@ -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)


[<EntryPoint>]
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
9 changes: 9 additions & 0 deletions 01-Simple-Linear-Regression/naive-fsharp/readme.md
Original file line number Diff line number Diff line change
@@ -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