Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems calling a method within the same class in f#
    text
    copied!<p>I m trying to implement something in f# that I already have in c# to see how much more succinct the syntax is. I used an option pricing formula (Black 76) as test because this seems like a functional problem to me. All seems fine but I have trouble computing the implied vol as I need to call a method of the same class from within. Here is what I have so far:</p> <pre class="lang-ml prettyprint-override"><code>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" -&gt; Math.Exp(-rf * time) * (Fwd * n.InverseCumulativeDistribution(d1) - Strike * n.InverseCumulativeDistribution(d2)) | "p" | "P" | "Put" | "put" -&gt; Math.Exp(-rf * time) * (Strike * n.InverseCumulativeDistribution(-d2)- Fwd * n.InverseCumulativeDistribution(-d1)) | _ -&gt; failwith "Unrecognized option type" member x.delta = match CallPutFlag with | "c" | "C" | "Call" | "call" -&gt; Math.Exp(-rf * time) * n.InverseCumulativeDistribution(d1) | "p" | "P" | "Put" | "put" -&gt; Math.Exp(-rf * time) * n.InverseCumulativeDistribution(-d1) | _ -&gt; 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" -&gt; time * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2) | "p" | "P" | "Put" | "put" -&gt; -time * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(-d2) | _ -&gt; failwith "Unrecognized option type" member x.theta = match CallPutFlag with | "c" | "C" | "Call" | "call" -&gt; -(Fwd * vol * n.Density(d1)) / (2.0 * Math.Sqrt(time)) - rf * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(d2) | "p" | "P" | "Put" | "put" -&gt; -(Fwd * vol * n.Density(d1)) / (2.0 * Math.Sqrt(time)) + rf * Strike * Math.Sqrt(-rf * time) * n.InverseCumulativeDistribution(-d2) | _ -&gt; 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 &gt;= tol &amp;&amp; k &lt; 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 &lt;- v - increment k &lt; - k + 1 sigmadiff = Math.Abs(increment) v </code></pre> <p>This all works apart from the implied vol function. Also it does not seem to be much more succinct than the c# version. Could you please let me know how I can call the method from within for the implied vol funcitons? Also do you know how to get rid of the let mutable (after all you are not supposed to use this in fsharp (I think). thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload