Programming the error function (erf) in Python

In summary, I am having very unusual problems posting in the math forum. I tried calculating the erf using a series to do it, as described by Wikipedia. But mine diverges, when it should approach 1.
  • #1
Tiiba
54
0
I'm having very unusual problems posting in the math forum.

I am trying to write a program that would calculate phi, the CDF of the standard normal curve, in Python. I tried calculating the erf using a series to do it, as described by Wikipedia. But mine diverges, when it should approach 1.

PHP:
def erfterm(x, n):
   num = (-1.0)**n * x**(2*n+1)
   denum = fact(n) * (2*n+1)
   return num/denum
   
def erf(x):
   #By Maclaurin series
   def term(n):
      num = (-1.0)**n * x**(2*n+1)
      denum = fact(n) * (2*n+1)
      return num/denum
   return sum([erfterm(x, i) for i in range(25)]) *(2 * sqrt(pi))

Something seems to be wrong with erfterm, because it's giving very large results. What gives? Is the equation at Wikipedia wrong? Or did I copy it wrong? Or is my computer wrong?
 
Technology news on Phys.org
  • #2
Does n start at 0 or 1? If 1 then the series would start 2(1)+1 and one misses the first term 2(0)+1. One has to take enough terms.

By the time x gets out to 2 or -2, the erf(x) should be nearly 1.

In the last line (2 * sqrt(pi)) should be (2 / sqrt(pi)), but that's only a factor and would not contribute to divergence.

One could debug by writing out the successive terms.

The Maclaurin series on Wikipedia is correct. Confirm with
http://mathworld.wolfram.com/Erf.html
 
  • #3
Also be careful, python 2.x does not autmatically promote floats.
So 2 * sqrt() will give an integer answer, you need to write 2.0 * sqrt().
 
  • #4
If it's erf-ing for learning programming, go for it. :)

But, erf() and erfc() have been available for python since 2003 at least. AFAIK. Other than as a fun exercise, why are you re-inventing the wheel? If this were going to be production code and I was involved in the project, I would insist that you use a widely tested implementation of something rather than a roll-your-own. It's called best practice.
 
  • #5
Astronuc said:
Does n start at 0 or 1?

0

mgb_phys said:
Also be careful, python 2.x does not autmatically promote floats.

Mine does. Always did.

jim mcnamara said:
But, erf() and erfc() have been available for python since 2003 at least.

http://docs.python.org/lib/module-math.html"
 
Last edited by a moderator:

1. What is the error function (erf) in Python and why is it useful?

The error function (erf) in Python is a mathematical function that is used to calculate the area under a Gaussian curve. It is useful in statistics, physics, engineering, and other fields for solving problems related to normal distributions.

2. How do I import the error function (erf) in Python?

To use the error function (erf) in Python, you need to import it from the "math" module. You can do this by using the following code: from math import erf. This will allow you to use the erf() function in your code.

3. How do I use the error function (erf) in Python to calculate the probability of a certain event?

To calculate the probability of a certain event using the error function (erf) in Python, you can use the erf() function with the appropriate parameters. For example, to calculate the probability of a value being less than a given number in a normal distribution, you can use erf(x/√(2σ2)), where x is the given number and σ is the standard deviation.

4. Can the error function (erf) in Python handle complex numbers?

Yes, the error function (erf) in Python can handle complex numbers. It supports both real and complex numbers as input, and returns a complex number as the output.

5. Are there any other built-in functions in Python that are related to the error function (erf)?

Yes, there are other related functions in Python, such as erfc() which calculates the complementary error function, and erfinv() which calculates the inverse error function. These functions can also be found in the "math" module.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
22
Views
730
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top