我试图在f#中实现我已经在c#中使用的东西,看看语法是多么简洁.我使用期权定价公式(黑色76)作为测试,因为这对我来说似乎是一个功能问题.一切似乎都很好,但我无法计算隐含的卷,因为我需要从内部调用同一个类的方法.这是我到目前为止:
module Module1
open System
type Black76 (CallPutFlag, Fwd, Strike, time, rf, vol, ?BlackPrice:float) =
let d1 = (Math.Log(Fwd / Strike) + (vol * vol * 0.5) * time) / (vol * Math.Sqrt(time))
let d2 = d1 - vol * Math.Sqrt(time)
let n = new MathNet.Numerics.Distributions.Normal()
member x.valuation =
match CallPutFlag with
| "c" | "C" | "Call" | "call" -> Math.Exp(-rf * time) * (Fwd * n.InverseCumulativeDistribution(d1) - Strike * n.InverseCumulativeDistribution(d2))
| "p" | "P" | "Put" | "put" -> Math.Exp(-rf * time) * (Strike * n.InverseCumulativeDistribution(-d2)- Fwd * n.InverseCumulativeDistribution(-d1))
| _ -> failwith "Unrecognized option type"
member x.delta =
match CallPutFlag with
| "c" | "C" | "Call" | "call" -> Math.Exp(-rf * time) * n.InverseCumulativeDistribution(d1)
| "p" | "P" | "Put" | "put" -> Math.Exp(-rf * time) * n.InverseCumulativeDistribution(-d1)
| _ -> failwith "Unrecognized option type"
member x.gamma =
Math.Exp(-rf * time) * (n.Density(d1) / (Fwd * vol * Math.Sqrt(time)))
member x.vega =
Math.Exp(-rf * time) * n.Density(d1) * Fwd * Math.Sqrt(time)
member x.rho =
match CallPutFlag with
| "c" | "C" | "Call" | "call" -> time * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2)
| "p" | "P" | "Put" | "put" -> -time * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(-d2)
| _ -> failwith "Unrecognized option type"
member x.theta =
match CallPutFlag with
| "c" | "C" | "Call" | "call" -> -(Fwd * vol * n.Density(d1)) / (2.0 * Math.Sqrt(time)) - rf * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2)
| "p" | "P" | "Put" | "put" -> -(Fwd * vol * n.Density(d1)) / (2.0 * Math.Sqrt(time)) + rf * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(-d2)
| _ -> failwith "Unrecognized option type"
member x.impliedvol =
let vst = Math.Sqrt(2.0*Math.Abs((Math.Log(Fwd/Strike)+rf*time)/time))
let tol = 0.0001
let mutable v = vst
let mutable sigmadiff = 1.0
let mutable k = 1
let kmax = 100
while (sigmadiff >= tol && k < kmax) do
let option = Black76.valuation(CallPutFlag, Fwd, Strike, time, rf, v)
let cvega = Black76.vega(CallPutFlag, Fwd, Strike, time, rf, v)
let increment = (option - BlackPrice) / cvega
v <- v - increment
k < - k + 1
sigmadiff = Math.Abs(increment)
v
Run Code Online (Sandbox Code Playgroud)
除了隐含的vol函数之外,这一切都有效.此外,它似乎不比c#版本简洁得多.你能不能让我知道如何从内部调用隐含的功能?你也知道如何摆脱let mutable(毕竟你不应该在fsharp中使用它(我想).谢谢
如果你想让代码更简洁,更实用,那么我可能会尝试重新构建它.我认为下面这些内容应该有用.
首先,你肯定不想重复字符串上的匹配,所以我定义了一个区分联合来捕获各种计算(然后你可以只解析一次字符串):
type CallPutFlag = Call | Put
Run Code Online (Sandbox Code Playgroud)
接下来,我们可以定义记录以保持等式的结果(我只添加了你正在使用的东西,但你可能想在这里添加更多):
type Black76Results = { Vega : float; Valuation : float }
Run Code Online (Sandbox Code Playgroud)
现在,我认为将black76函数与隐含波动率分开是很有意义的.该black76函数可以运行给定输入的计算,并将结果作为Black76Results记录值返回:
let black76 flag fwd strike time rf vol =
let d1 = (Math.Log(fwd / strike) + (vol * vol * 0.5) * time) / (vol * Math.Sqrt(time))
let d2 = d1 - vol * Math.Sqrt(time)
let n = new MathNet.Numerics.Distributions.Normal()
match flag with
| Call ->
let valuation = Math.Exp(-rf * time) * (fwd * n.InverseCumulativeDistribution(d1) - strike * n.InverseCumulativeDistribution(d2))
let delta = Math.Exp(-rf * time) * n.InverseCumulativeDistribution(d1)
let gamma = Math.Exp(-rf * time) * (n.Density(d1) / (fwd * vol * Math.Sqrt(time)))
let vega = Math.Exp(-rf * time) * n.Density(d1) * fwd * Math.Sqrt(time)
let rho = time * strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2)
let theta = -(fwd * vol * n.Density(d1)) / (2.0 * Math.Sqrt(time)) - rf * strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2)
{ Vega = vega; Valuation = valuation }
| Put ->
failwith "TODO: Similar for Put"
Run Code Online (Sandbox Code Playgroud)
虽然在一些共享代码Call和Put,我认为它看起来很多更具可读性,当你将二者分开到不同的分支(你仍然可以提取代码常用件到一个单独的功能).
现在,impliedVol它只是一个black76重复调用的函数:
let impliedVol flag fwd strike time rf blackPrice =
let vst = Math.Sqrt(2.0*Math.Abs((Math.Log(fwd/strike)+rf*time)/time))
let tol = 0.0001
let mutable v = vst
let mutable sigmadiff = 1.0
let mutable k = 1
let kmax = 100
while (sigmadiff >= tol && k < kmax) do
let b = black76 flag fwd strike time rf v
let option = b.Valuation
let cvega = b.Vega
let increment = (option - blackPrice) / cvega
v <- v - increment
k <- k + 1
sigmadiff <- Math.Abs(increment)
v
Run Code Online (Sandbox Code Playgroud)