Archive for March 2010

Happy Pi Day and Monte Carlo Method

Pi Calculation

Today is the \pi day, 03/14 2010. Happy \piday to all!

The Monte Carlo method generates multiple trials to determine the expected value of a random variable.

Calculating Pi is generally the hello world of Monte Carlo Method in Stochastic Calculus. So for today, I will try to give a sample calculation of pi as monte carlo in F#. There are very good articles on the Monte carlo and how the pi calculation is formulated.

Generally these are the steps to follow, before running the simulation

  1. Define the input set
  2. Generate randomly the input set
  3. Filter the random set using some computations
  4. Fold the result to the final result

Pi calculation formula :
\frac{Hitting inside the circle} {Hitting Outside the circle} = \frac { 1/4  \pi r^2} { r^2} = \frac {1}{4}\pi

open System
let calcPi() =
    let ran = new Random()
    let distances = seq { for i in 0 .. 100000 do
                            let x,y  = ran.NextDouble(), ran.NextDouble()
                            yield sqrt (x * x + y * y)
         }
 
    let count = distances |> Seq.filter (fun distance -> distance <= 1.0) 
                              |> Seq.length     
  4.0 * (float count) / (float (Seq.length distances))
 
let errorRate() = (1.0 - (calcPi() / Math.PI)) * 100.0
 
let print5 value = for i in 0 .. 5 do printf "%f\n" (value())
print5 (errorRate)
val calcPi : unit -> float
val errorRate : unit -> float
val print5 : (unit -> float) -> unit
Error rates:
-0.024698
-0.093453
-0.013239
-0.094726
-0.373563
0.308887
>

.

Book Review – Real World Functional Programming

Real World Functional Programming

Real-World functional programming has been written by Tomas Petricek and Jon Skeet.
Tomasz’s and Jon’s book is built around how to think functionally when programming and how to make best use of functional paradigm for real world scenerios. The book is written  mainly for the C# programmers who would like to switch/learn more of the functional world. It also features snippets with some technical background of the ideas involved.
As the title suggests the book is not focussed on one language, instead a mix of C# and F#, and it does a great job in bringing these worlds together,which is to me best of both worlds. Although, samples are all in functional style, when the problem needs the functional beauty to express, F# takes place. This gives any .NET developer to see when to use the necessary language for a particular problem.

The book starts slowly with the functional structures, and goes to monads, and to reactive libraries.  I found the language of the book clear to understand, and easy to follow. The samples in the beginning of a chapter starts with some really simple constructs, but at the end of a chapter they become more complicated, and a nice programs to reflect the idea of the chapter.

Applied functional programming is probably the most sophisticated part of the book. Some ideas mainly inspired from research papers (cited at the end) blended with modern languages and libraries and applied somewhat differently. Especially composable functional libraries and reactive functional programs were insightful and open the mind with new possibilities.

Finally, I would recommend this book whoever wants to switch to functional programming and also learn new techniques in general. Each sample is crafted well, and represent real value with its tutorial writing style.