Black Scholes Option Pricing

The code shows simple implementation of blackscholes algorithm.

Copy Source
Copy Link
Tools:
 1: type Style = Call | Put
 2:   
 3: let cnd x =
 4:    let pow x n = exp (n * log(x) ) 
 5:    let a1 =  0.31938153
 6:    let a2 = -0.356563782
 7:    let a3 =  1.781477937
 8:    let a4 = -1.821255978
 9:    let a5 =  1.330274429
10:    let pi = 4.0 * atan 1.0
11:    let l  = abs(x)
12:    let k  = 1.0 / (1.0 + 0.2316419 * l)
13:    let w  = ref (1.0-1.0/sqrt(2.0*pi)*exp(-l*l/2.0)*(a1*k+a2*k*k+a3*(pow k 3.0)+a4*(pow k 4.0)+a5*(pow k 5.0)))
14:    if (x < 0.0) then  w := 1.0 - !w
15:    !w
16:  
17: // call_put_flag: 'c' if call option; otherwise put option
18: // s: stock price
19: // x: strike price of option
20: // t: time to expiration in years
21: // r: risk free interest rate
22: // v: volatility
23: let black_scholes style s x t r v =
24:         let d1=(log(s / x) + (r+v*v/2.0)*t)/(v*sqrt(t))
25:         let d2=d1-v*sqrt(t)
26:         match style with
27:          | Call -> s*cnd(d1)-x*exp(-r*t)*cnd(d2)
28:          | Put -> x*exp(-r*t)*cnd(-d2)-s*cnd(-d1)
29:  
30:  
31: // Example usage::
32: // black_scholes Call 60.0 65.0 0.25 0.08 0.3;;
union case Style.Call: Style
union case Style.Put: Style
val cnd : float -> float

Full name: Snippet.cnd
val x : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val pow : (float -> float -> float)
val n : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val exp : 'T -> 'T (requires member Exp)

Full name: Microsoft.FSharp.Core.Operators.exp
val log : 'T -> 'T (requires member Log)

Full name: Microsoft.FSharp.Core.Operators.log
val a1 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val a2 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val a3 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val a4 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val a5 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val pi : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val atan : 'T -> 'T (requires member Atan)

Full name: Microsoft.FSharp.Core.Operators.atan
val l : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val abs : 'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
val k : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val w : float ref

  type: float ref
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Ref<float>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Multiple items
val ref : 'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------

type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>

  type: 'T ref
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Ref<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val sqrt : 'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val black_scholes : Style -> float -> float -> float -> float -> float -> float

Full name: Snippet.black_scholes
val style : Style

  type: Style
  implements: System.IEquatable<Style>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Style>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val s : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val t : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val r : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val v : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val d1 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val d2 : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType

More information

Link: http://fssnip.net/3I
Posted: 3 years ago
Author: Kishor Aher (website)
Tags: Finance