• Welcome to the new Internet Infidels Discussion Board, formerly Talk Freethought.

The Error Function

steve_bank

Diabetic retinopathy and poor eyesight. Typos ...
Joined
Nov 9, 2017
Messages
13,764
Location
seattle
Basic Beliefs
secular-skeptic
The error function and inverse error function.

Both important.

The error function is done by numerical integration. The inverse function by a series.

At small x values the precision is about 12 decimal places. It goes down as the magnitude of x increases.

The c coefficients for the inverse function could be precalculated and included as a table to speed up execution.

There is a series solution for the error function but have not tried it yet.

I went around in circles trying to figure put why I was not getting the number of decimal places I expected. Turned out I had to explicitly make arrays a double. It looks e otherwise they ended up as floats.

Code:
import math as ma
import cmath as cm

import numpy as np
import array as ar
import matplotlib.pyplot as plt
#-------------------------------------------------------------
def plotxy(xlo,xhi,ylo,yhi,x,y,title,xlabel,ylabel):
        # single y plot
        font1 = {'family': 'arial',
        'color':  'black',
        'weight': 'heavy',
        'size': 15,
        }
        [fig, p1] = plt.subplots(1)
        p1.plot(x,y,linewidth=2.0,color="k")
        p1.grid(color='k', linestyle='-', linewidth=1)
        p1.grid(which='major', color='k',linestyle='-', linewidth=0.8)
        p1.grid(which='minor', color='k', linestyle='-', linewidth=0.3)
        p1.set_xlim(xlo,xhi)
        p1.set_ylim(ylo,yhi)          
        p1.set_title(title, fontdict = font1)
        p1.set_xlabel(xlabel, fontdict = font1)
        p1.set_ylabel(ylabel, fontdict = font1)
        p1.minorticks_on()
        plt.show()
#-------------------------------------------------------------------------------
 
def coef():
    nc = 100
    c = nc*[0]
    c[0] = 1
    for k in range(nc):
        for m in range(k):
            c[k] += (c[m]*c[k-1-m])/((m+1)*(2*m + 1))
    for k in range(10):
        print(c[k])
           
#---------------------------------------------------------------------------
def erf(x):
    # error function
    # trapezoidal integration
    if abs(x) == 0.0: return 0.0
    sign = 1
    if x < 0.0:sign = -1.
    x = abs(x)
    s = 0.
    t = 0.
    n = 500000
    dt = x/n
    for i in range(n):
        y1 = ma.exp(-t*t)
        t2 = t + dt
        y2 = ma.exp(-t2*t2)
        s +=  y1 + (y2-y1) *.5
        t += dt
    s *= dt*2./ma.sqrt(ma.pi)
    return s * sign
#----------------------------------------------------------------------
def ierf(x):
    # inverse error function
    # adapted from wiki page on the error function
    nc = 1000
    c = ar.array('d',nc*[0])
    c[0] = 1
    for k in range(nc):
        for m in range(k):
            c[k] += (c[m]*c[k-1-m])/((m+1)*(2*m + 1))
    einv = 0
    q = ma.sqrt(ma.pi)*x/2.
    for k in range(nc):
        einv += ((c[k])/(2*k+1))*pow(q,(2*k+1))
    return einv
#-----------------------------------------------------------------------
def norm_dist(n,y,u,sigma):
    k = ma.sqrt(2)*sigma
    for i in range(n):
        # p>0, <1
        p = 0    
        while(p == 0):
            p = rn.random()
        p = 2.*p-1.
        y[i] = u + ierf(p)*k
 #--------------------------------------------------------------------  
n = 22
x = ar.array('d',n*[0])
y = ar.array('d',n*[0])
yi = ar.array('d',n*[0])

 # testing ierf()  
dx = .1
xsum = 0.123456789012345
for i in range(n):
        x[i] = xsum
        y[i] = erf(xsum)
        yi[i] = ierf(y[i])
        xsum += dx

for i in range(n):
    print("%+.15f    %.15f   %+.15f" %(x[i],y[i],yi[i]))
   
n = 100
t = np.linspace(-2,2,n)
y = ar.array('d',n*[0])
x = ar.array('d',n*[0])
for i in range(n):
    y[i] =  y[i] = erf(t[i])
    x[i] = ma.exp(-t[i]*t[i])
