Ahhh complex numbers in Excel VBA, the pinnacle of cubicle programming. Been there, done that!
The functions that you quote are indeed returning string
types. But you should be able to compose those functions, e.g.
Option Explicit
Public Function cf(u As Double, ttm As Double, rf As Double, q As Double, s As Double) As String
Dim b As Double
b = rf - q - 0.5 * s ^ 2
If u = 0 Then
cf = "1.0"
Else
With Application.WorksheetFunction
cf = .ImExp(.Complex(-0.5 * u ^ 2 * s ^ 2 * ttm, ttm * u * b))
End With
End If
End Function
This function will now return a string type, but Excel can nevertheless consume it:
Sub ATest()
Dim z1 As String, z2 As String, z3 As String
Dim x1 As Double, x2 As Double
' some parameters
Const ttm As Double = 1#
Const rf As Double = 0.05
Const q As Double = 0#
Const sigma As Double = 0.2
Const eps As Double = 0.0001
z1 = cf(-eps, ttm, rf, q, sigma)
z2 = cf(0, ttm, rf, q, sigma)
z3 = cf(eps, ttm, rf, q, sigma)
With Application.WorksheetFunction
x1 = .ImReal(.ImDiv( _
.ImSub(z3, z1), _
.Complex(0, 2 * eps))) ' first moment estimator
x2 = .ImReal(.ImDiv( _
.ImSum(.ImSub(z1, z2), .ImSub(z3, z2)), _
.Complex(-eps * eps, 0))) ' second moment estimator
End With
Debug.Print "drift:", Round(CDbl(x1), 6), (rf - 0.5 * sigma ^ 2) * ttm
Debug.Print "variance:", Round(CDbl(x2) - CDbl(x1) ^ 2, 6), sigma ^ 2 * ttm
End Sub
will result in
drift: 0,03 0,03
variance: 0,04 0,04
i.e. the return expectation.
If you want another implementation, there is one available at pfadintegral (I have never checked it). Another path, of course, would be to compose your own library in VBA (as I said, been there, done that!), but I think it may not be worthwile as there are many cornercases to think about, e.g. branch cutting, numerical underflow etc. ...
HTH?