Banner MathFinance - The bridge between investment banking and academic research in mathematical finance.
MathFinance - Financial Functions Library in Visual Basic

MathFinance - Financial Functions Library in Visual Basic

Here we present a number of auxiliary functions written in visual basic. It covers the one- and two-dimensional standard-normal distribution.
 
 
 
'*** returns the cumulative distribution function of a standard normal random variable, see Hull
Function nc(X) As Double
Dim a(1 To 5) As Double
If X < -7 Then
    nc = ndf(X) / Sqr(1 + X * X)
ElseIf X > 7 Then
    nc = 1 - nc(-X)
Else
nc = 0.2316419
a(1) = 0.31938153
a(2) = -0.356563782
a(3) = 1.781477937
a(4) = -1.821255978
a(5) = 1.330274429
nc = 1 / (1 + nc * Abs(X))
nc = 1 - ndf(X) * (a(1) * nc + a(2) * nc ^ 2 + a(3) * nc ^ 3 + a(4) * nc ^ 4 + a(5) * nc ^ 5)
If (X <= 0) Then nc = 1 - nc
End If
End Function
'******************************************************************************************************************
'*** returns the density function of a standard normal random variable
Function ndf(X) As Double
    ndf = 0.398942280401433 * Exp(-X * X * 0.5) '0.398942280401433 = 1/sqareroot(2*pi)
End Function
'******************************************************************************************************************
'*** function needed to calculate two dimensional cumulative distribution function, see Hull
Function fxy(X As Double, y As Double, a, b, rho As Double) As Double
    Dim a_s As Double
    Dim b_s As Double
    a_s = a / Sqr(2 * (1 - rho ^ 2))
    b_s = b / Sqr(2 * (1 - rho ^ 2))
    fxy = Exp(a_s * (2 * X - a_s) + b_s * (2 * y - b_s) + 2 * rho * (X - a_s) * (y - b_s))
End Function
'***************************************************************************************************************************
'*** function needed to calculate two dimensional cumulative distribution function, see Hull
'*** this equals ND2 if a and b and rho are all nonpositive, the generalization for the other cases is ND2 below
Function Ntwo(a, b, rho As Double) As Double
    Dim aij(0 To 3) As Double
    Dim bij(0 To 3) As Double
    Dim i As Double
    Dim j As Double
    aij(0) = 0.325303
    aij(1) = 0.4211071

    aij(2) = 0.1334425
    aij(3) = 0.006374323
    bij(0) = 0.1337764
    bij(1) = 0.6243247
    bij(2) = 1.3425378
    bij(3) = 2.2626645
    Ntwo = 0
    For i = 0 To 3
        For j = 0 To 3
            Ntwo = Ntwo + aij(i) * aij(j) * fxy(bij(i), bij(j), a, b, rho)
        Next j
    Next i
    Ntwo = Ntwo * Sqr(1 - rho ^ 2) / 3.141592654
End Function
'***************************************************************************************************************************
'*** calculates cumulative distribution function for a bivariate normal distribution, see Hull page 245, 246
Function ND2(a, b, rho As Double) As Double
    Dim rho1 As Double
    Dim rho2 As Double
    Dim denominator As Double
    If rho > 0.9999 Then
        ND2 = nc(Application.Min(a, b))
    ElseIf rho < -0.9999 Then
        ND2 = Application.Max(0, nc(a) - nc(-b))
    Else
        If a * b * rho <= 0 Then
            If a <= 0 And b <= 0 And rho <= 0 Then
                ND2 = Ntwo(a, b, rho)
            ElseIf a <= 0 And b * rho >= 0 Then
                ND2 = nc(a) - Ntwo(a, -b, -rho)
            ElseIf b <= 0 And rho >= 0 Then
                ND2 = nc(b) - Ntwo(-a, b, -rho)
            Else
                ND2 = nc(a) + nc(b) - 1 + Ntwo(-a, -b, rho)
            End If
        Else
            denominator = Sqr(a ^ 2 - 2 * rho * a * b + b ^ 2)
            rho1 = (rho * a - b) * Sgn(a) / denominator
            rho2 = (rho * b - a) * Sgn(b) / denominator
            ND2 = ND2(a, 0, rho1) + ND2(b, 0, rho2) - (1 - Sgn(a) * Sgn(b)) / 4
        End If
        If ND2 < 0 Then ND2 = 0
    End If
End Function
'********************************************************************************************
Ads
Suchen in:
Suchbegriffe:
In Partnerschaft mit Amazon.de























Email Section
Contacts

© MathFinance AG