Why is F# appealing in finance?Functional programming fits with much financial work “Programmatic modelling” A typed, efficient scripting language gets you a long way Plays differently
Trang 2What is F# and why is it interesting?
Trang 3F# is…
a productive, supported, interoperable, functional-first programming language that
allows you to write simple code to solve complex problems
Trang 4ematica…
Algorithmic Trading
Trang 5Why is F# appealing in finance?
Functional programming fits with much financial work
“Programmatic modelling”
A typed, efficient scripting language gets you a long way
Plays differently for different roles:
Enable quants to contribute to component development
Enables architects to explore hard problems fluently Enables developers to tackle parallel and async programming
Trang 6Simple Code, Strongly Typed
Trang 7} abstract class RoverCommand : Command { protected Rover Rover { get; private set; }
public RoverCommand(MarsRover rover) { this.Rover = rover;
} } class BreakCommand : RoverCommand { public BreakCommand(Rover rover) : base(rover) { } public override void Execute() { Rover.Rotate(-5.0);
} } class TurnLeftCommand : RoverCommand {
public TurnLeftCommand(Rover rover) : base(rover) {
} public override void Execute() { Rover.Rotate(-5.0);
} }
Simplicity: Functions as Values
Trang 8return new Tuple<U,T>(t.Item2, t.Item1) }
ReadOnlyCollection<Tuple<T,T,T>> Rotations<T>(Tuple<T,T,T> t) {
#
Trang 9The Big Trends
Trang 10Async.Parallel [ httpAsync "www.google.com"; httpAsync "www.bing.com";
httpAsync "www.yahoo.com"; ]
|> Async.RunSynchronously
Parallel I/O
Trang 11Async.Parallel [ for i in 0 200 -> computeTask i ]
|> Async.RunSynchronously
Parallel CPU
Trang 12F# 2.0 ships with Visual Studio 2010
Trang 13Demo
Trang 14F# can be used for everything,
Trang 15Benchmark Performance by Language
MS Confidential
Trang 16Example #1 (Power Company)
I have written an application to balance the national power generation schedule … for an energy company
the calculation engine was written in F#
The use of F# to address the complexity at the heart of this application clearly demonstrates a sweet spot for the language … algorithmic analysis of large data sets
Simon Cousins (Eon Powergen)
Trang 17Examples #2/#3: Finance companies
Trang 18Example #4: Biotech
F# rocks - building algorithms for DNA processing
and it's like a drug 12-15 at Amyris use F#
F# has been phenomenally useful I would be writing a lot of this in Python otherwise and F# is more robust, 20x
- 100x faster to run and faster to develop
Darren Platt, Amyris BioTechnologies
Trang 19Case Study #5: Microsoft “adPredict”
Trang 20Case Study #5: Microsoft “adPredict”
4 week project, 4 machine learning experts
100million probabilistic variables
Processes 6TB of training data
Real time processing ( N,000 impression updates / sec)
“F# was absolutely integral to our success”
“We delivered a robust, high-performance solution on-time.”
“We couldn’t have achieved this with any other tool given the constraints of the task”
“F# programming is fun – I feel like I learn more about programming every day”
Trang 21Asynchronous & Parallel & Reactive
async { let! res = <async-event>
}
React to a GUI Event React to a Timer Callback React to a Query Response React to a HTTP Response React to a Web Service Response React to a Disk I/O Completion Agent reacts to Message
Trang 22F# async + immutable
Parallel
Server
Agents
Trang 24Units of Measure – Typical Example
[<Measure>] type money
[<Measure>] type shares
Trang 25F# Futures:
Language Integrated Data
demo
Trang 27Type Providers: The Vision
Trang 28le, exp ress
ive , pr od
uctive addi tion
to
.NET
Ready for supported use in VS2010
F# greatly simplifies parallelism
An amaz
ing data-r
ich futur
e ahe ad
F#
In Summary
Trang 29http://meetup.com/FSharpLondon
Jul 4, 6:30pm SkillsMatter
A great place to meet F# users, trainers,
architects, consultants, …
Trang 30Latest Books about F#
Visit www.fsharp.net
Trang 31The world is information rich
Our languages need to be information-rich too
The Type Provider Manifesto?
Consume anything! Directly!
Strongly typed! No walls!
Trang 34F# Today!
F# Today!
Which talk?
Trang 35Some Basics
topic
Trang 36Language Integrated Web Data
demo
Trang 37let request : HttpWebRequest = downcast WebRequest.Create(queryUrl) request.Method <- "GET"
request.ContentType <- "application/x-www-form-urlencoded"
let response = request.GetResponse()
let result = try use reader = new StreamReader(response.GetResponseStream()) reader.ReadToEnd();
finally response.Close()
let data = Encoding.Unicode.GetBytes(result);
let stream = new MemoryStream() stream.Write(data, 0, data.Length);
stream.Position <- 0L
let ser = Json.DataContractJsonSerializer(typeof<Result<'T>>) let result = ser.ReadObject(stream) :?> Result<'T>
if result.Code<>"/api/status/ok" then raise (InvalidOperationException(result.Message)) else
result.Result
let elements = Query<ChemicalElement array>("[{'type':'/chemistry/chemical_element','name':null,'boiling_point':null,'atomic_mass':null}]")
let Query<'T>(query:string) : 'T = let query = query.Replace("'","\"") let queryUrl = sprintf "http://api.freebase.com/api/service/mqlread?query=%s" "{\"query\":"+query+"}"
let request : HttpWebRequest = downcast WebRequest.Create(queryUrl) request.Method <- "GET"
request.ContentType <- "application/x-www-form-urlencoded"
let response = request.GetResponse()
let result = try use reader = new StreamReader(response.GetResponseStream()) reader.ReadToEnd();
finally response.Close()
let data = Encoding.Unicode.GetBytes(result);
let stream = new MemoryStream() stream.Write(data, 0, data.Length);
stream.Position <- 0L
let ser = Json.DataContractJsonSerializer(typeof<Result<'T>>) let result = ser.ReadObject(stream) :?> Result<'T>
if result.Code<>"/api/status/ok" then raise (InvalidOperationException(result.Message)) else
result.Result
let elements = Query<ChemicalElement array>("[{'type':'/chemistry/chemical_element','name':null,'boiling_point':null,'atomic_mass':null}]")
elements |> Array.iter(fun element->printfn "%A" element)
How would we do this today?
Trang 38Web Data
Cloud Data
Enterprise Data
Enterprise Data
Local Data Web Services
Your Data
The Magic: Type Providers
Trang 39A Type Provider is….
A design-time component that provides a computed space of types and methods…
A compiler/IDE extension…
An adaptor between data/services and NET languages…
Trang 40Note: F# still contains no data
Open architecture
You can write your own type provider
Trang 41type Netflix = ODataService< "http://odata.netflix.com" >
Fluent, Typed Access To OData
Trang 42SQL Server/Entity Framework
type SQL = SqlEntityConnection< "Server='.\\SQLEXPRESS' " >
Fluent, Typed Access To SQL
Trang 43type EmeaSite = SharePointSite< "http://myemea/" >
Fluent, Typed Access To SharePoint Lists
Fluent, Typed Access To SharePoint Lists
Trang 44F# Futures: Queries
let avatarTitles =
query { for t in netflix.Titles do
where (t.Name.Contains "Avatar" ) }
Declarative LINQ queries
Trang 45Fundamentals - Whitespace Matters
Trang 46Fundamentals - Whitespace Matters
let computeDerivative f x =
let p1 = f (x - 0.05)
let p2 = f (x + 0.05)
(p2 – p1) / 0.1
Trang 47Your First F# Application
printfn "Hello World"
C:\test> fsc test.fs
C:\test> test.exe Hello World
C:\test>
C:\test> fsc test.fs
C:\test> test.exe Hello World
C:\test>
Trang 48Your Second F# Application
open System.Windows.Form
let form = new Form (Visible=true)
form.Click.Add (fun _ -> printfn "click")
Application.Run form
Trang 49Functional– Pipelines
x |> f
The pipeline operator
Trang 50Functional– Pipelines
x |> f1 |> f2 |> f3
Successive stages
in a pipeline
Trang 51Exported properties Exported method Object internals
Trang 52F# Futures: Queries
let avatarTitles =
query { for t in netflix.Titles do
where (t.Name.Contains "Avatar" )
sortBy t.Name
take 100 }
Declarative LINQ queries
Trang 53AdPredict: What We Observed
Immediate scaling to massive data sets
mega-data structures, 16GB machines
Live in the domain, not the language
Schema compilation and “Schedules”
Especially Excel, SQL Server
Trang 54Await! Await! Await!
F# example: Serving 50,000+ simultaneous TCP connections with ~10 threads
Trang 55Case Study #5: Microsoft adCenter “adPredict”
Weeks of data in training:
N,000,000,000 impressions, 6TB data
2 weeks of CPU time during training:
2 wks × 7 days × 86,400 sec/day =
1,209,600 seconds Learning algorithm speed requirement:
N,000 impression updates / sec N00.0 μs per impression update
Trang 56Example #4: Biotech
F# rocks - building algorithms for DNA processing
and it's like a drug 12-15 at Amyris use F#
F# has been phenomenally useful I would be writing a lot of this in Python otherwise and F# is more robust, 20x
- 100x faster to run and faster to develop
Darren Platt, Amyris BioTechnologies