Skip to content

Commit 751e424

Browse files
committed
Merge pull request #2 from it-depends/master
Add naive F# solution
2 parents 9335cc3 + 18f2860 commit 751e424

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>584f6b10-ee0f-4bc1-8894-278a80d6818d</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<RootNamespace>ConsoleApplication1</RootNamespace>
11+
<AssemblyName>Naive</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<TargetFSharpCoreVersion>4.3.1.0</TargetFSharpCoreVersion>
15+
<Name>ConsoleApplication1</Name>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<Tailcalls>false</Tailcalls>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<WarningLevel>3</WarningLevel>
25+
<PlatformTarget>AnyCPU</PlatformTarget>
26+
<DocumentationFile>bin\Debug\ConsoleApplication1.XML</DocumentationFile>
27+
<Prefer32Bit>true</Prefer32Bit>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<Tailcalls>true</Tailcalls>
33+
<OutputPath>bin\Release\</OutputPath>
34+
<DefineConstants>TRACE</DefineConstants>
35+
<WarningLevel>3</WarningLevel>
36+
<PlatformTarget>AnyCPU</PlatformTarget>
37+
<DocumentationFile>bin\Release\ConsoleApplication1.XML</DocumentationFile>
38+
<Prefer32Bit>true</Prefer32Bit>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="mscorlib" />
42+
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
43+
<Private>True</Private>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Core" />
47+
<Reference Include="System.Numerics" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="Program.fs" />
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<PropertyGroup>
54+
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
55+
</PropertyGroup>
56+
<Choose>
57+
<When Condition="'$(VisualStudioVersion)' == '11.0'">
58+
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
59+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
60+
</PropertyGroup>
61+
</When>
62+
<Otherwise>
63+
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
64+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
65+
</PropertyGroup>
66+
</Otherwise>
67+
</Choose>
68+
<Import Project="$(FSharpTargetsPath)" />
69+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
70+
Other similar extension points exist, see Microsoft.Common.targets.
71+
<Target Name="BeforeBuild">
72+
</Target>
73+
<Target Name="AfterBuild">
74+
</Target>
75+
-->
76+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
open System
2+
open System.IO
3+
4+
type point = {x:float; y:float}
5+
6+
let costOfSequence data a c =
7+
let costOfPoint point =
8+
(point.y - ( a * point.x + c)) ** 2.0
9+
data |> Array.sumBy costOfPoint
10+
11+
let rec climbHill func x y step = seq{
12+
yield (x, y, step)
13+
14+
let best =
15+
[(x+step, y); (x, y+step); (x-step, y); (x, y-step)]
16+
|> List.map (fun (x, y) -> (func x y, x, y))
17+
|> List.max
18+
19+
yield! match best with
20+
| (value', x', y') when value' > func x y -> climbHill func x' y' step
21+
| (value', _, _) -> climbHill func x y (step/10.0)
22+
}
23+
24+
let guess (data: point array) =
25+
let step = List.max <| [for d in data do yield abs d.y]
26+
let first = data.[0]
27+
let last = data.[data.Length - 1]
28+
if first.y = last.y then
29+
(0.0, first.y, step)
30+
else
31+
let a = (last.y - first.y)/(last.x - first.x)
32+
let c = first.y - (first.x * a)
33+
(a, c, step)
34+
35+
36+
[<EntryPoint>]
37+
let main argv =
38+
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
39+
40+
let readFloats file =
41+
File.ReadAllLines file
42+
|> Array.map float
43+
44+
let sampleData =
45+
Array.zip (readFloats @"..\data\ex2x.dat") (readFloats @"..\data\ex2y.dat")
46+
|> Array.map(fun (a, h) -> {x=a; y = h})
47+
48+
let minStep = 0.000000001
49+
let aGuess, cGuess, step = guess sampleData
50+
printfn "Starting with: a=%f c=%f step=%f" aGuess cGuess step
51+
let quality a c = -(costOfSequence sampleData a c)
52+
let trail = climbHill quality aGuess cGuess step
53+
54+
let a, c, _ = trail
55+
|> Seq.where (fun (_, _, step) -> abs step < minStep)
56+
|> Seq.head
57+
58+
printfn ""
59+
printfn "Answer: a=%0.8f c=%0.8f" a c
60+
61+
Console.ReadKey() |> ignore
62+
0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
F# Naive
2+
========
3+
4+
A naive linear regression implementation using F# started at the
5+
(meetup)[http://www.meetup.com/Cambridge-Programmers-Study-Group/events/220440417/]
6+
by Alessandro and me. I think it's first F# progrom
7+
by either of us.
8+
9+
John

0 commit comments

Comments
 (0)