plotxy(min(t),max(t),min(y),max(y),t,y,"error function","","")
plotxy(min(t),max(t),min(x),max(x),t,x,"integrated function","","")
 
1st column x, 2nd erf(x), 3rd ierf(erf(x)), 1st and 2bd columns should be the same ideally without numerical issues.

Code:
+0.123456789012345    0.138601545069331   +0.123456789012336
+0.223456789012345    0.248009349056034   +0.223456789012325
+0.323456789012345    0.352643009080857   +0.323456789012439
+0.423456789012345    0.450732641678103   +0.423456789012074
+0.523456789012345    0.540869698208496   +0.523456789011466
+0.623456789012345    0.622061505100424   +0.623456789012959
+0.723456789012345    0.693750024171002   +0.723456789012062
+0.823456789012345    0.755796279441680   +0.823456789011629
+0.923456789012345    0.808435578977759   +0.923456789008808
+1.023456789012345    0.852211287393477   +1.023456789003331
+1.123456789012345    0.887896214521806   +1.123456789020126
+1.223456789012345    0.916410663874049   +1.223456789021615
+1.323456789012345    0.938745033629231   +1.323456789018565
+1.423456789012345    0.955892933731264   +1.423456789024585
+1.523456789012345    0.968798489460522   +1.523456789031397
+1.623456789012345    0.978319235723991   +1.623456789053199
+1.723456789012346    0.985204074534570   +1.723456788995294
+1.823456789012346    0.990084358135653   +1.823456789039765
+1.923456789012346    0.993475335017614   +1.923456762497691
+2.023456789012346    0.995784915367513   +2.023452679772552
+2.123456789012346    0.997326863578126   +2.123320813460653
+2.223456789012346    0.998335965305193   +2.221923138965717
 
Hey Steve, does this function share any identity with the hyperbolic tangent function? Asking because it looks like it would.

It has a lot of shape similarities to tanh, and looks very much like a step function approximation. I'm asking mostly because I've been playing around with products to create pulse trains out of various step functions.
 
I do not know, but it is .related to the normal distribution.

An overview



One use for the inverse function is generating a normal distribution given a mean and standard deviation.

Pass a mean u and standard deviation sigma a, sort y, and create a histogram or bar chart and you will get a normal distribution. I think Numpy has a built in to do this function.

Code:
def norm_dist(n,y,u,sigma):
    k = ma.sqrt(2)*sigma
    for i in range(n): 
        # p>0, <1
        p = 0     
        while(p == 0):
            p = rna.random()
        p = 2.*p-1.
        y[i] = u + ierf(p)*k
 #--------------------------------------------------------------------
 
I do not know, but it is .related to the normal distribution.

An overview



One use for the inverse function is generating a normal distribution given a mean and standard deviation.

Pass a mean u and standard deviation sigma a, sort y, and create a histogram or bar chart and you will get a normal distribution. I think Numpy has a built in to do this function.

Code:
def norm_dist(n,y,u,sigma):
    k = ma.sqrt(2)*sigma
    for i in range(n):
        # p>0, <1
        p = 0
        while(p == 0):
            p = rna.random()
        p = 2.*p-1.
        y[i] = u + ierf(p)*k
 #--------------------------------------------------------------------
Well, I think I recall something about normal distributions and a relationship to the Tanh function, particularly in that the Heaviside step function's Fourier Transform is a normal distribution?

This is based on a VERY fast look through literature.

That thing I was playing with a couple years back that I went a little crazy with IS actually a pretty fascinating bit of math study for me. Apparently it has some relationship to Willans Formulae?

Anyway, the connection is interesting to be because the other day I actually succeeded in making a prime counting function out of it, and as it is, it turned out to be a sum of a double product of tanh, and putting it in those terms lets me smoothly control the error.

\(\sum_{d=2}^{x}\left(\left(\prod_{v=0}^{\operatorname{floor}\left(\sqrt{d}\right)}\left(\prod_{n=0}^{\operatorname{ceil}\left(\frac{d-\left(\left(v+2\right)\cdot2\right)}{\left(v+2\right)}\right)}\ \tanh\ -k\left(d-\left(v+2\right)\left(n+2\right)\right)\right)\right)^{2}\right)\)

