.NET at 9.400 ft above sea level

Programming in Quito, 2.860 m above sea level

  • A quick and dirty implementation of Excel NORMINV in F#

    A couple of weeks ago I posted an example implementation of Excel NORMINV function in C#, in there I mentioned that what I actually needed was an F# version, but I used C# as an stepping stone moving from the C++ version I originally found. Well, here you have my attempt at implementing NORMINV in F#:

    let probit mu sigma p =
        if p < 0. || p > 1. then failwith "The probability p must be greater than 0 and lower than 1"
        elif sigma < 0. then failwith "The standard deviation sigma must be positive"
        elif sigma = 0. then mu
            let q = p - 0.5
            let value =
                if abs q <= 0.425 then          // 0.075 <= p <= 0.925
                    let r = 0.180625 - q * q
                    let a0, a1, a2, a3, a4, a5, a6, a7 =
                        3.387132872796366608, 133.14166789178437745, 1971.5909503065514427, 13731.693765509461125,
                        45921.953931549871457, 67265.770927008700853, 33430.575583588128105, 2509.0809287301226727
                    let b0, b1, b2, b3, b4, b5, b6, b7 =
                        1., 42.313330701600911252, 687.1870074920579083, 5394.1960214247511077,
                        21213.794301586595867, 39307.89580009271061, 28729.085735721942674, 5226.495278852854561
                    q*(((((((a7*r + a6)*r + a5)*r + a4)*r + a3)*r + a2)*r + a1)*r + a0)
                        / (((((((b7*r + b6)*r + b5)*r + b4)*r + b3)*r + b2)*r + b1)*r + b0)
                else                            // closer than 0.075 from {0,1} boundary
                    let r = sqrt -(log (if q > 0. then 1. - p else p))
                    let val1 =
                        if r <= 5. then         // <==> min(p,1-p) >= exp(-25) ~= 1.3888e-11
                    let a0, a1, a2, a3, a4, a5, a6, a7 =
                         1.42343711074968357734, 4.6303378461565452959, 5.7694972214606914055, 3.64784832476320460504,
                         1.27045825245236838258, 0.24178072517745061177, 0.0227238449892691845833, 7.7454501427834140764e-4
                     let b0, b1, b2, b3, b4, b5, b6, b7 =
                         1., 2.05319162663775882187, 1.6763848301838038494, 0.68976733498510000455,
                        0.14810397642748007459, 0.0151986665636164571966, 5.475938084995344946e-4, 1.05075007164441684324e-9
                            let r1 = r - 1.6
                            (((((((a7*r1 + a6)*r1 + a5)*r1 + a4)*r1 + a3)*r1 + a2)*r1 + a1)*r1 + a0)
                                / (((((((b7*r1 + b6)*r1 + b5)*r1 + b4)*r1 + b3)*r1 + b2)*r1 + b1)*r1 + b0)
                        else                    // very close to  0 or 1
                            let a0, a1, a2, a3, a4, a5, a6, a7 =
                                6.6579046435011037772, 5.4637849111641143699, 1.7848265399172913358, 0.29656057182850489123,
                                0.026532189526576123093, 0.0012426609473880784386, 2.71155556874348757815e-5, 2.01033439929228813265e-7
                            let b0, b1, b2, b3, b4, b5, b6, b7 =
                                1., 0.59983220655588793769, 0.13692988092273580531, 0.0148753612908506148525,
                                7.868691311456132591e-4, 1.8463183175100546818e-5, 1.4215117583164458887e-7, 2.04426310338993978564e-15
                            let r1 = r - 5.
                            (((((((a7*r1 + a6)*r1 + a5)*r1 + a4)*r1 + a3)*r1 + a2)*r1 + a1)*r1 + a0)
                                / (((((((b7*r1 + b6)*r1 + b5)*r1 + b4)*r1 + b3)*r1 + b2)*r1 + b1)*r1 + b0)
                    if q < 0. then -val1 else val1
            sigma*value + mu

    I wouldn’t get angry if some of you think the code is ugly, I’m afraid such is the way of evaluating several polynomials (each pair of polynomials carefully tuned for a specific segment) and then dividing them. At any rate, I invite you to compare this version with the C# version (let alone the C++ version), I hope you find the F# implementation terser. You can download a Visual Studio 2010 sample project here:

    Please remember I have made only a handful of tests, so I encourage you to do (a lot of) additional testing if you plan to use this function for anything even slightly serious.


  • F#, the ACM, and the SEC

    It all started with a twit from @mulambda: “Phil Wadler lists #fsharp as a candidate for SEC regulation spec language: http://tinyurl.com/2edfxka” I downloaded the, nonetheless, Association for Computer Machinery answer to the Securities and Exchange Commission proposal (and ask for comments) on requiring Python programs to be provided to explain contractual cash flow provisions. I quickly skimmed the ACM document and twitted “Java, C#, and F# recommended by the #ACM for SEC regulation spec language http://is.gd/e49Vt #fsharp /via @mulambda”. Later, I read with more care the ACM answer and I found that I really should clarify my twit:


  • A quick and dirty implementation of Excel NORMINV function in C#

    We are piloting the implementation of some financial risk models in F#, it so happens that the models are already implemented in Excel, so I was slowly digging out the formulas in the cells and translating them to F#. Everything was going fine until I found out that some formulas used the NORMINV function which doesn't exist in the .NET libraries. I started to look for F#, and then C#, implementations without luck (as we are just in the lets-see-if-this-have-any-chance-of-flying stage, we can’t afford any of the excellent but paid numerical libraries for .NET). The closest thing I found was a C++ implementation. The code looked really weird to me (my fault, not the coder's), so I decided to do the translation in two steps: first from C++ to C#, then on to F#. The C# translation seems to be working now, and you can download it from SkyDrive:


  • Scrum vs. CMMI Level 3

    Of late, I have been helping start a Microsoft SDL implementation effort and, as part of it, it comes the decision of what flavor of MSF we should use: Agile (Scrum nowadays) or CMMI (roughly Level 3 with the Team Foundation Server template). Now, this is a corporate customer, expecting to have budgets and schedules defined in order to green light any sizeable project, so we naturally lean to CMMI but I can’t help remembering all the formal methodology implementation efforts I’ve seen (and sometimes helped Ruborizado) fail (RUP, anyone?). So, after a few years, I am reading about the subject again, and in Chapter 7 “Effective Change Leadership for Process Improvement” of Michael West’s Real Process Improvement Using the CMMI (ISBN 0849321093), I find these pearls of wisdom on the behavior of top management:

    You are not exempt from the history or the statistically documented behavior patterns of your peer executives, you are not an exception, so here is what you’re going to say to the organization when it comes to CMMI or process improvement:

    • We must get to CMMI Level (pick your number, 1 to 5) to remain competitive.

    • I want everyone to support the CMMI effort.

    • Your job (or promotion or performance review or raise, you choose) depends on your contribution to CMMI (or getting the maturity level).

    • The long-term viability of our enterprise relies on our process capability, not on our individual heroics.

    • These are our processes and I expect all of you to follow them.

    • CMMI is really important to all of us and our future.

    • I expect all of you to embrace change and do things differently.

    Those are the things you’ll say. Here is what you’re going to do or not do :

    • When push comes to shove, when you are forced to choose between heroics and process to get the product out the door, you will choose heroics.

    • You will reward the heroes, the people who work all night or all weekend to solve a customer problem. You will not reward the silent, humble engineer who puts quality first and prevents problems from ever getting to the customer.

    • You will ask for status of the CMMI effort in terms of CMMI compliance. You will not ask for measures that indicate improvements in productivity, quality, cycle time, employee satisfaction, customer satisfaction, organizational learning, or other measures of operational excellence.

    • You will not ask the process people to give you estimates for effort, cost, and schedule to achieve a CMMI maturity level. You will give them a target date that is tied to your bonus.

    • You will tell the people who report to you that you want their support of the CMMI effort. You will not give them any incentive, positive or negative, for that support.

    • You will not bother to learn the organizational processes yourself; they are for everyone else.

    • You will not personally exhibit the change in behaviors you expect from others.

    Alas, as so many who have gone before you, you will fail. Oh, you will get your maturity level/bonus/promotion/raise/praise/plaque on the wall, but make no mistake, you will have failed in leading your organization to change and grow.

    So exactly and painfully true! I really don’t have an answer for the customer yet, but I will certainly read as much (and as fast) as I can of Michael book.


  • Visual Studio 2010 and .NET Framework Beta 2 available on Wednesday

    This blog has been abandoned for the longest time :-$ but I’ve got great news to try and re-inaugurate it (again): it’s just been announced that beta 2 for Visual Studio 2010 and .NET Framework 4 will be available the day after tomorrow, i.e. on October 21st; moreover, we now have a firm date for the launch of the final versions of these products: March 22nd 2010. There is a lot of cool stuff in the new versions of Visual Studio and .NET Framework but my personal favorites (at least for the time being :-) are:


  • Entity Framework, LINQ to SQL and Oracle

    Amid the debate about which is better and have more future (two things that not necessarily go together) between LINQ to SQL and Entity Framework, one thing they have in common is the fact that Oracle is in “no comment” mode about both of them. It’s like Oracle would be expecting that the lack of its “official” provider for Entity Framework, let alone LINQ to SQL, would somehow move people to develop in Java instead of .NET Framework. IMHO, Visual Studio 2008 is so productive that people may first consider moving from Oracle to SQL Server before moving from VS 2008 to JDeveloper.


  • Free F# libraries (well, almost)

    In what was one of the very last PDC2008 sessions, Luca Bolognese did an encore presentation of F#, instead of trying to tell you what it was all about I invite you to watch the video (Luca is engaging and funny, and the session is so packed with information that one our will pass in no time). What I wanted to do is to talk about a couple of very interesting libraries, all written in F#, that Luca used in his demos:


  • The first Visual F# CTP is here!

    You leave on vacation for one short week and a lot happens... for example, Don Syme & co. have released the first F# CTP, well on the way (hopefully before this year's end) to put F# on the same level as C#, C++ or VB.NET. As far as I know, this will be an historical event: for the first time a mainstream platform (commercial or otherwise) wholly adopts a functional language. Allow me to seize the occasion to reiterate that there are several reasons for the functional programming paradigm to be considered interesting important, IMHO the most relevant are: