Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 25. September 2011 17:40

As promised at the meeting, here is the code I used in the presentation at the North Bay .NET user group last week. Let me know if you have comments or questions!

by Mathias 25. September 2011 05:31

headCodeCamp

It’s this time of the year again: on Saturday & Sunday October 8 + 9, Silicon Valley CodeCamp is taking place at Foothill College in Los Altos Hill. There are currently over 200 sessions listed, and 2000 people signed up already. I am expecting lots of fun - again.

I’ll be giving 3 talks this year:

  • Sat, 11:45 - Beginning TDD for C# Developers.
  • Sun, 1:15: For Those About to Mock.
  • Sun, 2:45: An excursion in F#

Hope to see you there, and also that I will have some energy left to attend some of the other talks!

More information about my talks here.

by Mathias 23. September 2011 12:47

I had the pleasure to present at the North Bay .NET user group this week on F#, where people asked me all sorts of great questions. At some point, we got into List comprehensions – a convenient syntax to generate lists - via an example along these lines (which returns a list of the multiple of 2, from 2 to 40):

let list = 
   [ for i in 1 .. 20 do yield 2 * i ]

While slightly more complex, the actual example is in essence equivalent. A question followed: how complex can the code within the brackets be?

Well, pretty much as complex as you want it to be. Take this for instance:

let list =
   [
      for i in 1 .. 10 do yield 2 * i
      for i in 1 .. 10 do yield 3 * i
   ]

This will return a list of the multiples of 2 from 2 to 20, followed in the same list by the multiples of 3 from 3 to 30. Nice.

But you can go much wilder, and start putting code in there, too. For instance, we can expand the previous example a bit and morph it into a nice and concise FizzBuzz:

type Fizzbuzz = Fizz | Buzz | FizzBuzz | Number of int

let fizzBuzz n =
   [
      let fizzBuzzConvert number =
         if number % 2 = 0 && number % 5 = 0 then FizzBuzz
         elif number % 5 = 0 then Buzz
         elif number % 2 = 0 then Fizz
         else Number(number)
      
      for i in 1 .. n do yield fizzBuzzConvert i
   ]

We declare a discriminated union, covering all the possible outcomes of FizzBuzz, declare inside the comprehension itself a function that maps an integer to a FizzBuzz result, and generate the list of results from i to n. Running this in the interactive window results in the following:

let fizzBuzz n =
   [
      let fizzBuzzConvert number =
         if number % 2 = 0 && number % 5 = 0 then FizzBuzz
         elif number % 5 = 0 then Buzz
         elif number % 2 = 0 then Fizz
         else Number(number)
      
      for i in 1 .. n do yield fizzBuzzConvert i
   ];;

type Fizzbuzz =
  | Fizz
  | Buzz
  | FizzBuzz
  | Number of int
val fizzBuzz : int -> Fizzbuzz list

> let f = fizzBuzz 20;;

val f : Fizzbuzz list =
  [Number 1; Fizz; Number 3; Fizz; Buzz; Fizz; Number 7; Fizz; Number 9;
   FizzBuzz; Number 11; Fizz; Number 13; Fizz; Buzz; Fizz; Number 17; Fizz;
   Number 19; FizzBuzz]

I would classify List comprehension under the “nice to have” features – it’s perfectly possible to write excellent code without it. At the same time, it’s a very, very convenient way to work with Lists, which I now miss in C#…

by Mathias 12. September 2011 14:39

Just saw that version 3.1 of Microsoft Solver Foundation has been released. I haven’t had time to try it out yet, but the list of improvements looks promising. A better non linear solver, better MIP and MIQP – sounds like Santa has come early! And I am curious about the faster bindings with LINQ…

by Mathias 11. September 2011 12:39

The project I am currently working on involves developing a forecasting model. Starting from an initial estimate, the model will progressively update its forecast as time goes by and real data becomes available.

The process of developing such a model is iterative by nature: you design the mechanics of a forecasting algorithm, look at how it would have performed on historical data, fine-tune the design and parameters based on the results, and rinse & repeat.

The problem I started running into is the “look at how it would have performed on historical data”. There is plenty of data available, which is both a blessing and a curse. A blessing, because more data means better validation. A curse, because as the amount of data increases, it becomes difficult to navigate through it, and focus on individual cases.

So far, my approach has been to create metrics of fit between a model and a set of data, and to run a model against large sets of data, measuring how well the model is doing against the data set. However, I still don’t have a good solution for digging into why a particular case is not working so well. What I would like to achieve is to identify a problematic case, and explore what is going on, ideally by generating charts on the fly to visualize what is happening. Unfortunately, the tools I am using currently do not accommodate that scenario well. Excel is great at producing charts in a flexible manner, but my model is .NET code, and I don’t have a convenient, lightweight way to use C# code in Excel. Conversely, creating exploratory charts from C# is somewhat expensive, and requires a lengthy cycle: write code for the chart, compile (and lose whatever is loaded in memory), observe – and repeat.

I am currently exploring an alternative, via F# and FSharpChart. F# offers a very interesting possibility over C#, F# Interactive (fsi). Fsi is a REPL (Read, Evaluate, Print Loop), which allows you to type in code interactively in the console and execute it as you go. The beauty of it is that you can experiment with code live, without having to go through the code change / recompile cycle. Add to the mix FSharpChart, a library/script which wraps .NET DataVisualization.Charting and makes it conveniently usable from F#, and you get a nice way to write .NET code and generate charts, on the fly.

Let’s illustrate on a simple example. Suppose I have a model that simulates sales following a Poisson process, and want to check whether this “looks right”. First, let’s download FSharpChart, create a folder called “Explore” on the Desktop, and copy the FSharpChart.fsx script file into that folder. Then, let’s create an empty text file called Explore.fsx in the same folder, which we will use to experiment with code and charts, and save whatever snippets come in handy at the time.

Setup

Next, let’s double-click on the Explore.fsx file, which will then be opened in Visual Studio, and type in the following:

#load @"C:\Users\Mathias\Desktop\Explore\fsharpchart.fsx"

open System
open System.Drawing
open MSDN.FSharp.Charting

let random = new Random()

// Simulate a Poisson distribution with parameter lambda
let poisson lambda =
   let L = Math.Exp(-lambda)
   let rec simulate (k,p) =
      if p > L then simulate (k + 1, p * random.NextDouble())
      else k - 1
   simulate (0, 1.0)

let sales lambda periods = [ 
   for i in 1.0 .. periods -> (i, poisson lambda) ]

More...

Comments

Comment RSS