I spent some time this afternoon working on an identity between the tanh^2 products I'm working with and cosines/sines that I thought was interesting insofar as trying to figure out whether there's a simpler way to express this.

if the f(x) is as such
\(f\left(x\right)\ =\ \left(\left(\prod_{n=-\infty}^{-\infty}\tanh^{2}\left(k\left(\frac{x}{2}\right)-k\pi\left(n\right)\right)\right)\cdot\sqrt{2}-\frac{1}{2}\right)\)
then
\(\left(f\left(x\right)-f\left(x+\pi\right)\right)\ =\ -\cos\left(x\right)\)
and I think this holds for all K, since there's symmetry there?

Strangely for the set of integers, the f(x)=-cos(x) already.

I wonder if there's a more useful identity buried in there...

I also wonder if there's a way to express the half function in terms of plain trig? It seems like there should be a way.

\(d\left(x\right)\ =\left(\prod_{n=-\infty}^{-\infty}\tanh^{2}\left(k\left(x\right)-k\pi\left(n\right)\right)\right)\)
\(d\left(x\right)-d\left(x+\frac{\pi}{2}\right)\)
equals
\(-\frac{\cos\left(2x\right)}{\sqrt{2}}\)
 
Last edited:
The Gaussian curve (normal distribution) has several interesting and unique properties.

Once upon a time I had fun writing various simple routines in C, including code to generate standard Gaussian variates (random variables). (Here, "standard" means having zero mean and variance of 1.) On the way I became aware of something which seems very interesting. It is interesting in TWO ways: (1) it leads to almost-counterintuitive properties of the Gaussian, and (2) it leads to a speedup in the code to generate Gaussian variates. (I mentioned this here some years ago, but it seems interesting enough to mention again.)

If a and b are two independent standard Gaussian variates, then (a, b) is the (x, y) value of a TWO-dimensional Gaussian variate. Clear so far?

If you plot the density of the 2-D Gaussian distribution, you get a 3-D rendition of an actual bell, rather than just the 2-D cross-section of a bell.

