Blog Home  Home Feed your aggregator (RSS 2.0)  
A Random Walk Around .Net - Black Scholes F#
 
 Monday, June 09, 2008

F# is a relatively new .NET technology that is worthy of a developer’s arsenal.  It provides quite a few benefits such as type inference, native non-mutable states for concurrency, pattern matching and script-esque capabilities.

                One thing I would love is the ability to write unit tests in F#.  I believe the resulting code would be succinct and reusable.  Though, I have not figured out such a framework, here is my first attempt at F# through the famous Black Scholes .

                Without much of a history, finance or math lesson, Black Scholes is an equation to price options.  Basically, an option is a financial instrument that gives the buyer the option to buy or sell another underlying asset at a certain price in the future for a premium paid today.  Black Scholes is a closed form equation to help estimate the premium to charge for such an option.

#light

open System

type PutOrCall =
   | Call = 1
   | Put = 2

let CumulativeNormalDistribution (x: double) =
   let L = abs(x)
   let K = 1.0 / (1.0 + 0.2316419 * L)
   let dcnd =
      1.0 
      - 1.0 
      / sqrt(2.0 * Math.PI) 
      * exp(-L * L / 2.0) 
      * (0.31938153 * K 
      + -0.356563782 * K  * K 
      + 1.781477937 * Math.Pow(K, 3.0) 
      + -1.821255978 * Math.Pow(K, 4.0) 
      + 1.330274429 * Math.Pow(K, 5.0))
   if x < 0.0 then 1.0 - dcnd
   else dcnd
   
let BlackScholes (pc: PutOrCall) stock strike time interestRate vol =
   let d1 = (log(stock / strike) 
             + (interestRate + vol * vol / 2.0) * time) 
             / (vol * sqrt(time))
   let d2 = d1 - vol * sqrt(time)
   if pc = PutOrCall.Call then 
      stock * CumulativeNormalDistribution(d1) - 
      strike * exp(-interestRate * time) * CumulativeNormalDistribution(d2)
   else 
      strike * exp(-interestRate * time) * CumulativeNormalDistribution(-d2) - 
      stock * CumulativeNormalDistribution(-d1)
Monday, June 09, 2008 8:15:11 PM (Central Standard Time, UTC-06:00)  #    Comments [0]    | 
Comments are closed.
Copyright © 2010 Yezdaan Baber. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: