Created
July 13, 2010 08:08
-
-
Save ecounysis/473605 to your computer and use it in GitHub Desktop.
Black-Scholes Option Pricing Model in OCaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let list_sum ls = List.fold_left (fun a x->a+.x) 0.0 ls | |
let list_init c f = Array.to_list (Array.init c (fun x->f x)) | |
let normal zz = | |
match zz with | |
|v when v = 0.0 -> 0.5 | |
|v when v >= 6.0 -> 1.0 | |
|v when v <= -6.0 -> 0.0 | |
|v -> | |
let p = 0.2316419 in | |
let b = [0.31938153;-0.356563782;1.781477937;-1.821255978;1.330274428] in | |
let f = 1.0/.(sqrt (2.0*.3.1415926)) in | |
let abszz = abs_float v in | |
let ff = f*.(exp((-.(abszz**2.0)/.2.0))) in | |
let sfunc e b = b/.((1.0+.p*.abszz)**e) in | |
let sfunclist = list_init 6 (fun x->(sfunc (float x))) in | |
let s = list_init 5 (fun x->List.nth sfunclist (x+1) (List.nth b x)) in | |
let sz = ff *. (list_sum s) in | |
if v>0.0 then 1.0-.sz else sz | |
let blackscholes strike asset standarddev riskfreerate days2expiration = | |
let daysinyear = 365.0 in | |
let sqrtt days = sqrt (days/.daysinyear) in | |
let n strike ass sd r days = | |
let ls = log ass in | |
let lx = log strike in | |
let t = days/.daysinyear in | |
let sd2 = sd**2.0 in | |
ls-.lx+.r*.t+.(sd2*.t/.2.0) in | |
let delta strike ass sd r days = | |
let n = n strike ass sd r days in | |
let sqT = sqrtt days in | |
let d = sd*.sqT in | |
let d1 = n/.d in | |
normal d1 in | |
let delta2 n sd days = | |
let sqT = sqrtt days in | |
let d = sd*.sqT in | |
let d1 = n/.d in | |
normal d1 in | |
let nd2 n sd days = | |
let sqT = sqrtt days in | |
let d = sd*.sqT in | |
let d1 = n/.d in | |
let d2 = d1-.sd*.sqT in | |
normal d2 in | |
let bond strike ass sd r days = | |
let n = n strike ass sd r days in | |
let t = days/.daysinyear in | |
let nd1 = delta2 n sd days in | |
let nd2 = nd2 n sd days in | |
-.strike*.nd2*.(exp ((-.r)*.t)) in | |
let callValue strike ass sd r days = | |
let n = n strike ass sd r days in | |
let t = days/.daysinyear in | |
let nd1 = delta2 n sd days in | |
let nd2 = nd2 n sd days in | |
let b = bond strike ass sd r days in | |
ass*.nd1+.b in | |
let putValue strike ass sd r days = | |
let t = days/.daysinyear in | |
let call = callValue strike ass sd r days in | |
(exp ((-.r)*.t)) *. strike -. ass +. call in | |
[(putValue strike asset standarddev riskfreerate days2expiration); | |
(callValue strike asset standarddev riskfreerate days2expiration); | |
(delta strike asset standarddev riskfreerate days2expiration); | |
(bond strike asset standarddev riskfreerate days2expiration)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment