Programming the error function (erf) in Python

Click For Summary

Discussion Overview

The discussion revolves around programming the error function (erf) in Python, specifically focusing on the implementation of the Maclaurin series to calculate the cumulative distribution function (CDF) of the standard normal curve. Participants explore issues related to the correctness of the implementation, potential errors in the code, and the rationale behind coding the function from scratch versus using existing libraries.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports issues with their implementation of the erf function, suggesting that the series diverges when it should converge to 1.
  • Another participant questions whether the series starts at n=0 or n=1, indicating that starting at n=1 would miss the first term.
  • Concerns are raised about the last line of the implementation, where a participant suggests that the factor should be (2 / sqrt(pi)) instead of (2 * sqrt(pi)), although they note this would not cause divergence.
  • A participant mentions that Python 2.x does not automatically promote integers to floats, which could affect the results if not handled properly.
  • There is a suggestion that using existing implementations of erf and erfc in Python would be preferable to writing a custom implementation, especially for production code.
  • Another participant confirms that n should start at 0 and asserts that their Python environment does promote floats automatically.

Areas of Agreement / Disagreement

Participants express differing views on whether to implement the erf function from scratch or use existing libraries. There is no consensus on the correctness of the initial implementation, as multiple potential issues are raised without resolution.

Contextual Notes

Participants highlight potential limitations in the implementation, including the starting index for the series, the handling of float promotion in Python 2.x, and the correctness of the mathematical formulation used in the code.

Who May Find This Useful

Individuals interested in programming mathematical functions in Python, particularly those learning about numerical methods or the error function, may find this discussion relevant.

Tiiba
Messages
53
Reaction score
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
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
 
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().
 
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.
 
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:

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
55
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K