Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA POISSON Law Function

Status
Not open for further replies.

T111

Programmer
Jun 28, 2003
91
IE
I need to get this function out of excel and into ASP.Net(VB). Anyone know how to do it?
Thanks,
T111!
 
I'll guess you'll need to create an Excel Application object within the vb code and then use something like;

xlObj.WorksheetFunction.Poisson(arg1,arg2,arg3)
 
Yes as you said I made a reference to the excel COM object. Then I could access all excels functions
e.g.
Code:
Dim objExcel As New Excel.Application
Dim result As Double
result = objExcel.Application.Poisson(0, 0.05, True)
I should of thinking of that!
Thanks!
 
I needed to eliminate excel so here is the function in case anyone else needs it! I found it on some website in VB 6 its updated to .NET

Code:
Imports Microsoft.VisualBasic
Imports System.Math

Public Class GlobalPoisson

    Public Function IncompleteGammaC(ByVal a As Double, _
         ByVal x As Double) As Double
        Dim result As Double
        Dim IGammaEpsilon As Double
        Dim IGammaBigNumber As Double
        Dim IGammaBigNumberInv As Double
        Dim ans As Double
        Dim ax As Double
        Dim c As Double
        Dim yc As Double
        Dim r As Double
        Dim t As Double
        Dim y As Double
        Dim z As Double
        Dim pk As Double
        Dim pkm1 As Double
        Dim pkm2 As Double
        Dim qk As Double
        Dim qkm1 As Double
        Dim qkm2 As Double
        Dim Tmp As Double

        IGammaEpsilon = 0.000000000000001
        IGammaBigNumber = 4.5035996273705E+15
        IGammaBigNumberInv = 2.22044604925031 * 0.0000000000000001
        If x <= 0.0# Or a <= 0.0# Then
            result = 1.0#
            IncompleteGammaC = result
            Exit Function
        End If
        If x < 1.0# Or x < a Then
            result = 1.0# - IncompleteGamma(a, x)
            IncompleteGammaC = result
            Exit Function
        End If
        ax = a * Log(x) - x - LnGamma(a, Tmp)
        If ax < -709.782712893384 Then
            result = 0.0#
            IncompleteGammaC = result
            Exit Function
        End If
        ax = Exp(ax)
        y = 1.0# - a
        z = x + y + 1.0#
        c = 0.0#
        pkm2 = 1.0#
        qkm2 = x
        pkm1 = x + 1.0#
        qkm1 = z * x
        ans = pkm1 / qkm1
        Do
            c = c + 1.0#
            y = y + 1.0#
            z = z + 2.0#
            yc = y * c
            pk = pkm1 * z - pkm2 * yc
            qk = qkm1 * z - qkm2 * yc
            If qk <> 0.0# Then
                r = pk / qk
                t = Abs((ans - r) / r)
                ans = r
            Else
                t = 1.0#
            End If
            pkm2 = pkm1
            pkm1 = pk
            qkm2 = qkm1
            qkm1 = qk
            If Abs(pk) > IGammaBigNumber Then
                pkm2 = pkm2 * IGammaBigNumberInv
                pkm1 = pkm1 * IGammaBigNumberInv
                qkm2 = qkm2 * IGammaBigNumberInv
                qkm1 = qkm1 * IGammaBigNumberInv
            End If
        Loop Until t <= IGammaEpsilon
        result = ans * ax

        IncompleteGammaC = result
    End Function

    Public Function IncompleteGamma(ByVal a As Double, ByVal x As Double) As Double
        Dim result As Double
        Dim IGammaEpsilon As Double
        Dim ans As Double
        Dim ax As Double
        Dim c As Double
        Dim r As Double
        Dim Tmp As Double

        IGammaEpsilon = 0.000000000000001
        If x <= 0.0# Or a <= 0.0# Then
            result = 0.0#
            IncompleteGamma = result
            Exit Function
        End If
        If x > 1.0# And x > a Then
            result = 1.0# - IncompleteGammaC(a, x)
            IncompleteGamma = result
            Exit Function
        End If
        ax = a * Log(x) - x - LnGamma(a, Tmp)
        If ax < -709.782712893384 Then
            result = 0.0#
            IncompleteGamma = result
            Exit Function
        End If
        ax = Exp(ax)
        r = a
        c = 1.0#
        ans = 1.0#
        Do
            r = r + 1.0#
            c = c * x / r
            ans = ans + c
        Loop Until c / ans <= IGammaEpsilon
        result = ans * ax / a

        IncompleteGamma = result
    End Function

    Public Function LnGamma(ByVal x As Double, ByRef SgnGam As Double) As Double
        Dim result As Double
        Dim a As Double
        Dim B As Double
        Dim c As Double
        Dim P As Double
        Dim Q As Double
        Dim u As Double
        Dim w As Double
        Dim z As Double
        Dim i As Long
        Dim LogPi As Double
        Dim LS2PI As Double
        Dim Tmp As Double

        SgnGam = 1.0#
        LogPi = 1.1447298858494
        LS2PI = 0.918938533204673
        If x < -34.0# Then
            Q = -x
            w = LnGamma(Q, Tmp)
            P = Int(Q)
            i = Round(P)
            If i Mod 2.0# = 0.0# Then
                SgnGam = -1.0#
            Else
                SgnGam = 1.0#
            End If
            z = Q - P
            If z > 0.5 Then
                P = P + 1.0#
                z = P - Q
            End If
            z = Q * Sin(PI * z)
            result = LogPi - Log(z) - w
            LnGamma = result
            Exit Function
        End If
        If x < 13.0# Then
            z = 1.0#
            P = 0.0#
            u = x
            Do While u >= 3.0#
                P = P - 1.0#
                u = x + P
                z = z * u
            Loop
            Do While u < 2.0#
                z = z / u
                P = P + 1.0#
                u = x + P
            Loop
            If z < 0.0# Then
                SgnGam = -1.0#
                z = -z
            Else
                SgnGam = 1.0#
            End If
            If u = 2.0# Then
                result = Log(z)
                LnGamma = result
                Exit Function
            End If
            P = P - 2.0#
            x = x + P
            B = -1378.25152569121
            B = -38801.6315134638 + x * B
            B = -331612.992738871 + x * B
            B = -1162370.97492762 + x * B
            B = -1721737.0082084 + x * B
            B = -853555.664245765 + x * B
            c = 1.0#
            c = -351.815701436523 + x * c
            c = -17064.2106651881 + x * c
            c = -220528.590553854 + x * c
            c = -1139334.44367983 + x * c
            c = -2532523.07177583 + x * c
            c = -2018891.41433533 + x * c
            P = x * B / c
            result = Log(z) + P
            LnGamma = result
            Exit Function
        End If
        Q = (x - 0.5) * Log(x) - x + LS2PI
        If x > 100000000.0# Then
            result = Q
            LnGamma = result
            Exit Function
        End If
        P = 1.0# / (x * x)
        If x >= 1000.0# Then
            Q = Q + ((7.93650793650794 * 0.0001 * P - 2.77777777777778 * 0.001) * P + 0.0833333333333333) / x
        Else
            a = 8.11614167470508 * 0.0001
            a = -(5.95061904284301 * 0.0001) + P * a
            a = 7.93650340457717 * 0.0001 + P * a
            a = -(2.777777777301 * 0.001) + P * a
            a = 8.33333333333332 * 0.01 + P * a
            Q = Q + a / x
        End If
        result = Q

        LnGamma = result
    End Function

End Class

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top