code for inverse erf function

pete6982

New Member
Joined
Aug 7, 2006
Messages
19
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
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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.
 
Upvote 0
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!
 
Upvote 0
Pete

A 'sqrt' function?

Why not just use VBA's Sqr function?:)
 
Upvote 0
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 :)
 
Upvote 0

Forum statistics

Threads
1,216,434
Messages
6,130,611
Members
449,584
Latest member
c_clark

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top