That (x, y) value can be expressed in polar coordinates as (r, θ) where r = √(x^2 + y^2) and θ = tan-1(x/y). This notion leads to a stream-lined code to develop Gaussian variates:
Code:
     // randy() returns a variate uniform over (0, 1)
     theta = randy() * 2 * PI;
     r = sqrt(-2 * ln(randy());
     x = r * cos(theta);
     y = r * sin(theta);
     // x and y are each standard Gaussian variates
     // x and y are INDEPENDENT
Two calls to randy() and only four calls to simple math functions produce TWO Gaussian variates. And -- despite that each is scaled by r, the two variates are independent! Nifty? Any cross-section of this 2-D distribution will be a normal curve, but r itself (sqrt(-2 * ln(randy())) is not Gaussian. For example it has kurtosis = 2 compared with the Gaussian's kurtosis = 3.

I apologize if my comments constitute a hijack, or are off-topic. Please request a threat split if there is a problem.

Well, I think I recall something about normal distributions and a relationship to the Tanh function, particularly in that the Heaviside step function's Fourier Transform is a normal distribution?

This is based on a VERY fast look through literature.
. . .

The integral of tanh() has a "bell-curve" shape, vaguely similar to -- but distinct from -- the Gaussian bell-curve. Similarly, tanh() and the derivative of the Gaussian curve both are shaped like bounded activation functions, though again the exact shapes are different.

Other than that I have nothing
 
Jaryn I know next to nothing on number theory but I know something about step functions and transforms.

Se the post on the FFT thread, you can see the transform for yourself.
 
So, I did a bunch more experimentation last night, probably getting close to crazy-me levels of interest which might imply it's time to shelve it again until next spring.

Between last night and this morning I discovered the ability to find an integer equivalence.

\(d\left(x\right)\ =\left(\left(\prod_{x_{2}=-\frac{s}{2}}^{\frac{s}{2}}\tanh\left(k\left(x\right)-k\pi\left(2x_{2}-\frac{1}{2}\right)\right)\cdot\tanh\left(k\left(x\right)-k\pi\left(2x+\frac{1}{2}\right)\right)\right)\right)\)
this version is at least stable, apparently? Not entirely sure. Anyway,
\(d\left(\pi x\right)2^{\frac{1}{2^{2}}}=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k = 0.

Still, at different K, the convergence is different.
\(d\left(\pi x\right)=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k -> infinity

anyway, being able to step from any sort of infinite product here to some sort of sum, or even a "flat" functions that would be useful. It seems like there should be a "clean" function that doesn't use products that expresses this.

Sorry for shitting up your thread with my junk.
 
So, I did a bunch more experimentation last night, probably getting close to crazy-me levels of interest which might imply it's time to shelve it again until next spring.

Between last night and this morning I discovered the ability to find an integer equivalence.

\(d\left(x\right)\ =\left(\left(\prod_{x_{2}=-\frac{s}{2}}^{\frac{s}{2}}\tanh\left(k\left(x\right)-k\pi\left(2x_{2}-\frac{1}{2}\right)\right)\cdot\tanh\left(k\left(x\right)-k\pi\left(2x+\frac{1}{2}\right)\right)\right)\right)\)
this version is at least stable, apparently? Not entirely sure. Anyway,
\(d\left(\pi x\right)2^{\frac{1}{2^{2}}}=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k = 0.

Still, at different K, the convergence is different.
\(d\left(\pi x\right)=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k -> infinity

anyway, being able to step from any sort of infinite product here to some sort of sum, or even a "flat" functions that would be useful. It seems like there should be a "clean" function that doesn't use products that expresses this.

Sorry for shitting up your thread with my junk.
I would never discourage anyone's interest in math,

The math and science forums once lively have been dead for a while.


Post away.
 
So, I did a bunch more experimentation last night, probably getting close to crazy-me levels of interest which might imply it's time to shelve it again until next spring.

Between last night and this morning I discovered the ability to find an integer equivalence.

\(d\left(x\right)\ =\left(\left(\prod_{x_{2}=-\frac{s}{2}}^{\frac{s}{2}}\tanh\left(k\left(x\right)-k\pi\left(2x_{2}-\frac{1}{2}\right)\right)\cdot\tanh\left(k\left(x\right)-k\pi\left(2x+\frac{1}{2}\right)\right)\right)\right)\)
this version is at least stable, apparently? Not entirely sure. Anyway,
\(d\left(\pi x\right)2^{\frac{1}{2^{2}}}=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k = 0.

Still, at different K, the convergence is different.
\(d\left(\pi x\right)=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k -> infinity

anyway, being able to step from any sort of infinite product here to some sort of sum, or even a "flat" functions that would be useful. It seems like there should be a "clean" function that doesn't use products that expresses this.

Sorry for shitting up your thread with my junk.
I would never discourage anyone's interest in math,

The math and science forums once lively have been dead for a while.


Post away.
I don't suppose you have the time to try and find a more easily expressed identity here? I'm not even looking to prove any of the identities I find. Most are "intuitive" insofar as I just played around with sliders in Desmos.

I haven't even really nailed down what f(K) is in d(x)f(K) = -cos(pi*x) (x is integer).

If I can find the convergence for the more complicated stable tanh product, the tanh^2 product will follow since the squared stable product and the squared unstable product are identical.
 
So, I did a bunch more experimentation last night, probably getting close to crazy-me levels of interest which might imply it's time to shelve it again until next spring.

Between last night and this morning I discovered the ability to find an integer equivalence.

\(d\left(x\right)\ =\left(\left(\prod_{x_{2}=-\frac{s}{2}}^{\frac{s}{2}}\tanh\left(k\left(x\right)-k\pi\left(2x_{2}-\frac{1}{2}\right)\right)\cdot\tanh\left(k\left(x\right)-k\pi\left(2x+\frac{1}{2}\right)\right)\right)\right)\)
this version is at least stable, apparently? Not entirely sure. Anyway,
\(d\left(\pi x\right)2^{\frac{1}{2^{2}}}=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k = 0.

Still, at different K, the convergence is different.
\(d\left(\pi x\right)=-\cos\left(\pi x\right)\)
for all integer x, at the infinite product, for k -> infinity

anyway, being able to step from any sort of infinite product here to some sort of sum, or even a "flat" functions that would be useful. It seems like there should be a "clean" function that doesn't use products that expresses this.

Sorry for shitting up your thread with my junk.
I would never discourage anyone's interest in math,

The math and science forums once lively have been dead for a while.


Post away.
I don't suppose you have the time to try and find a more easily expressed identity here? I'm not even looking to prove any of the identities I find. Most are "intuitive" insofar as I just played around with sliders in Desmos.

I haven't even really nailed down what f(K) is in d(x)f(K) = -cos(pi*x) (x is integer).

If I can find the convergence for the more complicated stable tanh product, the tanh^2 product will follow since the squared stable product and the squared unstable product are identical.
For some reason though, I just haven't been able to really hang on to either linear algebra or calculus? Finding derivatives and integrals is just a blank in my head and I have never really grasped what I need to know to understand why derivatives and integrals.

I know the tangent line to a curve thing, it just likes to slip away often enough. Memorization comes so difficult for me! I fully intend to get tattoos with the logic next time I manage to learn it!

Any help here is appreciated, because I think I'm at the very edges of my understanding. If you would like me to discuss all of what I've been on with this, I can start a new thread and go through it from the very beginning of the idea. Ideally, I would like to have proofs for interesting stuff around the idea?

Anyway, it seems like a fun forum exercise, and something actually befitting free thinkers. Besides, maybe someone else CAN actually find this interesting.
 
Beyond straightforward textbook kinds of problems I always used numeral integration. and differentiation.


Code:
n = 1000
y = np.ndarray(shape=(n),dtype=np.double)
derv = np.ndarray(shape=(n),dtype=np.double)
integ = np.ndarray(shape=,dtype=np.double)
t = np.linspace(0,2*np.pi,b)

dt = t[1]-t[0]
for i in range:y = np.sin(t)

for i in range(n-1):derv = (y[i+1]-y[)/dt # derivative of sin numerically

When plotting derv note the length of derv is n-1.

This might get you started.
 
Beyond straightforward textbook kinds of problems I always used numeral integration. and differentiation.


Code:
n = 1000
y = np.ndarray(shape=(n),dtype=np.double)
derv = np.ndarray(shape=(n),dtype=np.double)
integ = np.ndarray(shape=,dtype=np.double)
t = np.linspace(0,2*np.pi,b)

dt = t[1]-t[0]
for i in range:y = np.sin(t)

for i in range(n-1):derv = (y[i+1]-y[)/dt # derivative of sin numerically

When plotting derv note the length of derv is n-1.

This might get you started.
I mean, the weirdest part is that I've tutored people through introductory calc, it's just that I never seem to have to find the slope function of an equation.

It's sad because I want to be able to express the slope function (a clock-like step function) of arcsin(sin(x)), because I want to eliminate a discontinuity that happens when correcting phase in polar coordinates, to guarantee I always get the right answer that won't correct into said discontinuities. It's not that I actually strictly need the step function, I just don't want to approximate it, you know? I want to find it from the terms of arcsin(sin(x))

I can't remember it entirely, and I don't know how to express a single iteration of it, but I did see a function pop out of that that did a trapezoidal wave! I'll have to see if I can find it again.
 
My math was limited to applied undergrad math like calculus, differential equations, complex variables, linear algebra, probabilities, and statistics. I needed enough depth to apply it.

Topics like prime numbers were background information.

I had a calculus teacher who said to get good at calculus you have to work problems, and he was right. I got into the habit of a yearly math and physics review. I got out texts and worked basic emblems to keep it fresh.

Derivatives apply to continuous functions. You can do piece wise differentiation over intervals.

For a step change as dx ->0 dy/dx-> infinity, a singularity. You can differentiate the function on either side of the discontinuity.

For example. A sine with a step, you differentiate the sine either side of the step. If you are puzzled on a problem code it, simulate it, and plot it. It is how I did it. Python is a good learning and problem solving tool.

Get a text and code problems.

Code:
#sine with step
n = 1000
ys = np.ndarray(shape=(n),dtype = np.double)
t = np.linspace(0,4*np.pi,n)  #radians

for i in range(n):
    ys[i] = 1*np.sin(t[i])
    if t[i] > np.pi/2: ys[i] = ys[i] + 1
                 
plotxy(0,max(t),-4,4,t,ys,"","","")
 
Wikipedia has a big  List of probability distributions - the error function is essentially the cumulative normal distribution.

The error function is also related to the  Incomplete gamma function along with the chi-squared distribution. Some cumulative statistical distributions are related to the incomplete  Beta function like the F distribution of analysis of variance (ANOVA).
 
Back
Top Bottom