Results 1 to 7 of 7

code for inverse erf function

This is a discussion on code for inverse erf function within the Excel Questions forums, part of the Question Forums category; I am doing work with confidence intervals for normally distributed data and I want to be able to calculate specific ...

  1. #1
    New Member
    Join Date
    Aug 2006
    Location
    Louisville, KY
    Posts
    19

    Default code for inverse erf function

    I am doing work with confidence intervals for normally distributed data and I want to be able to calculate specific intervals (such as .92, etc). If x is the mean and y is the std dev I know that CI=(x-ny,x+ny), where n=sqrt(2)*inverseERF(CI).

    I tried to make a vba function to calculate the invERF, but I'm off by a bit.

    Does anyone know of an existing invERF funcion? I can't find actual code for one anywhere.

    Thanks,
    Pete

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092

    Default

    There are ERF and ERFC functions in the Analysis ToolPak, but I don't know if those are what you want. There is also a CONFIDENCE function.

  3. #3
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    18,453

    Default

    Hi Pete

    Look at the function CONFIDENCE() as Andrew said. If, however, you really need to compute the value of the invERF there is a small code (posted by JJ 05-18-06 09:20). It is in Pascal and so should be very easy to port to vba. The name of the function is invErf

    http://les-mathematiques.u-strasbg.f...86277&t=286277

    Hope this helps
    PGC

  4. #4
    New Member
    Join Date
    Aug 2006
    Location
    Louisville, KY
    Posts
    19

    Default

    thanks guys. i was able to recode the pascal invERF function into vba. My worksheet works like a charm now.

    in case anyone else ever wants a vba version, here's what I ended up with:
    Code:
    Function invERF(y As Double) As Double
    Dim pi As Double, x As Double, d As Double
    pi = 3.14159265358979
    If y < 0 Then
        invERF = 0 'interval includes the mean only
        Exit Function
    ElseIf y >= 1 Then
        invERF = 10 'makes the interval include everything
        Exit Function
    'for my purposes, I only want the function to process input from 0 to 1
    ElseIf y < 0.596 Then
        x = sqr(pi) / 2 * y * (1 + (pi / 12) * y * y)
    Else
        x = sqr(-Log((1 - y) * sqr(pi)))
    End If
    d = (y - ERF(x)) / (2 * Exp(-x * x) / sqr(pi))
    x = x + d
    Do While Abs(d) >= 0.00000001
        d = (y - ERF(x)) / (2 * Exp(-x * x) / sqr(pi))
        x = x + d
    Loop
    invERF = x
    End Function
    
    Function ERF(x As Double) As Double
    
    Dim f As Double, c As Double, pi As Double
    Dim j As Integer
    c = 0
    pi = 3.14159265358979
    If 1.5 < x Then
        c = 2 - c
        j = 3 + Int(32 / x)
        f = 0
        Do While j <> 0
            f = 1 / (f * j + x * sqr(2))
            j = j - 1
        Loop
        f = f * c * (3 - c * c) * Exp(-x * x) / sqr(2 * pi) + (c - 1) * (3 * c * c + c - 8) / 6
    Else
        j = 3 + Int(9 * x)
        f = 1
        Do While j <> 0
            f = 1 + f * x * x * (0.5 - j) / j / (0.5 + j)
            j = j - 1
        Loop
        f = c + f * x * (2 - 4 * c) / sqr(pi)
    End If
        ERF = f
    End Function
    thanks for the help!

  5. #5
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    70,735

    Default

    Pete

    A 'sqrt' function?

    Why not just use VBA's Sqr function?
    If posting code please use code tags.

  6. #6
    New Member
    Join Date
    Aug 2006
    Location
    Louisville, KY
    Posts
    19

    Default

    hehe, didn't even know it was there

  7. #7
    New Member
    Join Date
    Nov 2015
    Posts
    1

    Default Re: code for inverse erf function

    Quote Originally Posted by pete6982 View Post
    thanks guys. i was able to recode the pascal invERF function into vba. My worksheet works like a charm now.

    in case anyone else ever wants a vba version, here's what I ended up with:
    Code:
    Function invERF(y As Double) As Double
    Dim pi As Double, x As Double, d As Double
    pi = 3.14159265358979
    If y < 0 Then
        invERF = 0 'interval includes the mean only
        Exit Function
    ElseIf y >= 1 Then
        invERF = 10 'makes the interval include everything
        Exit Function
    'for my purposes, I only want the function to process input from 0 to 1
    ElseIf y < 0.596 Then
        x = sqr(pi) / 2 * y * (1 + (pi / 12) * y * y)
    Else
        x = sqr(-Log((1 - y) * sqr(pi)))
    End If
    d = (y - ERF(x)) / (2 * Exp(-x * x) / sqr(pi))
    x = x + d
    Do While Abs(d) >= 0.00000001
        d = (y - ERF(x)) / (2 * Exp(-x * x) / sqr(pi))
        x = x + d
    Loop
    invERF = x
    End Function
    
    Function ERF(x As Double) As Double
    
    Dim f As Double, c As Double, pi As Double
    Dim j As Integer
    c = 0
    pi = 3.14159265358979
    If 1.5 < x Then
        c = 2 - c
        j = 3 + Int(32 / x)
        f = 0
        Do While j <> 0
            f = 1 / (f * j + x * sqr(2))
            j = j - 1
        Loop
        f = f * c * (3 - c * c) * Exp(-x * x) / sqr(2 * pi) + (c - 1) * (3 * c * c + c - 8) / 6
    Else
        j = 3 + Int(9 * x)
        f = 1
        Do While j <> 0
            f = 1 + f * x * x * (0.5 - j) / j / (0.5 + j)
            j = j - 1
        Loop
        f = c + f * x * (2 - 4 * c) / sqr(pi)
    End If
        ERF = f
    End Function
    thanks for the help!
    SO basically i am a complete idiot where it comes to this but does this still work as i am trying to do it for some work i have to do and it keeps coming up with the error #NAME?

    If needed can give more info

    Thanks

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com