# F# For Quantitative Finance

June 10, 2014 3 Comments

I picked up Johan Astborg’s F# for Quantitative Finance a couple of days ago and I thought it was great. It was a practical hands-on way of working with FSharp. It is so good, I think I am going to use it as a basis for a couple Triangle .NET User group F# SIGs. One of the great things about the book are the code samples –they expose features of the F# language in a way that allows beginners to understand. So in the trade-off between clarity and cleverness, Astborg errs on the side of clarity. The only thing I think I would change would be to remove Chapter 2. Chapter 2 suffers the same problem that many “intro to X computer language” books have –> pages of language features with small independent samples. This kind of book does not help many (if any) people learn the language easily, and it reads about as well as the MSDN component docs.

I did notice that Astborg did use the typical scientific computing approach to variable naming. For example, on page 141 he creates a function like this:

- member this.black_scholes call_put_flag s x t r v =
- let cnd x =
- let a1 = 0.3198253
- let a2 = -0.35653782
- let a3 = 1.781477937
- let a4 = -1.821255978
- let a5 = 1.330274429
- let pi = 3.141592654
- let l = abs(x)
- let k = 1.0/(1.0 + 0.2316419)
- let w = (1.0)
- if x < 0.0 then 1.0 – w else w
- let d1=(log(s/x) + (r*v*v*0.5)*t)/(v*sqrt(t))
- let d2=d1-v*sqrt(t)
- match call_put_flag with
- | Put -> x*exp(-r*t)*cnd(-d2)-s*cnd(-d1)
- | Call ->s*cnd(d1)-x*exp(-r*t)*cnd(d2)

Concentrating only on the function’s arguments, what is s,x,t,r, and v? It is not immediately apparent and you have to read the comments above the function to discover that s is the stockprice, etc… I think there is a better way and I wonder if FxCop (if it ever comes to F#) would agree with me.. For example, this is a slightly better version

- member this.black_scholes call_put_flag stockPrice strikePriceOfPotion timeToExpierationInYears riskFreeInterestRate volatility =
- let cnd x =

The problem is that the number of arguments makes the naming unwieldy. A third, and much preferred option is to pass in a data structure:

- type PutCallFlag = Put | Call
- type black_scholes_input_data =
- {stockPrice:float;
- strikePriceOfPotion:float;
- timeToExpierationInYears:int;
- riskFreeInterestRate:float;
- volatility:float}
- type StockAnalyzer() =
- member this.black_scholes (call_put_flag:PutCallFlag,inputData:black_scholes_input_data) =
- let cnd x =
- let a1 = 0.3198253
- let a2 = -0.35653782
- let a3 = 1.781477937
- let a4 = -1.821255978
- let a5 = 1.330274429
- let pi = 3.141592654
- let l = abs(x)
- let k = 1.0/(1.0 + 0.2316419)
- let w = (1.0)
- if x < 0.0 then 1.0 – w else w
- let d1=(log(inputData.stockPrice/inputData.strikePriceOfPotion)

I doubt there is a performance hit and the code becomes much more readable. It also forces the really long functions to be broken up and each piece becomes more testable.I f there is not a corresponding domain for the input_arguments, then that naming will have to do.

Looks like a good book to pick up! And I’ve also noticed that quite a few grad students and/or researchers tend to use variable names like that…

Pingback: F# Weekly #24-#25, 2014 | Sergey Tihon's Blog

You know, there’s a good reason to use short variable names: it’s much easier to follow the structure of the equations, without getting lost in a sea of names. Short names, defined in the comments, is just about ideal, even more so in a case like this, where the abbreviations used are the standard ones used in the literature.