F# Weekly #52, 2012 – New Year Edition

Welcome to F# Weekly,

This is 10th anniversary edition of F# Weekly and last in this year. I want to say thank you to those who were with me all the time. I wish you Happy New Year full of functional happiness, love and new achievements.

HappyNY2013

Last portion of news from 2012:

News

Blogs & Tutorials

That’s all for now.  See you in the New 2013 Year.

Previous F# Weekly edition – #51

Rhythm of the F# Сommunity heartbeat

The new year is getting closer. It is time of magic and miracles, time to sum up and to lift the veil of secrecy over F# Community.

I have been collecting all tweets (and retweets) with hashtag #fsharp since the beginning of November. It is time to show up some statistics. 😉

When are we tweeting? – Always!

F# Community Heartbeat
F# Community Heartbeat. London time zone (UTC)

Do we have a rest from F#? – A bit on Saturday.

DayOfWeek
Tweets distribution by day of week.

Who to follow? – Choose yourself.

Top 20 most active users (based on the tweets and RT)
Top 20 most active users (based on the tweets and RT)

Who writes more of unique tweets?

Top 20 users that generates unique tweets.
Top 20 users that generates unique tweets.

Who is the best retweeter?

Top 20 best retweeters.
Top 20 the best retweeters.

F#/.NET function minimization (optimization)

I have done some research on function minimization algorithms implemented on .NET. Short summary can be found below.

Gradient descent

Gradient descent is one of the simplest function optimization algorithms. You can implement it by yourself or using one of the following articles:

DotNumerics

DotNumerics is a Numerical Library for .NET. The library is written in pure C# and has more than 100,000 lines of code with the most advanced algorithms for Linear Algebra, Differential Equations and Optimization problems.

Unfortunately, dotNumerics does not have a detailed documentation. Let’s go through all minimization algorithms implemented in dotNumerics. First of all, we implement banana function from simplex method example available on the library site.

#r @"DotNumerics.dll"
open System
open DotNumerics.Optimization

//f(a,b) = 100*(b-a^2)^2 + (1-a)^2
let BananaFunction (x: float array) =
    100.0 * Math.Pow((x.[1] - x.[0] * x.[0]), 2.0) + Math.Pow((1.0 - x.[0]), 2.0)

Downhill Simplex

Downhill Simplex method of Nelder and Mead

The key advantage of Downhill Simplex method is that it does not require the gradient function. All you need is a function and an initial guess.

let initialGuess = [|0.1; 2.0|]

let simplexMin =
    let simplex = Simplex();
    simplex.ComputeMin(BananaFunction,initialGuess);

We have a bit of control over the evaluation model. We can restrict MaxFunEvaluations and specify custom Tolerance in Simplex model. In this case, model instantiation looks like below.

    let simplex = Simplex(MaxFunEvaluations=10000, Tolerance=1e-5);

Truncated Newton

“A Survey of Truncated-Newton Methods”, Journal of Computational and Applied Mathematics.

All other algorithms require gradient function to make calculation.


//f'a(a,b) = (100*(b-a^2)^2 + (1-a)^2)'a = 100*2*(b-a^2)*(-2a) - 2*(1-a)
//f'b(a,b) = (100*(b-a^2)^2 + (1-a)^2)'b = 100*2*(b-a^2)
let BananaFunctionGradient (x: float array) =
    [|100.0 * 2.0 * (x.[1] - x.[0] * x.[0]) * (-2.0 * x.[0]) - 2.0 * (1.0 - x.[0]);
      100.0 * 2.0 * (x.[1] - x.[0] * x.[0])|]

let newtonMin =
    let newton = TruncatedNewton()
    newton.ComputeMin(BananaFunction,BananaFunctionGradient,initialGuess);

Truncated Newton algorithm has three more configuration parameters than Downhill Simplex: Accuracy, MaximunStep and SearchSeverity.

L-BFGS-B

Limited memory Broyden–Fletcher–Goldfarb–Shanno method

let bfgsMin =
    let lbfgsb = L_BFGS_B()
    lbfgsb.ComputeMin(BananaFunction, BananaFunctionGradient, initialGuess);

L-BFGS-B has one more configuration parameters than Downhill Simplex – it is AccuracyFactor.

Results

Below you can find evaluation results received from models with default parameters.

Real: 00:00:00.024, CPU: 00:00:00.062, GC gen0: 0, gen1: 0, gen2: 0
val simplexMin : float [] = [|0.999999998; 0.9999999956|]
Real: 00:00:00.074, CPU: 00:00:00.078, GC gen0: 0, gen1: 0, gen2: 0
val newtonMin : float [] = [|0.9999999999; 0.9999999999|]
Real: 00:00:00.137, CPU: 00:00:00.140, GC gen0: 0, gen1: 0, gen2: 0
val bfgsMin : float [] = [|1.0; 1.0|]

F# Weekly #51, 2012

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Blogs & Tutorials

That’s all for now.  Have a great week and remember you can message me on twitter (@sergey_tihon) with any F# news.

Previous F# Weekly edition – #50

F# for heating

Sometimes, when you feel really alone and want a bit of heat then F# can help you here.

All you need is a laptop and F#. Just type the following snippet into FSI and wait for a minute. =)

[|1..999|] |> Array.Parallel.iter (fun _ ->
    while true do ignore())

Wish you warm Christmas!

P.S. The same solution works when you are freezing.

F# Weekly #50, 2012

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Blogs & Tutorials

Upcoming events

That’s all for now.  Have a great week and remember you can message me on twitter (@sergey_tihon) with any F# news.

Previous F# Weekly edition – #49

F# Weekly #49, 2012

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Blogs & Tutorials

That’s all for now.  Have a great week and remember you can message me on twitter (@sergey_tihon) with any F# news.

Previous F# Weekly edition – #48

F# Weekly #48, 2012

Welcome to F# Weekly,

F# shines like a diamond in the daily coding routine. Piece of that shine from this past week:

News

  • Zach Bray announced FunScript. (F# to JavaScript compiler with JQuery etc. mappings through a TypeScript type provider)
  • The Nuget team fixed the F# compatibility bugs. You can use the nightly build.
  • F# works with MonoDroid.(more details)
  • Voting began for better F# support in NuGet.

Blogs & Tutorials

Upcoming events

That’s all for now.  Have a great week and remember you can message me on twitter (@sergey_tihon) with any F# news.

Previous F# Weekly edition – #47